I am facing with a wierd situation where the class is definitely on the classpath of my android app but I keep getting NoClassDefFoundError exceptions.
I have checked that the class indeed exists through this code snippet:
try{
Field dexField = PathClassLoader.class.getDeclaredField("mDexs");
dexField.setAccessible(true);
PathClassLoader classLoader =
(PathClassLoader)Thread.currentThread().getContextClassLoader();
Log.d("OUT", "Class loader" + classLoader.getClass().getName());
DexFile[] dexs = (DexFile[]) dexField.get(classLoader);
Log.d("OUT", "Enumerating");
for (DexFile dex : dexs) {
Enumeration<String> entries = dex.entries();
while (entries.hasMoreElements()) {
// (3) Each entry is a class name, like "foo.bar.MyClass"
String entry = entries.nextElement();
Log.d("OUT", "Entry: " + entry);
}
}
}catch(Throwable e){
Log.e("OUT", e.getMessage(), e);
}
// Here I reference to the problematic class
When running the app on a device the log does print the class name, and throws the NoClassDefFoundError error upon accessing to it.
I’m lost here.
Any advice? I’d be very grateful. Thanks.
You need to understand that, at least in canonical Java, “NoClassDefFoundError” does not mean that the named .class file could not be found in the classpath. Rather, after it was located some problem prevented the class from being loaded.
Two common reasons for this are a naming problem (the class in the wrong location in the classpath structure, relative to its internally declared package name, or simply misnamed), or a problem loading some other class that’s needed to perform validation of the named class. Occasionally the problem is due to an exception in the static initializer of the class.