jeudi 5 janvier 2017

Using Java reflection mechanism from within a JAR on Android

In my AndroidStudio project I want to use a JAR library that uses reflection mechanism to dynamically load a class and run its methods. However, it fails to load the class and I'm getting the ClassNotFoundException.

When I try to load the class from within an activity -- it works. It only fails when the system tries to load the class from within the library. What is more, the library cannot load classes that are with the library, resulting in the same error. Furthermore, I cannot load the classes from the library, even from the activity. The class is loaded as follows:

Class<?> actionClass = Class.forName(callbackClass,true,ClassLoader.getSystemClassLoader());

The exception thrown looks as follows:

     java.lang.ClassNotFoundException: demos.callbacks.GetDayOfAWeekCallback
   at java.lang.Class.classForName(Native Method)
   at java.lang.Class.forName(Class.java:324)
   at com.example.tomek.test1.MainActivity.buttonOnClick(MainActivity.java:38)
   at java.lang.reflect.Method.invoke(Native Method)
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
   at android.view.View.performClick(View.java:5198)
   at android.view.View$PerformClick.run(View.java:21147)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5417)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "demos.callbacks.GetDayOfAWeekCallback" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    ... 14 more
 Suppressed: java.lang.ClassNotFoundException: demos.callbacks.GetDayOfAWeekCallback
   at java.lang.Class.classForName(Native Method)
   at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
   at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
  ... 15 more
 Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available





Aucun commentaire:

Enregistrer un commentaire