Android 上的 NoClassDefFoundError - 一个非常奇怪的情况?
我面临着一个奇怪的情况,该类肯定在我的 android 应用程序的类路径上,但我不断收到 NoClassDefFoundError 异常。
我已经通过以下代码片段检查了该类确实存在:
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
在设备上运行应用程序时,日志会打印类名称,并在访问它时抛出 NoClassDefFoundError 错误。
我在这里迷路了。
有什么建议吗?我将非常感激。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要了解,至少在规范的 Java 中,“NoClassDefFoundError”并不意味着在类路径中找不到指定的 .class 文件。相反,在找到它之后,某些问题阻止了该类的加载。
出现这种情况的两个常见原因是命名问题(类在类路径结构中的位置错误,相对于其内部声明的包名称,或者只是命名错误),或者加载执行命名类验证所需的其他类时出现问题。有时,问题是由于类的静态初始值设定项中的异常造成的。
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.