I am somewhat new to Android and have been struggling for hours on how to include the Hamcrest matchers in my test project. The code looks like it should run in Eclipse, no indicated errors. But when I run the test, the Hamcrest classes are not loading. I get "NoClassDefFoundError: org.hamcrest.Matchers".
I've spent the last 3 hours looking through the forums figuring that someone has encountered this before, but with no luck. I must be doing something stupid as the IDE can see the classes, but the runtime doesn't.
Here is the test source:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.hamcrest.Matcher;
import android.test.InstrumentationTestCase;
public class AppDescLoaderTest extends InstrumentationTestCase {
public void testHamcrest() {
assertThat(3, equalTo(3));
}
}
I've tried adding the hamcrest-all-1.2.jar as a Library and as an external jar. Same result both ways, code looks fine in IDE but fails to load classes at runtime.
My theory is that the hamcrest.jar is not going with the .apk file and that is why its classes are not being loaded. If that is true, how do I get it to be pushed over to the android along with the .apk file?
Assuming you are on the current R17 tools, then...
I've tried adding the hamcrest-all-1.2.jar as a Library and as an external jar. Same result both ways, code looks fine in IDE but fails to load classes at runtime.
Step #1: Undo all of that.
Step #2: Create a libs/
directory in the root of your project.
Step #3: Put your JAR in libs/
.
And you are done. This used to be the recipe just for command-line builds, but it is now the recipe for Eclipse-based builds as well.