Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError on external library project for Android

I use eclipse for Google Android development.

I've created a library project ([x] Is Library in the Android-settings), which includes an external jar-file (Referenced Libraries). This library project are referenced in another Project (the actual project which will use the library project). This is done by add the project under the Android-settings.

the source compiles but if I want to execute it on the device, I get the NoClassDefFoundError for a class which is inside the jar-file which is included in the library project.

Edit: The jar-file ist added to the exported entries ([x] my.jar on the Order and Export-Tab from the library project)

Is there a clean way to get this working?

like image 544
f00860 Avatar asked Nov 03 '11 12:11

f00860


4 Answers

It has been clearly stated in offcial API here:

A library project can include a JAR library

You can develop a library project that itself includes a JAR library, however you need to manually edit the dependent application project's build path and add a path to the JAR file

The jar lib must be manually added to the dependent application project's build path, not only the library project build path itself.

Update from SDK r17:

This is automatically handled by ADT now, check out new feature for ADT 17.0.0 release here:

Added feature to automatically setup JAR dependencies. Any .jar files in the /libs folder are added to the build configuration (similar to how the Ant build system works). Also, .jar files needed by library projects are also automatically added to projects that depend on those library projects. (more info)

like image 180
yorkw Avatar answered Nov 11 '22 06:11

yorkw


For those who followed the steps(even check the projects in "Order and Export") and still have the java.lang.ClassNotFoundException in the API 17, the final step is to check that your compiler does not run with Java 1.7. If is 1.7 then you should change it to 1.6 for all your projects. After that it will ask to rebuild all the projects and successfully ran on my phone :)

To change the java compile version in eclipse, this is located in: Project properties > Java Compiler > Compiler Compliance level: 1.6

like image 23
Gareve Avatar answered Nov 11 '22 07:11

Gareve


Go to project properties -> build path-> libraies

If you see your jar files like this

snmp4j.jar - e:\software\jars

Its may your problem

Add libs folder in your project and copy jar file in that folder. Right click jar file and go build path -> add to build path. Then you can see your jar as

snmp4j.jar  - project_name/libs 

Its worked for me.

like image 2
Senthil Avatar answered Nov 11 '22 05:11

Senthil


I had two projects using the same library: one working, the other one crashing with java.lang.NoClassDefFoundError. After nothing else helped me, I looked into the file project.properties in the root directory of my project. The working project had the android.library.reference line (the last line below), the crashing one did not:

# Project target.
target=android-17
android.library.reference.1=../my-library-project

I manually added the line and it started working! (Yes, I did try both (project) properties -- java build path -- projects and (project) properties -- java build path -- order and exports -- nothing helped.)

PS By the way, "project properties" has also the "project references" tab. No idea if it would work.

like image 2
18446744073709551615 Avatar answered Nov 11 '22 07:11

18446744073709551615