在运行时出现NoClassDefFoundError,但类在classes.dex中,是什么? [英] NoClassDefFoundError at runtime but class is in classes.dex, what givies?

查看:306
本文介绍了在运行时出现NoClassDefFoundError,但类在classes.dex中,是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经包含了一个从Eclipse Android项目构建的JAR文件,并且在我的Android Studio项目中引用了它,如下所示:

  compile files('libs/libraryproject.jar')

这有效,并且我能够自动完成代码引用.当我编译APK时,一切都很好.我安装并运行,然后收到no class def错误:

 java.lang.NoClassDefFoundError: com.android.canvas.CanvasContainer
            at com.app.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

但是,当我解压缩APK并使用dexdump查看classes.dex文件时,我看到上面的类存在.

为什么然后在运行时出现此类未找到错误?

更新:

由于Chris的建议,我注意到在日志的前面,由于无法解析interface 1990 'Lorg/cocos2dx/lib/Cocos2dxHelper$Cocos2dxHelperListener;',因此无法链接我的CanvasContainer类.显然,我的Cocos库代码似乎没有作为JAR的一部分导出.

解决方案

具有运行时无法满足的依赖关系的类通常会在安装,dex优化或类似的准备过程中删除.

如果卸载该应用程序,启动一些收集所有logcat的内容,然后再次安装它,则可以通过搜索结果来发现在安装/准备过程中生成的日志中隐含了该问题./p>

在此特定情况下,对该问题的编辑表明丢失的类本身具有这种不满意的依赖关系.

I have included a JAR file built from an Eclipse Android project, and I am referencing it in my Android Studio project like the following:

  compile files('libs/libraryproject.jar')

This works, and I am able to get auto-complete on code references. When I compile the APK everything is fine. I install and run, and then receive a no class def error:

 java.lang.NoClassDefFoundError: com.android.canvas.CanvasContainer
            at com.app.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

However, when I unzip the APK, and use dexdump to view the classes.dex file, I see that the above class exists there.

Why then am I getting this class not found error at runtime?

Update:

Thanks to Chris's suggestion I noticed that earlier in the logs, my CanvasContainer class could not be linked due to it not being able to resolve interface 1990 'Lorg/cocos2dx/lib/Cocos2dxHelper$Cocos2dxHelperListener;'. Pretty apparently it looks like my Cocos library code is not getting exported as part of my JAR.

解决方案

A class with dependencies which cannot be met at runtime will usually be dropped during installation, dex optimization or similar preparatory process.

If you uninstall the app, start up something collecting all of logcat and then install it again, you may find by searching through the result that there is mention of the issue buried in the logs generated during the installation/preparation process.

In this specific case here, an edit to the question indicates that the missing class itself had this type of unsatisfied dependency.

这篇关于在运行时出现NoClassDefFoundError,但类在classes.dex中,是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