Eclipse 中出现 java.lang.NoClassDefFoundError,但 Ant 中则不然

发布于 2024-12-20 16:29:58 字数 1918 浏览 7 评论 0原文

我在使用一些 Android 项目和 Eclipse 时遇到了一个非常奇怪的问题。我有许多 Android 项目,它们都使用大部分相同的代码,因此将大量代码移至 Android 库项目中。长期以来,这对于三个应用程序来说效果很好。今天,我也想将该库用于第四个应用程序,但遇到了麻烦。基本上,情况如下:

  • 库项目:包含一组活动以及一些实用程序类
  • 新项目:在某种情况下实例化库项目中的一个活动。

这应该都是微不足道的事情,我什至在其他三个应用程序中做了完全相同的事情,并且具有来自完全相同的库项目的完全相同的活动。在新项目中,我在项目属性中引用了库项目,并将库项目中的活动添加到了 AndroidManifest.xml 中。 Eclipse 现在识别了这一点,并让我引用该活动。一切都很好。

应用程序运行良好,直到我执行从库项目启动活动的操作。应用程序退出,logcat 告诉我:

FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: no.company.application.base.SomeDetailActivity
    at no.company.someotherapplication.SomeListActivity$4.onClick(SomeListActivity.java:466)
    at android.view.View.performClick(View.java:2538)
    at android.view.View$PerformClick.run(View.java:9152)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3691)
    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:847)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
    at dalvik.system.NativeStart.main(Native Method)

这就是乐趣的开始。我知道该库项目与 .apk 捆绑在一起,因为我正在运行该项目的一些实用程序代码。但是,当从 Ant 编译这个确切的项目时,通过使用命令 ant debugant installd,应用程序运行得很好,包括启动 Activity。因此,完全相同的项目设置在 Ant 中运行良好,但在 Eclipse 中却不行。

我尝试了通过谷歌搜索找到的技巧,例如清理所有项目,删除特定项目的 .project 和 .classpath 并重新生成它们。我还通过将清单文件与工作项目中的清单文件进行比较来确保我的清单文件是正确的。无论我做什么,我都会再次遇到这个问题。由于它使用 Ant 编译和运行得很好,在我看来,项目本身没有任何问题,但我确实需要能够通过 Eclipse 编译它。

编辑:我刚刚使用dexdump工具反编译了Eclipse创建的apk中的classes.dex,并且能够确认该类与apk捆绑在一起。我找到了一个具有正确的类描述符和实例字段的类。另一方面,我也反编译了Ant生成的版本,发现了不少差异。太多了,这里不再重复。使用 Ant 和 Eclipse 时生成的文件应该是相同的吗?

I have a quite weird problem with some Android projects and Eclipse. I have a number of Android projects that all use much of the same code, and have therefore moved a lot of code into an Android Library project. This has, for a long time, worked quite well for three apps. Today, I wanted to use the library for a fourth app as well, and ran into trouble. Basically, the situation is as follows:

  • Library project: contains a set of activities as well as some utility classes
  • New project: instantiates one of the activities from the library project in a certain situation.

This should all be trivial stuff, I have even done the exact same thing in three other applications with the exact same activity from the exact same library project. In the new project I have referenced the library project in the project properties, and added the activity from the library project to AndroidManifest.xml. Eclipse now recognizes this, and lets me reference the activity. Everything is good.

The application runs fine until I execute the action that starts the activity from the library project. The application exits, and logcat tells me this:

FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: no.company.application.base.SomeDetailActivity
    at no.company.someotherapplication.SomeListActivity$4.onClick(SomeListActivity.java:466)
    at android.view.View.performClick(View.java:2538)
    at android.view.View$PerformClick.run(View.java:9152)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3691)
    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:847)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
    at dalvik.system.NativeStart.main(Native Method)

This is where the fun starts. I know that the library project is bundled with the .apk, as I have some utility code from this project running. But when compiling this exact project from Ant, by using the commands ant debug and then ant installd, the applications runs just fine, including starting the activity. So, the exact same project setup runs fine with Ant, but not with Eclipse.

I have tried the tricks I have found through googling, such as cleaning all projects, deleting .project and .classpath for the specific projects and regenerating them. I have also ensured that my manifest file is correct by comparing it with the manifest file from the working projects. No matter what I do, I end up with this problem again. As it compiles and runs just fine with Ant, it seems to me that there is nothing wrong with the projects itself, but I really need to be able to compile it through Eclipse as well.

EDIT: I just decompiled classes.dex in the apk created by Eclipse with the dexdump tool, and was able to confirm that the class is bundled with the apk. I found a class with correct class descriptor and instance fields. On the other hand, I also decompiled the version generated by Ant, and got quite a few differences. Too many to repeat here. Is the generated file supposed to be the same when using Ant and Eclipse?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

零崎曲识 2024-12-27 16:29:58

我刚刚遇到了同样的问题。就我而言,我已为 Java 构建路径正确设置了它,但忘记添加库。转到属性 -> Android 然后单击“库”部分中的“添加...”,然后添加库项目。之后它按预期运行。

I just hit this same problem. In my case I had it set properly for the Java build path but forgot to add the library. Go to Properties -> Android then click "Add..." in the Library section then add the library project. After that it ran as expected.

偏爱你一生 2024-12-27 16:29:58

有时,android 插件不能很好地生成资源,从而导致此类问题。尝试以下简单的过程:

  1. 更改布局 xml 之一中的某些 id 并保存 xml(项目中的任何布局都可以)。
  2. 插件编译项目后,将 id 改回并保存。

如果这是问题,那么应该在项目编译后解决它。

Sometimes the android plugin does not generate the resources very well which cause to such issues. Try following simple procedure:

  1. Change some id in one of the layout xml and save the xml (any layout that is in the project will do).
  2. After the project compiled by the plug in change the id back and save.

If this is the problem then it should solve it after the project compiled.

一场春暖 2024-12-27 16:29:58

我解决了将 jar 包含在最终项目的构建路径中的问题。库的构建路径显然还不够。

这很糟糕,但至少它有效。

I solved including the jar in the buildpath of the final project. The buildpath of the library apparently was not enough.

It sucks, but at the least it works.

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