Eclipse 中的 java.lang.NoClassDefFoundError,但不是 Ant [英] java.lang.NoClassDefFoundError in Eclipse, but not with Ant

查看:23
本文介绍了Eclipse 中的 java.lang.NoClassDefFoundError,但不是 Ant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

  • 库项目:包含一组活动以及一些实用程序类
  • 新建项目:在特定情况下实例化库项目中的一项活动.

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

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.

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

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)

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

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.

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

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

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?

推荐答案

我刚刚遇到了同样的问题.在我的情况下,我为 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.

这篇关于Eclipse 中的 java.lang.NoClassDefFoundError,但不是 Ant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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