简体   繁体   中英

Class not found only on android 6.0+

Android 6.0+ does not find a class compiled in same app but in another module/lib.

Example:

com.example.app (Default app package)

com.example.lib (Library compiled)

When I call some class from app default package using reflection it only works on android version before 6

Android 4.4.2 (WORKS)

Class.forName("com.example.lib.SomeClass");

Android 6.0 (FAILS ClassNotFoundException)

Class.forName("com.example.lib.SomeClass");

This usually happens when SomeClass is loaded with a different ClassLoader than the application class loader (the one which loaded Class ). Potential causes can be that you have multiple dex files or that you are using instant run so Android will use more than one ClassLoader to load all the classes in your application.

If you added your library as a compile dependency you normally should be able to refer/get it using SomeClass.class in your code, instead of Class.forName("com.example.lib.SomeClass"). If that is not possible you need to somehow get hold of the ClassLoader which loaded SomeClass and call classLoader.findClass("com.example.lib.SomeClass")

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