Link to home
Start Free TrialLog in
Avatar of errang
errangFlag for Afghanistan

asked on

java.lang.NoClassDefFoundError in Android project

Hey,

       I'm trying to link an external Java program in my Android application; and I'm getting a java.lang.NoClassDefFoundError.

I don't understand why I'm getting this error; mainly because I've included the Java code in the Build Path for the Android project.

I tried searching online and found this post on stackoverflow: http://stackoverflow.com/questions/2247998/noclassdeffounderror-eclipse-and-android

The error is almost identical; but I do not have a folder called lib that I can change to libs... and I tried the other fixes of including external jars; and also tried creating a new project and starting from scratch... but that didn't help either.

Here is the jar file I generated: https://www.dropbox.com/s/kq0fvicfzz8tn0h/GMailReader.jar

Appreciate any help on this!
Avatar of mccarl
mccarl
Flag of Australia image

and I tried the other fixes of including external jars
I am only asking this because you didn't specifically mention it, did you also ensure that you ticked the checkbox next to your external jar on the 'Order and Exports' tab?

but I do not have a folder called lib that I can change to libs
One other thing I would try, you say you don't have a folder called 'lib' (or 'libs) but can you try creating a 'libs' folder, in either the folder that the jar file is currently in, or within your project structure (possibly under 'assests' if you have that), put the jar in the new 'libs' folder and then include in your build path from there. (And tick the checkbox on 'Order and Exports' as above)
Avatar of errang

ASKER

>>I am only asking this because you didn't specifically mention it, did you also ensure that you ticked the checkbox next to your external jar on the 'Order and Exports' tab?

Yes, I did that.

>>One other thing I would try, you say you don't have a folder called 'lib' (or 'libs) but can you try creating a 'libs' folder, in either the folder that the jar file is currently in, or within your project structure (possibly under 'assests' if you have that), put the jar in the new 'libs' folder and then include in your build path from there. (And tick the checkbox on 'Order and Exports' as above)

I did have a 'libs' folder; it contained some android support stuff.

I did notice that the 'META-INF' folder inside the jar file didn't contain any real information.
Avatar of errang

ASKER

I think the problem is with the MANIFEST.MF file generated by Eclipse...

This is all that's there in that file:

Manifest-Version: 1.0

Open in new window

Are you referring to the MANIFEST.MF file that is packaged in to your .APK file? Or the one in the external .JAR that you are trying to include?

Either way, I don't think it is necessary to have anything particularly meaningful in these files for it all to work in Android.


You say that you DID have a libs folder... Did you try putting the JAR in there?
Avatar of errang

ASKER

>>  Are you referring to the MANIFEST.MF file that is packaged in to your .APK file? Or the one in the external .JAR that you are trying to include?

The one in the external .JAR file.

>>  You say that you DID have a libs folder... Did you try putting the JAR in there?

Yes... Eclipse won't let me.

Here are the screenshots.
lib-directory.png
Avatar of errang

ASKER

Sorry... meant to add another screenshot; here's a screenshot of Eclipse recognizing that the library exists.
Library-exists.png
here's a screenshot of Eclipse recognizing that the library exists
Yeah, there are (at least) TWO parts to using external libraries with Java based apps.

1) You include libraries in the Java Build Path, so that Eclipse knows about them and does code-completion, etc (as your screenshot shows) and so that the compiler, javac, can compile your code into class files without throwing errors due to unknown classes, methods, etc. All this is known as 'compile-time' activities.

2) The libraries need to be present, along with your code when you are running your app, so that the actual methods, etc can be called while passing values and receiving return values. All this is known as 'run-time' activities.

SO... as mentioned the screenshot shows that the library is being recognised at compile-time since the code-completion is working and (i presume) you are able to compile your .java files into .class files. However, at run-time, the class must not be present as you are getting the error that you are seeing. The bit in the middle of this is the packager/deployer and in the case of android I believe they call it 'dx'. dx is what takes all your compiled .class files for the code that you have written, and hopefully the .class files in your external .jar libraries and does a second, different, compilation on them to produce a classes.dex file that contains all the code (both yours and external libraries) in the format needed to be run on Android. (It then also takes this and other various files/resources and creates the .apk file that is the final package of your app).

So while your screenshot shows that Eclipse knows about your external library, that doesn't necessarily mean the 'dx' knows about it too, or that it is correctly compiling it into the classes.dex file, or .apk file. Perhaps 'dx' only looks in the 'libs' folder and doesn't care about other entries in the Java Build Path of Eclipse. Perhaps 'dx' is able to find your library but some operation it is doing on it (such as the second compile that it does) is not working correctly. Since this compile that 'dx' does is changing the code from Java Bytecode (.class file) format to DEX format, maybe there is some code in that library that just CAN'T be recompiled in that way.


So what I would suggest, firstly, see if you can copy your external library .jar file into that 'libs' folder directly using the filesystem, since Eclipse seems to not allow you to do that! Then do a refresh (F5) on your project and then Project->Clean and see if that helps. Secondly, I would say to see if there are any error/warning messages from when 'dx' runs (when the project is building) that refer to it not being able to compile the external library classes into the classes.dex file. You may have to see if you can turn up the amount of logging 'dx' does whilst it is running. Thirdly, if you want to test if 'dx' is correctly seeing external library .jar files, you could try just creating a very small .jar file yourself that has a simple class in it with one method that does something trivial, such as add two ints together and returns the result, and then use this simple class in your android app. You would expect there is no problems in the compiling something so simple into the classes.dex file, so if it works fine, then you know that it is finding external library jars ok and that the problem must be related to the nature of the code in GMailReader.jar, otherwise if it gives you a similar NoClassDefFound exception for this class, then the problem is still that 'dx' is not finding your .jar files properly.
Avatar of errang

ASKER

>> So what I would suggest, firstly, see if you can copy your external library .jar file into that 'libs' folder directly using the filesystem

This didn't work either; still getting the same classdefnotfound error.

>>  Thirdly, if you want to test if 'dx' is correctly seeing external library .jar files, you could try just creating a very small .jar file yourself that has a simple class in it with one method that does something trivial, such as add two ints together and returns the result, and then use this simple class in your android app.

I actually did try this.. I made a simple class that had one public function that would return the string "Hello World!"; that didn't work either... so maybe the '.dex' file is not compiling these at runtime? How would I even fix this? Isn't this supposed to be taken care of by the Android SDK/Eclipse? The only thing I saw was "Order and Export" in Java Build Path.
GMailReader-in-libs.png
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of errang

ASKER

Just going to convert the code to PHP and use a webview.... This is way too painful.