NoClassDefFoundError for code in an Java library on Android
Asked Answered
T

24

78

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...!

Transistorize answered 30/12, 2011 at 10:47 Comment(3)
Why would you ever try to create a class in a package that already exists? (com.android.*). That's bad form, try changing the package around.Paddock
Does this only happen some of the times? or does it happen all the time when you build locally?Conversation
It happened only for the app launched to Play. Haven't seen it myself, so I have just seen the stack trace retrieved on Crittercism.Transistorize
L
125

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.

Lablab answered 3/10, 2012 at 5:19 Comment(6)
your edited ans worked for me as well...been pulling my hair for 2-3 hrs due to ClassDefNotFoundErrorFaxan
I am leaving a comment that someone may find one day. My problem was referencing a class from a library project that should not have been used. I found the error/solution in the "Libraries" tab instead. I don't know if it will ever help anyone, but now its on the internet.Intercessor
Your suggestion to switch "gen" with "src" worked, everything else failed! Thanks!Radiative
That solution didn't work for me. However, moving an included JAR file to the top did. This was for AFreeChart. Generically, changing the build order is the part of this answer that may solve this problem. The NoClassDefFoundError is a hard-to-trace-down problem. As an aside: I often delete gen to cause a rebuild. In that case gen gets moved to the bottom. That has never been a problem for me.Vapor
here is one more hit n try which worked for me .....followed mentioned 6 steps, then unselected my own libs (jackson ect) in "Order and Export" TabHainan
what kind of thing is this , that caused me quite alot time but , @Sabo saved me an equal or bigger time .THANKS SaboOzenfant
T
17

I'm currently using SDK 20.0.3 and none of the previous solutions worked for me.

I was able to get things to work by

  • Right clicking the Android project
  • Then selecting Build Path -> Link Source to bring up the Link Source dialog.
  • Then I specified the 'src' folder of the Java project and gave it a name other than the default of 'src' (e.g. 'src2').
  • You can now right-click -> delete the new 'src2' folder.

This brought this created Java class files that were compiled for the Dalvik VM instead of the Java VM when the Android project was built. This allowed the Java class files in the Android Library jar file to go thru dexing when the Android project .apk was created. Now when the app was run the Java project classes were found instead of a Java.lang.NoClassDefFoundError being thrown.

If you want to use a jar file instead of linking the source, then you will need to create a Library Android project. (An Android Project with 'is library' checked in Properties -> Android.) You will then need to either link the source of the Java Project to the Android Library project as described above or copy the source files from the 'src' folder of the Java Project to the 'src' folder of the Android Library project. Build the Android Library project. Then you will be able copy the Android Project jar file that was created into the 'libs' folder of the Android folder because the class files in it were compiled for the Davlik VM instead of the Java VM when the Android project was built. This allows the Java class files in the Android Library jar file to go thru dexing when the Android project .apk is created. Now when the app is run the Java project classes will be found instead of a Java.lang.NoClassDefFoundError being thrown.

Testee answered 30/12, 2011 at 10:47 Comment(0)
S
17

Try going to Project -> Properties -> Java Build Path -> Order & Export and ensure Android Private Libraries are checked for your project and for all other library projects you are using. i got the solution by following below link NoClassDefFoundError Android Project?

Sticker answered 21/6, 2013 at 6:13 Comment(0)
R
8

Just in case it helps someone, I faced the same issue. My jar file was under the libs directory and the jar was added to the build path. Still it was throwing that exception.

I just cleaned the project and it worked for me.

Eclipse -> Project -> Clean -> Select the project and clean

Renaldorenard answered 28/3, 2013 at 10:13 Comment(0)
U
7

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

Unaccomplished answered 4/7, 2013 at 5:46 Comment(0)
R
6

This error is also generated when you make an app that uses the Google API (such as Maps) but run it on a device that targets the Android API.

Ringster answered 13/8, 2012 at 23:50 Comment(1)
this finally fixed my problem. somehow the fix project properties function doesn't change the target. i fixed this by updating my google apiOfficer
A
5

I encountered this NoClassDefFoundError when I used java.nio.charset.StandardCharsets.

The reason is that StandardCharsets has not existed until JRE 1.7. If I make the java compile version set to 1.7, Eclipse complained that "Android requires compiler compliance level 5.0 or 6.0". So, I fixed it by right-click the project name->Android Tools->Fix Project Properties. It is compiled with JRE1.6.

However, because StandardCharsets has not existed until 1.7. It reported NoClassDefFoundError when I ran it.

I has not come to realize this until after trying a lot of other methods including reinstalling JDK. The real reason is clearly told by the meaning of NoClassDefFoundError: The class cannot be found at run time although it passed compilation.

General conclusion is that as long as Android does not work with JRE 1.7, if you use any new feature provided since 1.7, you will encounter this error.

My solution is that I copied those source code into my code!

Ageratum answered 21/4, 2013 at 10:20 Comment(1)
+1 I tried using a .jar file that I had compiled with Java 1.7. After recompiling the jar with Java 1.6, it worked fine.Revival
P
4

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.

