dimanche 19 août 2018

Using native library in external classes loaded dynamically (with reflection)

In my application, I'm using DexClassLoader to load a class of an external apk as follow:

DexClassLoader dexClassLoader = new DexClassLoader("path/to/someApkfile.apk", myOptimizedDirectory, null, myContext.getClassLoader());
Class<?> MyClass =  dexClassLoader.loadClass("its.package.name.MyClass");

Then invoking a method using reflection as follow:

Object myInstance = MyClass.newInstance();
MyClass.getDeclaredMethod("foo").invoke(myInstance);

here is the method foo:

public void foo() {
 System.Load("path/to/arch/specific/native/lib/libname.so");
 // I know that this native library loaded successfully because JNI_OnLoad is called.

 // here is the problem
 myNativeMethod("arg");
}

Here is the log message:

mMessages: uncaught exception
    java.lang.UnsatisfiedLinkError: No implementation found for int its.package.name.MyClass.myNativeMethod(java.lang.String) (tried Java_its_package_name_MyClass_myNativeMethod) ...

I am sure that the method signature in my native code is correct and nothing is wrong there.

Also, I'm sure that it successfully loaded native library, so why I does it say No implementation found?


Please don't say why are you doing this? or it is error prone or it is a bad practice, just point out what is the problem.

Thanks in advance :)





Aucun commentaire:

Enregistrer un commentaire