Android 外部库项目出现 NoClassDefFoundError
我使用 eclipse 进行 Google Android 开发。
我创建了一个库项目(Android 设置中的[x] Is Library
),其中包含一个外部 jar 文件(引用库)。该库项目在另一个项目(将使用该库项目的实际项目)中引用。这是通过在 Android 设置下添加项目来完成的。
源代码可以编译,但如果我想在设备上执行它,我会得到一个类的 NoClassDefFoundError 错误,该类位于库项目中包含的 jar 文件内。
编辑: jar 文件已添加到库中的Order and Export
-选项卡上的导出条目([x] my.jar
)项目)
有没有一种干净的方法来让它工作?
I use eclipse for Google Android development.
I've created a library project ([x] Is Library
in the Android-settings), which includes an external jar-file (Referenced Libraries). This library project are referenced in another Project (the actual project which will use the library project). This is done by add the project under the Android-settings.
the source compiles but if I want to execute it on the device, I get the NoClassDefFoundError for a class which is inside the jar-file which is included in the library project.
Edit: The jar-file ist added to the exported entries ([x] my.jar
on the Order and Export
-Tab from the library project)
Is there a clean way to get this working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
官方API此处已经明确说明:
必须手动将 jar lib 添加到依赖应用程序项目的构建路径,而不仅仅是库项目构建路径本身。
从 SDK r17 更新:
现在由 ADT 自动处理,查看 ADT 17.0.0 版本的新功能 此处:
It has been clearly stated in offcial API here:
The jar lib must be manually added to the dependent application project's build path, not only the library project build path itself.
Update from SDK r17:
This is automatically handled by ADT now, check out new feature for ADT 17.0.0 release here:
对于那些遵循这些步骤(甚至检查“订购和导出”中的项目)但在 API 17 中仍然出现 java.lang.ClassNotFoundException 的用户,最后一步是检查您的编译器是否未在 Java 1.7 上运行。如果是 1.7,那么您应该将所有项目的它更改为 1.6。之后它会要求重建所有项目并在我的手机上成功运行:)
要更改 eclipse 中的 java 编译版本,这位于:项目属性 > Java编译器>编译器合规级别:1.6
For those who followed the steps(even check the projects in "Order and Export") and still have the java.lang.ClassNotFoundException in the API 17, the final step is to check that your compiler does not run with Java 1.7. If is 1.7 then you should change it to 1.6 for all your projects. After that it will ask to rebuild all the projects and successfully ran on my phone :)
To change the java compile version in eclipse, this is located in: Project properties > Java Compiler > Compiler Compliance level: 1.6
转到
项目属性 ->构建路径-> libraies
如果您看到这样的 jar 文件,
这可能是您的问题
在项目中添加 libs 文件夹并复制该文件夹中的 jar 文件。右键单击 jar 文件并转到
build path ->添加到构建路径
。然后你就可以看到你的罐子,因为它对我有用。
Go to
project properties -> build path-> libraies
If you see your jar files like this
Its may your problem
Add libs folder in your project and copy jar file in that folder. Right click jar file and go
build path -> add to build path
. Then you can see your jar asIts worked for me.
我有两个项目使用同一个库:一个工作正常,另一个因
java.lang.NoClassDefFoundError
崩溃。在没有其他帮助之后,我查看了项目根目录中的文件
project.properties
。工作项目有
android.library.reference
行(下面的最后一行),崩溃的项目没有:我手动添加了该行,它开始工作!
(是的,我确实尝试了(项目)属性--java构建路径--项目和(项目)属性--java构建路径--顺序和导出--没有任何帮助。)
PS 顺便说一下,“项目属性”还有“项目引用”选项卡。不知道它是否会起作用。
I had two projects using the same library: one working, the other one crashing with
java.lang.NoClassDefFoundError
.After nothing else helped me, I looked into the file
project.properties
in the root directory of my project.The working project had the
android.library.reference
line (the last line below), the crashing one did not:I manually added the line and it started working!
(Yes, I did try both (project) properties -- java build path -- projects and (project) properties -- java build path -- order and exports -- nothing helped.)
PS By the way, "project properties" has also the "project references" tab. No idea if it would work.
当我升级到 ADT17 时,我遇到了一个小问题,我的库没有正确导入。事实证明,这是因为我的库作为依赖项从我的 lib 文件夹而不是库链接!
看来库从现在开始必须位于 libs 文件夹中
I had a minor issue when I upgraded to ADT17 where my libs weren't being imported properly. Turns out this is because my librarys were being linked as dependancies from my lib folder not libs!
Seems librarys have to be in the libs folder from now
我遇到了类似的问题,但这里的解决方案都没有解决它。
简短版本:该 JAR 是用 Java
1.7
编译的,Dalvik 无法找到该 JAR。我们构建了一个内部 JAR,并使用 Maven 在后端和移动客户端之间共享。问题最终是 JAR 是用 Java 1.7 编译的。 Android dalvik 仅支持 Java 1.5 和 1.6。这就是为什么 Eclipse 中一切正常,因为此时它还没有编译为 dalvik。
我们甚至指定了目标和源Maven 编译器插件中的版本 使用 Java 1.6 进行编译。这不起作用,因为构建机器上只安装了 JDK 1.7。 Maven 页面底部的一个小注释给了我们提示:
注意:仅仅设置 target 选项并不能保证你的代码实际上在指定版本的 JRE 上运行。
看看你是否有也有这个问题,请将您的
*.jar
文件重命名为*.zip
将其解压,然后在MANIFEST.MF
文件中查找 < code>Build-Jdk: 参数查看实际编译您的 JAR 的 Java 版本。I had a similar problem and non of the solutions out here fixed it.
Short version: the JAR was compiled in Java
1.7
and could not be found by Dalvik.We build an internal JAR that we share across backend and mobile clients with Maven. The problem ended up being that the JAR was compiled with Java 1.7. Androids dalvik only supports Java 1.5 and 1.6. This is why everything works fine in Eclipse as it's not compiled to dalvik at this point.
We even specified the target and source version in the Maven compiler plugin to compile with Java 1.6. This did not work because only JDK 1.7 was installed on the build machine. A small note at the bottom of the Maven page gave us the hint:
Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version.
To see if you have this problem as well, rename your
*.jar
file to*.zip
unpack it, and look in theMANIFEST.MF
file for theBuild-Jdk:
parameter to see what Java version actually compiled your JAR.另一件需要注意的事情是库包名称。
如果您使用 ADT21 并且碰巧有具有相同包名的库,则编译期间会出现错误,但它仍然会在 Eclipse 中编译并运行,从而为您提供缺少某些资源类的 APK。因此,当您运行应用程序时会发生崩溃。
如果您使用 ANT 编译它,您会看到编译错误,指出两个或多个库使用相同的包名称。
要解决此问题,请使用 Android Tools -> 重命名您的库项目。重命名应用程序包。然后一切都会恢复正常。
我花了几乎一整天的时间才弄清楚这个问题......
Another thing to pay attention to is library package names.
If you are using ADT21 and you happen to have libraries that have the same package name, there will be error during compile but it will still compile and run in Eclipse, giving you an APK that is missing some of the resource classes. Hence the crash when you run the app.
If you compile it with ANT then you can see the compile error that says two or more libraries use the same package name.
To fix this, rename your library project by using Android Tools -> Rename Application Package. Then everything will go back to normal.
It took me almost entire day to figure this out...