简体   繁体   中英

Why java.lang.NoSuchMethodError when dynamically load a dex jar?

There is a interface IA class in APK file. I create a jar containing IA's implementation MA . The jar has a Global class and B class . The jar is loaded dynamically with Dexclassloader from data directory.

interface IA {
  test();
}

class Global {
   IA instance;  //refer to  MA instance.
}

class B {
  ....
  instance.test() ; // throw NoSuchMethodError
....
} 

But it's OK to call instance's test method through reflection.

Why? Surely I am not familiar with some principals of Classloader or DexClassloader. Could someone give explanation for me? Thanks

I too got a NoSuchMethod error, after doing some testing I found out that the problem was that the DEX file inside the JAR wasn't named classes.dex as it's supposed to be. The fact that the app was able to create a new instance of your class doesn't necessarily mean that the DEX was loaded properly, I tested it with an empty JAR (ie removed all internal files via 7-ZIP) and was still able to instantiate my class (although I guess it would work only with a default constructor that gets zero parameters).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM