Question

There are tons of questions about this, but none gives a clear solution to this problem (is it really so difficult?)

I'm trying to add a jar to my Android project. Since r17, Google says we should add the jars to /libs folder, and that the andriod tools will take care of the rest.

I have included a very big library, and it works good, except of one class, so I decided to try and make my own jar.

I created a new java project, and added a simple Test class. Then I exported it as a jar and manually copied the jar into the /libs folder. I cleaned and rebuilt my android app without problems, but at runtime, referencing the Test class, i get the following errors:

java.lang.NoClassDefFoundError: mytestpackage.Test

When I include the source of the jar into my project directly, it works (of course).

So what am I doing wrong?

Was it helpful?

Solution 2

Well, after struggling with it for a day now, I found out that I had the compiler level set to 1.7 in the library project, so when I exported the jar, it was exported with a higher compiling level than what android supports (1.6).

So the solution, in the project of the jar to export: Properties -> Java Compiler -> Enable specific settings -> Compiler compilance level -> 1.6

That fixed it for me, I hope it's useful.

OTHER TIPS

Since r17, Google says we should add the jars to /libs folder, and that the andriod tools will take care of the rest.

Correct.

So what am I doing wrong?

If I had to guess, in addition to copying the JAR to libs/, you also did "Add to Build Path" in Eclipse (per the other answer). Not only is that not necessary, but reports indicate that doing this somehow suppresses actually packaging the JAR from libs/ into the APK. If indeed you did this, undo the manual build path change, and see if that helps.

You need to update your .classpath. In Eclipse, mark the library (put in /libs) and Right click->Build Path->Add to build path

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top