简体   繁体   中英

java.lang.NoClassDefFoundError in Eclipse, but not with Ant

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?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM