1

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
 int result = myNativeMethod("arg");
}

And here the myNativeMethod:

private native int myNativeMethod(String 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 does it say No implementation found?

Any help would be appreciated.

JoshDM
  • 4,939
  • 7
  • 43
  • 72
amrezzd
  • 1,787
  • 15
  • 38
  • I believe you will get same error even if you don't *invoke* the method **foo()**. That's because Java must resolve the native methods when it loads the class, does not defer this until the actual native method is called. – Alex Cohn Aug 19 '18 at 09:49
  • Are you using Proguard to shrink the code? Maybe the ProGuard obfuscation procedure has changed the name of that method. Try to add "STATIC" in the native declaration: "private static native int myNativeMethod(String arg);" because from the error log I don't understand if the "MyClass.myNativeMethod(..)" refers to a try to read a STATIC method or not. – emandt Aug 19 '18 at 10:08
  • @AlexCohn belive me, there is no error till calling `myNativeMethod`. I think it resolves the native methods but fail to link them. – amrezzd Aug 19 '18 at 10:12
  • @emandt Progrard is disabled in debug mode. – amrezzd Aug 19 '18 at 10:17

1 Answers1

0

I'm not sure but I faced similar issue when tried to get objecy's ClassLoader using Object.getClassLoader() and tried to get Object's Methods that are not found. I solved this issue by using "ClassLoader.getSystemClassLoader()" instead of "myContext.getClassLoader()" for "parent" parameter.

emandt
  • 2,547
  • 2
  • 16
  • 20