Portative answered 30/12, 2011 at 10:54 Comment(3)
Thank you Alberto! Your post helped me to understand the problem. Now I am curious about how Android is handling the classpath. Currently reading this issue: #1095822Transistorize
@Kjoendo You are welcome. I will read the link about the issue. :)Portative
I've just had this for a ConcurrentSkipListMap; I think it's because my application was run on a Froyo device, and that class is absent in Froyo; however, AdMob requires me to target Ice Cream Sandwich so I hadn't noticed this!Lasser
R
4

In my case, I was trying to add a normal java class (from a normal java project) compiled with jre 1.7 to an android app project compiled with jre 1.7.

The solution was to recompile that normal java class with jre 1.6 and add references to the android app project (compiled with jre 1.6 also) as usual (in tab order and export be sure to check the class, project, etc).

The same process, when using an android library to reference external normal java classes.

Don't know what's wrong with jre 1.7, when compiling normal java classes from a normal java project and try to reference them in android app or android library projects.

If you don't use normal java classes (from a normal java project) you don't need to downgrade to jre 1.6.

Roaster answered 26/10, 2013 at 13:19 Comment(0)
P
3

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.

Prot answered 27/11, 2012 at 15:24 Comment(0)
V
2

I met NoClassDefFoundError for a class that exists in my project (not a library class). The class exists but i got NoClassDefFoundError. In my case, the problem was multidex support. The problem and solution is here: Android Multidex and support libraries

You get this error for Android versions lower than 5.0.

Vicinity answered 27/6, 2015 at 13:5 Comment(0)
P
2

On Android Studio:

1) Add multiDexEnabled = true in your default Config

2) Add compile com.android.support:multidex:1.0.0 in your dependencies

3) Application class extend MultiDexApplication instead of just Application

Portillo answered 5/5, 2016 at 3:22 Comment(0)
M
1

Other idea. For example, you have class derived from "android.support.v4.app.Fragment".

However you made a mistake and inherited it from "android.app.Fragment". Then you will have this error on the Android 2 devices.

Memento answered 25/6, 2013 at 9:38 Comment(0)
G
1

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.

Gerstein answered 30/7, 2013 at 9:16 Comment(0)
W
1

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"
/>
Willmert answered 10/12, 2013 at 14:39 Comment(0)
H
0

I fixed this issue by adding library project path in project.propertied manually. some how eclipse did not added this entry automaticvally along with "add project". so the point where app was trying to refer any componenrt inside lib project it was crashing .

you also can try the same thing . app dependecy in projec.properties like

android.library.reference.1=....\android-sdks\extras\google\google_play_services\libproject/google-play-services_lib

and run .

Hainan answered 12/3, 2013 at 11:3 Comment(0)
R
0

jars used to be in the lib/ folder, now they're in libs/ If you use lib, you can move your jars from lib to libs, and remove the dependencies from project properties/java build path because Android will now find them automatically.

Reenareenforce answered 6/11, 2013 at 12:40 Comment(0)
P
0

For me, the issue was that the referenced library was built using java 7. I solved this by rebuilding using the 1.6 JDK.

Pharmacopsychosis answered 19/1, 2014 at 11:51 Comment(0)
A
0

Some time java.lang.NoClassDefFoundError: error appear when use ART instead of Dalvik runtime. To change runtime just go to Developer Option -> Select Runtime -> Dalvik.

Aphid answered 1/5, 2014 at 13:25 Comment(0)
W
0

If none of the above works (like it happened to me), and you're using as a library another project in Eclipse.

Do so: Right click project -> Properties -> Android -> Library -> Add

That did it for me! Adding a project as a library (Project Properties)

Weidner answered 10/5, 2014 at 8:35 Comment(0)
B
0

Activity again added to manifest.I tried in my manifest then app worked.

 <activity
        android:name="com.xxx.xxx.MainActivity"
Burgett answered 18/11, 2014 at 14:23 Comment(0)
C
0

Try go to

  • Right click project
  • Java build path
  • Source
  • right side -> Add folder button
  • selected libs folder and OK

I hope it helps.

Caning answered 25/3, 2015 at 6:1 Comment(0)
P
0

Solutions:

  1. List item
  2. Check Exports Order
  3. Enable Multi Dex
  4. Check api level of views in layout. I faced same problem with searchView. I have check api level while adding searchview but added implements SearchView.OnQueryTextListener to class file.
Propitiatory answered 14/10, 2016 at 13:38 Comment(0)
O
-4

1)In Manifest file mention your activity name and action for it and also category . 2)In your Activity mention your starting contentview and mention your view id's in the activity.

Overijssel answered 30/12, 2011 at 11:55 Comment(1)
Somebody has erroneously flagged this as not an answer. This is an attempt at an answer, and so qualifies as an answer. If you don't like the answer, the proper action is to down vote it (as 5 other people already have), not flag it. See Your Answer is in Another Castle - When an Answer is not an Answer: meta.stackexchange.com/questions/225370/…Deutero

© 2022 - 2024 — McMap. All rights reserved.