NoClassDefFoundError for code in an Java library o

2018-12-31 16:47发布

I am experiencing an error quite often among my users. The app crashes during startup. When the MainActivity is supposed to be loaded the VM apparently cannot find the class. I cannot figure out why. The architecture of the app is that there is a common project that both my free and pro version are using. Don't know if it is relevant. See the stack trace below. Any thoughts?

java.lang.NoClassDefFoundError: com.android.common.MainActivity
at com.mycompany.myapp.Splash.onCreate(Splash.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
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:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.android.common.MainActivity in loader     dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.android.pro-1.apk]

Edit: Thanks for the comment below, Richard. Now I have changed com.android.Splash to something else. It wasn't the real classname anyway. My bad...!

24条回答
大哥的爱人
2楼-- · 2018-12-31 17:04

I had the same issue, I did the following to fix the problem.

  1. Go to "Properties" of the project.
  2. Select "Java Build Path"
  3. Select "Order and Export" Tab
  4. You should see the selected project's "src" and "gen" paths and dependencies here.
  5. The order how they listed were first "src" and then "gen" path
  6. I switch them, so that "gen" folder is build before the "src"

gen - automated code in project (from dependencies and references)
src - source code in project

There was no need to restart the Eclipse. It just started working.

Honestly I have never tried "Android Tools > Fix Project Properties", sometimes it might be doing the same thing. I do not know, I just did above after seen the error message, thinking something is wrong with the build paths.


Edit


Later on it was not sufficient, I was getting the error again. Then I "checked" all the dependencies listed in that view. Now it works again. So far so good. I will keep this updated if it fails again.

FYI: in my last attempt, I tried "Android Tools > Fix Project Properties", but it didn't work out for me.

查看更多
荒废的爱情
3楼-- · 2018-12-31 17:06

I tried all of the above said solutions but did not worked out for me, Here is what i did to get it done project-> config build path-> order and export-> move dependent project on top

查看更多
还给你的自由
4楼-- · 2018-12-31 17:06

The NoClassDefFoundError description is, from the SO tag:

The Java Error thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found. The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

Or better:

NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time.

from this page. Check it, there are some ways to solve the error. I hope it helps.

查看更多
君临天下
5楼-- · 2018-12-31 17:06

I am guessing that you don't specify javac's target when creating the common library, so javac automatically uses the latest available target, which is likely 1.7 (Java7) or 1.8 (Java8).

It has already been stated that

Android requires compiler compliance level 5.0 or 6.0

dx of Android's build tools < 19.0.0 isn't able to convert Java7 (or higher) bytecode to Dalvik bytecode.

So either use a build tools version >= 19.0.0 or use javac with -target 6, by modifying for example your ant build.xml like this:

<javac
srcdir="src/"
destdir="build/"
target="6"
/>
查看更多
时光乱了年华
6楼-- · 2018-12-31 17:10

For me this problem was related to the API that I was using not being deployed. For example, I used a ZSDK_API.jar as a reference.

To fix the issue I had to right click on the project and select properties. From JAVA BUILD PATH, I had to add the API that was not being deployed. This was weird as the app was throwing MainActivity class not found and in reality the error was caused by the DiscoveryHandler class not being found.

Hopes this helps someone.

查看更多
素衣白纱
7楼-- · 2018-12-31 17:12

I have just figured out something with this error.

Just make sure that the library jar file contains the compiled R class under android.support.v7.appcompat package and copy all the res in the v7 appcompat support "project" in your ANDROID_SDK_HOME/extras/android/support/v7/appcompat folder.

I use Netbeans with the Android plugin and this solved my issue.

查看更多
登录 后发表回答