How to resolve java.lang.NoClassDefFoundError - android

I am using:
org.apache.commons.lang.builder.EqualsBuilder
in a in an Android library project (A) that in turn is used in another library project (B).
This works fine in the Android app that consumes library project B.
However, I have built an Android Test project to unit test the Android app - in particular the library project B classes. When doing so I encounter the following exception:
java.lang.NoClassDefFoundError: org.apache.commons.lang.builder.EqualsBuilder
I have included
commons-lang-2.6.jar
which contains the
org.apache.commons.lang.builder.EqualsBuilder
namespace in library project A build path for export.
I have searched SO for this issue and found a number of posts on the NoClassDefFoundError, but none that have a situation with unit testing.

I found the answer. After reviewing this article, I realized that I needed both the jar from project A and the commons-lang-2.6.jar in the test project. Adding BOTH jars to the build path and export resolved the issue.

Related

Android Library Project Export

I am developing first time Android Library Project that have external dependences of different libraries like retrofit and ormlite-android. The problem i am facing right now is when i use my Android Library Project .aar file inside other project the class not found exception occur when run the code. Please can any one guide me how to fix such issues. Do i have to add external libraries reference inside my dependent app ?

Project Building successfully in Eclipse but not in Intellij

I have an Android project which uses the following library projects along with using the Android Support v4:
ActionBarSherlock
SlideMenuLib
Facebook
Google Play services
My project is working fine in eclipse and im able to launch the porject directly from the IDE but when I try to import and run the same project in IntelliJ (13.0.2), it gives me the following error
Android Dex: [Splash] com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
I know from the error that somehow IntelliJ is picking up the Supportv4 library multiple times from all the three places but any attempt to play around with the Support library imports, the project stops compiling saying com.android.support.v4 is missing in both Eclipse and Intellij.
Fixed the problem. Thanks to the solution found here: Intellij: android-support-v4.jar in included in actionBarSherlock and Project => IllegalArgumentException
I changed the scope of the support library in ActionbarSherlock and SlideMenuLib to 'provided' and that did the trick.
You have problems with your imports probably your project are using the same two libraries (older and new) and do not know with one should be used.
Check your Libs files, lib-project and references.
Try to: build path -> Order and Export, Uncheck the jar that is the same that the imported library uses.

Adding native support prevents my library to build correctly

This is a quite long story, but I'd really appreciate any help on this.
I am building an Android application using eclipse under windows. The application consists in one application (lets call it App) project and two library projects (let's call them libA and libB).
The libB project contains only java code (and android resources). It references the libA project which contains few java classes, lots of C++ classes and java wrappers for these native C++ classes generated using Swig.
In turn the App project references the libB project
I have managed to handle the swig part of the build within ndk-build by adding my own rules in the Android.mk makefile. And all of this was working good.
Lately I wanted to give a try to eclispse managed ndk-builds, this way I would no more have to manually trigger native builds through console.
So I've added "native support" to libA and (after just a little adaptation/configuration) it does build correctly the native libA.so file.
The issue is that ever since I've added native support to my library and updated eclispe's android plugins (ADT, CDT, NDK), it has become impossible to import libA in other projects. It seems the jar file is never created. And there is no option to "remove native support".
The error it's giving me in libB is:
The container 'Android Dependencies' references non existing library 'D:\path\to\libA\bin\libA.jar'
And something similar in App, but one for each library:
The container 'Android Dependencies' references non existing library 'D:\path\to\libA\bin\libA.jar'
The container 'Android Dependencies' references non existing library 'D:\path\to\libB\bin\libB.jar'
So I tried to go back, and revert everything related to native support in the projects. So, I have to call ndk-build manually through console (it works, the shared object is built). But the eclipse part still fails with the same errors. So I'm now suspecting the plugins update v22.0.0.v201305140200--675183.
Does someone have an idea ?
Thanks.
** EDIT #1 **
I've continued investigations, and managed to partially solve my issue by installing the Android SDK's build tools (separate package in SDK manager now).
The result is that I can now build my App project with ant in a console, but eclipse still can't. It seems like eclipse is trying to read /path/to/libA/bin/libA.jar, but what is created is /path/to/libA/bin/classes.jar
One new issue, when building the App project with on command line is that it doesn't import the shared library from libA.
Continuing investigations...
** EDIT #2 **
I managed to fix the problem related to shared object not being imported. Created a jni folder with Application.mk and Android.mk in it. These makefiles serve the sole purpose of importing the lib, no source file there ( that's why I had no *.mk files there)
But eclipse + ADT 22.0.0 still can't build the project (searching for libA.jar and libB.jar instead of classes.jar in each location).
** EDIT #3 **
I continued investigations, and I have removed everything related to eclipse in my project (i.e. .externalToolBuilders, .project, .cproject), re-imported them in my eclipse workspace. Now the build system manages to import the jar library for libA and libB. But obviously it doesn't trigger ndk-build (since they are no more configured with native support).

Use Robotium to test android project with library

I have an android application consisting of a library project that contains the source code. I then have two additional projects, that are used to manage a pro, and lite version of the app.
I have been unable to get robotium to load the class files from either of my proxy apps(pro and lite, which reference the library via a module reference), or the library itself. I have tried a bunch of different class names, and references, but the test project fails below.
public MainTest() {
super("com.joe.app.lib", Main.class);
}
I haven't found any discussion here about testing with library projects in android. Looking for advice.
Error msg
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joe.app.tests/com.job.app.tests.MainTest}: java.lang.ClassNotFoundException: com.joe.tests.MainTest in loader dalvik.system.PathClassLoader[/data/app/com.joe.app.tests-1.apk]
While not specific to Robotium, this post may help you using external libraries in your test project.
In case you haven't found a solution yet or others are getting to this question due to similar errors after the last the adt update.
In Project Properties of your Test Project go to item Java Build Path and select tab Order and Export. There you'll have to check the project you're testing against so it's properly exported.
For latest ADT you also have to have the Android Private Libraries exported in your projects.

Self-contained test library project cannot find the library classes

According to this SDK guide, unit-testing a Library project can be achieved by creating a standard application project, reference the Library project and then instrument the application for unit testing.
However, when I do this and launch the test application I get the message
No tests found with test runner 'JUnit 3'.
I'm using Eclipse and the Android ADT plugin, all latest versions.
Note: the projects compile just fine. The test project also installs fine to the emulator. But in the console I can see that it looks for <library>.apk, which of course doesn't exist since I'm compiling this as a library into the test project.
Anyone got this to work? And if so, what is the trickery here?
Update: after discovering and fixing a problem, which was actually including the test classes (!), the test runner now can find all tests. But, all the tests fail with the following exceptions:
java.lang.NoClassDefFoundError: <nameOfClassInLibraryProject>
nameOfClassInLibraryProject are classes defined in the library project. These classes should be compiled into the test project, and indeed, everything compiles just fine. But when running the test project, the runtime doesn't seem to find the library classes.
After much fiddling and wasted time in Eclipse I have managed to get Android Library projects to work.
According to the Working with Library Projects article:
Instead, you must compile the library indirectly, by referencing the library from a dependent application's build path, then building that application.
The problem was that I interpreted this to mean that the library project should be added to the Projects tab in Java Build Path. Doing this makes the test project compile since the library code is obviously available to the compiler. But since the library is not compiled into a .jar or .apk in itself, the library classes are never deployed to the device.
The solution is to not add the library project to Projects, rather on the Source tab, add the library /src folder using the Link Source... button. And yes, it is the library src folder, not the library project root, that must be linked into the test project.

Categories

Resources