Android 上 Java 库中的代码出现 NoClassDefFoundError
我的用户经常遇到错误。应用程序在启动过程中崩溃。当应该加载 MainActivity 时,VM 显然找不到该类。我不明白为什么。该应用程序的架构是,我的免费版和专业版都使用一个通用项目。不知道是否相关。请参阅下面的堆栈跟踪。有什么想法吗?
java.lang.NoClassDefFoundError: com.android.common.MainActivity
at com.mycompany.myapp.Splash.onCreate(Splash.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.android.common.MainActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.android.pro-1.apk]
编辑: 感谢您在下面的评论,理查德。现在我已将 com.android.Splash 更改为其他内容。无论如何,这不是真正的类名。我的坏...!
I am experiencing an error quite often among my users. The app crashes during startup. When the MainActivity is supposed to be loaded the VM apparently cannot find the class. I cannot figure out why. The architecture of the app is that there is a common project that both my free and pro version are using. Don't know if it is relevant. See the stack trace below. Any thoughts?
java.lang.NoClassDefFoundError: com.android.common.MainActivity
at com.mycompany.myapp.Splash.onCreate(Splash.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.android.common.MainActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.android.pro-1.apk]
Edit:
Thanks for the comment below, Richard. Now I have changed com.android.Splash
to something else. It wasn't the real classname anyway. My bad...!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
我遇到了同样的问题,我执行了以下操作来解决该问题。
- 项目中的自动化代码(来自依赖项和参考)
src - 项目中的源代码
无需重新启动 Eclipse。它刚刚开始工作。
老实说,我从未尝试过“Android Tools > Fix Project Properties”,有时它可能会做同样的事情。我不知道,我只是在看到错误消息后执行了上述操作,认为构建路径有问题。
编辑
后来这还不够,我再次收到错误。然后我“检查”了该视图中列出的所有依赖项。现在又可以了。到目前为止,一切都很好。如果再次失败,我会保持更新。
仅供参考:在我上次尝试中,我尝试了“Android 工具 > 修复项目属性”,但对我来说没有成功。
I had the same issue, I did the following to fix the problem.
gen - automated code in project (from dependencies and references)
src - source code in project
There was no need to restart the Eclipse. It just started working.
Honestly I have never tried "Android Tools > Fix Project Properties", sometimes it might be doing the same thing. I do not know, I just did above after seen the error message, thinking something is wrong with the build paths.
Edit
Later on it was not sufficient, I was getting the error again. Then I "checked" all the dependencies listed in that view. Now it works again. So far so good. I will keep this updated if it fails again.
FYI: in my last attempt, I tried "Android Tools > Fix Project Properties", but it didn't work out for me.
尝试转到项目 ->属性-> Java 构建路径 ->订单&导出并确保为您的项目和您正在使用的所有其他库项目检查 Android 私有库。我通过以下链接得到了解决方案 NoClassDefFoundError Android 项目?
Try going to Project -> Properties -> Java Build Path -> Order & Export and ensure Android Private Libraries are checked for your project and for all other library projects you are using. i got the solution by following below link NoClassDefFoundError Android Project?
我目前使用的是 SDK 20.0.3,之前的解决方案都不适合我。
我可以通过
这带来了创建的 Java 类文件,这些文件是在构建 Android 项目时为 Dalvik VM 而不是 Java VM 编译的。这允许 Android 库 jar 文件中的 Java 类文件在创建 Android 项目 .apk 时通过 deshing。现在,当应用程序运行时,会找到 Java 项目类,而不是抛出 Java.lang.NoClassDefFoundError。
如果您想使用 jar 文件而不是链接源,那么您将需要创建一个 Library Android 项目。 (在“属性”->“Android”中选中“is 库”的 Android 项目。)然后,您需要将 Java 项目的源链接到 Android 库项目(如上所述),或者从“src”文件夹复制源文件将 Java 项目的文件复制到 Android 库项目的“src”文件夹中。构建 Android 库项目。然后,您将能够将创建的 Android 项目 jar 文件复制到 Android 文件夹的“libs”文件夹中,因为其中的类文件是在构建 Android 项目时为 Davlik VM 而不是 Java VM 编译的。这允许 Android 库 jar 文件中的 Java 类文件在创建 Android 项目 .apk 时通过 deshing。现在,当应用程序运行时,将找到 Java 项目类,而不是抛出 Java.lang.NoClassDefFoundError。
I'm currently using SDK 20.0.3 and none of the previous solutions worked for me.
I was able to get things to work by
This brought this created Java class files that were compiled for the Dalvik VM instead of the Java VM when the Android project was built. This allowed the Java class files in the Android Library jar file to go thru dexing when the Android project .apk was created. Now when the app was run the Java project classes were found instead of a Java.lang.NoClassDefFoundError being thrown.
If you want to use a jar file instead of linking the source, then you will need to create a Library Android project. (An Android Project with 'is library' checked in Properties -> Android.) You will then need to either link the source of the Java Project to the Android Library project as described above or copy the source files from the 'src' folder of the Java Project to the 'src' folder of the Android Library project. Build the Android Library project. Then you will be able copy the Android Project jar file that was created into the 'libs' folder of the Android folder because the class files in it were compiled for the Davlik VM instead of the Java VM when the Android project was built. This allows the Java class files in the Android Library jar file to go thru dexing when the Android project .apk is created. Now when the app is run the Java project classes will be found instead of a Java.lang.NoClassDefFoundError being thrown.
以防万一它对某人有帮助,我也遇到了同样的问题。我的 jar 文件位于 libs 目录下,并且该 jar 已添加到构建路径中。但它仍然抛出异常。
我刚刚清理了该项目,它对我有用。
日食->项目->清洁->选择项目并清理
Just in case it helps someone, I faced the same issue. My jar file was under the libs directory and the jar was added to the build path. Still it was throwing that exception.
I just cleaned the project and it worked for me.
Eclipse -> Project -> Clean -> Select the project and clean
我尝试了上述所有解决方案,但对我来说没有成功,
这是我为完成它所做的
项目->配置构建路径->订单及出口->将依赖项目移到顶部
I tried all of the above said solutions but did not worked out for me,
Here is what i did to get it done
project-> config build path-> order and export-> move dependent project on top
当您制作使用 Google API(例如地图)的应用程序但在面向 Android API 的设备上运行该应用程序时,也会生成此错误。
This error is also generated when you make an app that uses the Google API (such as Maps) but run it on a device that targets the Android API.
我在使用java.nio.charset.StandardCharsets时遇到了这个NoClassDefFoundError。
原因是 StandardCharsets 直到 JRE 1.7 才存在。如果我将 java 编译版本设置为 1.7,Eclipse 会抱怨“Android 需要编译器合规级别 5.0 或 6.0”。因此,我通过右键单击项目名称 -> Android Tools - > 修复项目属性来修复它。它是用JRE1.6编译的。
然而,因为StandardCharsets直到1.7才存在。当我运行它时它报告NoClassDefFoundError。
直到尝试了很多其他方法(包括重新安装 JDK)之后,我才意识到这一点。 NoClassDefFoundError 的含义已经清楚地说明了真正的原因:尽管编译通过了,但在运行时找不到该类。
一般的结论是,只要Android不支持JRE 1.7,如果您使用1.7以来提供的任何新功能,您都会遇到此错误。
我的解决方案是我将这些源代码复制到我的代码中!
I encountered this NoClassDefFoundError when I used java.nio.charset.StandardCharsets.
The reason is that StandardCharsets has not existed until JRE 1.7. If I make the java compile version set to 1.7, Eclipse complained that "Android requires compiler compliance level 5.0 or 6.0". So, I fixed it by right-click the project name->Android Tools->Fix Project Properties. It is compiled with JRE1.6.
However, because StandardCharsets has not existed until 1.7. It reported NoClassDefFoundError when I ran it.
I has not come to realize this until after trying a lot of other methods including reinstalling JDK. The real reason is clearly told by the meaning of NoClassDefFoundError: The class cannot be found at run time although it passed compilation.
General conclusion is that as long as Android does not work with JRE 1.7, if you use any new feature provided since 1.7, you will encounter this error.
My solution is that I copied those source code into my code!
NoClassDefFoundError 的描述来自 SO 标签:
或者更好:
来自此页面。检查一下,有一些方法可以解决该错误。我希望它有帮助。
The NoClassDefFoundError description is, from the SO tag:
Or better:
from this page. Check it, there are some ways to solve the error. I hope it helps.
就我而言,我试图将使用 jre 1.7 编译的普通 java 类(来自普通的 java 项目)添加到使用 jre 1.7 编译的 android 应用程序项目中。
解决方案是使用 jre 1.6 重新编译该普通 java 类,并像往常一样添加对 android 应用程序项目(也使用 jre 1.6 编译)的引用(按 Tab 键顺序和导出,请务必检查类、项目等)。
当使用 android 库引用外部普通 java 类时,过程相同。
当从普通的java项目编译普通的java类并尝试在android应用程序或android库项目中引用它们时,不知道jre 1.7有什么问题。
如果您不使用普通的 java 类(来自普通的 java 项目),则无需降级到 jre 1.6。
In my case, I was trying to add a normal java class (from a normal java project) compiled with jre 1.7 to an android app project compiled with jre 1.7.
The solution was to recompile that normal java class with jre 1.6 and add references to the android app project (compiled with jre 1.6 also) as usual (in tab order and export be sure to check the class, project, etc).
The same process, when using an android library to reference external normal java classes.
Don't know what's wrong with jre 1.7, when compiling normal java classes from a normal java project and try to reference them in android app or android library projects.
If you don't use normal java classes (from a normal java project) you don't need to downgrade to jre 1.6.
对我来说,这个问题与我使用的 API 未部署有关。例如,我使用
ZSDK_API.jar
作为参考。为了解决这个问题,我必须右键单击该项目并选择属性。从 JAVA BUILD PATH 中,我必须添加未部署的 API。这很奇怪,因为应用程序抛出 MainActivity 类未找到,而实际上该错误是由未找到 DiscoveryHandler 类引起的。
希望这对某人有帮助。
For me this problem was related to the API that I was using not being deployed. For example, I used a
ZSDK_API.jar
as a reference.To fix the issue I had to right click on the project and select properties. From JAVA BUILD PATH, I had to add the API that was not being deployed. This was weird as the app was throwing MainActivity class not found and in reality the error was caused by the DiscoveryHandler class not being found.
Hopes this helps someone.
我遇到了 NoClassDefFoundError 对于我的项目中存在的类(不是库类)。该类存在,但我收到 NoClassDefFoundError。就我而言,问题在于 multidex 支持。问题和解决方案在这里: Android Multidex 和支持库
对于 Android,您会收到此错误5.0以下版本。
I met NoClassDefFoundError for a class that exists in my project (not a library class). The class exists but i got NoClassDefFoundError. In my case, the problem was multidex support. The problem and solution is here: Android Multidex and support libraries
You get this error for Android versions lower than 5.0.
在 Android Studio 上:
1) 在您的默认配置中添加
multiDexEnabled = true
2) 在您的依赖项中添加编译com.android.support:multidex:1.0.0
3)
Application class
扩展MultiDexApplication
而不仅仅是应用程序
On Android Studio:
1) Add
multiDexEnabled = true
in your default Config2) Add compile com.android.support:multidex:1.0.0 in your dependencies
3)
Application class
extendMultiDexApplication
instead of justApplication
其他想法。
例如,您有从“android.support.v4.app.Fragment”派生的类。
然而你犯了一个错误并从“android.app.Fragment”继承了它。然后您将在 Android 2 设备上遇到此错误。
Other idea.
For example, you have class derived from "android.support.v4.app.Fragment".
However you made a mistake and inherited it from "android.app.Fragment". Then you will have this error on the Android 2 devices.
我刚刚发现了这个错误。
只需确保库 jar 文件包含 android.support.v7.appcompat 包下已编译的 R 类,并将 v7 appcompat 支持“项目”中的所有资源复制到 ANDROID_SDK_HOME/extras/android/support/v7/appcompat 文件夹中。
我将 Netbeans 与 Android 插件一起使用,这解决了我的问题。
I have just figured out something with this error.
Just make sure that the library jar file contains the compiled R class under android.support.v7.appcompat package and copy all the res in the v7 appcompat support "project" in your ANDROID_SDK_HOME/extras/android/support/v7/appcompat folder.
I use Netbeans with the Android plugin and this solved my issue.
我猜测您在创建公共库时没有指定
javac
的目标,因此javac
自动使用最新的可用目标,可能是 1.7 (Java7) 或1.8(Java8)。已经说过了
Android 要求Android 构建工具的
dx
dx
19.0.0 无法将 Java7(或更高版本)字节码转换为 Dalvik 字节码。因此,要么使用构建工具版本 >= 19.0.0,要么使用
javac
和 -target 6,例如修改您的 antbuild.xml
,如下所示:I am guessing that you don't specify
javac
's target when creating the common library, sojavac
automatically uses the latest available target, which is likely 1.7 (Java7) or 1.8 (Java8).It has already been stated that
dx
of Android's build tools < 19.0.0 isn't able to convert Java7 (or higher) bytecode to Dalvik bytecode.So either use a build tools version >= 19.0.0 or use
javac
with -target 6, by modifying for example your antbuild.xml
like this:我通过手动在project.properties中添加库项目路径解决了这个问题。
一些 Eclipse 没有自动添加此条目以及“添加项目”的原因。因此,当应用程序尝试引用 lib 项目内的任何组件时,它就会崩溃。
你也可以尝试同样的事情。 projec.properties 中的应用程序依赖项,例如
并运行 .
I fixed this issue by adding library project path in project.propertied manually.
some how eclipse did not added this entry automaticvally along with "add project". so the point where app was trying to refer any componenrt inside lib project it was crashing .
you also can try the same thing . app dependecy in projec.properties like
and run .
jar 曾经位于 lib/ 文件夹中,现在位于 libs/ 中
如果您使用 lib,您可以将 jar 文件从 lib 移动到 libs,并从项目属性/java 构建路径中删除依赖项,因为 Android 现在会自动找到它们。
jars used to be in the lib/ folder, now they're in libs/
If you use lib, you can move your jars from lib to libs, and remove the dependencies from project properties/java build path because Android will now find them automatically.
对我来说,问题是引用的库是使用 java 7 构建的。我通过使用 1.6 JDK 重建解决了这个问题。
For me, the issue was that the referenced library was built using java 7. I solved this by rebuilding using the 1.6 JDK.
有时使用 ART 而不是 Dalvik 运行时时会出现 java.lang.NoClassDefFoundError: 错误。要更改运行时,只需转到开发人员选项 ->选择运行时->达尔维克。
Some time java.lang.NoClassDefFoundError: error appear when use ART instead of Dalvik runtime. To change runtime just go to Developer Option -> Select Runtime -> Dalvik.
如果以上方法都不起作用(就像发生在我身上一样),并且您正在使用 Eclipse 中的另一个项目作为库。
这样做:
右键项目->属性->安卓->图书馆->添加
这对我来说是成功的!
将项目添加为库(项目属性)
If none of the above works (like it happened to me), and you're using as a library another project in Eclipse.
Do so:
Right click project -> Properties -> Android -> Library -> Add
That did it for me!
Adding a project as a library (Project Properties)
活动再次添加到清单中。我在清单中尝试过,然后应用程序工作了。
Activity again added to manifest.I tried in my manifest then app worked.
尝试
我希望它有帮助。
Try go to
I hope it helps.
解决方案:
Solutions: