Android 外部库项目出现 NoClassDefFoundError

发布于 2024-12-13 20:46:45 字数 394 浏览 1 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

桃扇骨 2024-12-20 20:46:45

官方API此处已经明确说明:

库项目可以包含 JAR 库

您可以开发一个本身包含 JAR 库的库项目,但是您需要手动编辑依赖应用程序项目的构建路径并添加 JAR 文件的路径

必须手动将 jar lib 添加到依赖应用程序项目的构建路径,而不仅仅是库项目构建路径本身。

从 SDK r17 更新:

现在由 ADT 自动处理,查看 ADT 17.0.0 版本的新功能 此处

添加了自动设置 JAR 依赖项的功能。 /libs 文件夹中的任何 .jar 文件都会添加到构建配置中(类似于 Ant 构建系统的工作方式)。此外,库项目所需的 .jar 文件也会自动添加到依赖于这些库项目的项目中。 (更多信息

It has been clearly stated in offcial API here:

A library project can include a JAR library

You can develop a library project that itself includes a JAR library, however you need to manually edit the dependent application project's build path and add a path to the JAR file

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:

Added feature to automatically setup JAR dependencies. Any .jar files in the /libs folder are added to the build configuration (similar to how the Ant build system works). Also, .jar files needed by library projects are also automatically added to projects that depend on those library projects. (more info)

许仙没带伞 2024-12-20 20:46:45

对于那些遵循这些步骤(甚至检查“订购和导出”中的项目)但在 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

維他命╮ 2024-12-20 20:46:45

转到项目属性 ->构建路径-> libraies

如果您看到这样的 jar 文件,

snmp4j.jar - e:\software\jars

这可能是您的问题

在项目中添加 libs 文件夹并复制该文件夹中的 jar 文件。右键单击 jar 文件并转到 build path ->添加到构建路径。然后你就可以看到你的罐子,因为

snmp4j.jar  - project_name/libs 

它对我有用。

Go to project properties -> build path-> libraies

If you see your jar files like this

snmp4j.jar - e:\software\jars

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 as

snmp4j.jar  - project_name/libs 

Its worked for me.

A君 2024-12-20 20:46:45

我有两个项目使用同一个库:一个工作正常,另一个因 java.lang.NoClassDefFoundError 崩溃。
在没有其他帮助之后,我查看了项目根目录中的文件 project.properties
工作项目有 android.library.reference 行(下面的最后一行),崩溃的项目没有:

# Project target.
target=android-17
android.library.reference.1=../my-library-project

我手动添加了该行,它开始工作!
(是的,我确实尝试了(项目)属性--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:

# Project target.
target=android-17
android.library.reference.1=../my-library-project

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.

来世叙缘 2024-12-20 20:46:45

当我升级到 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

旧时浪漫 2024-12-20 20:46:45

我遇到了类似的问题,但这里的解决方案都没有解决它。

简短版本:该 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 the MANIFEST.MF file for the Build-Jdk: parameter to see what Java version actually compiled your JAR.

第几種人 2024-12-20 20:46:45

另一件需要注意的事情是库包名称。

如果您使用 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文