Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Instrumentation test does not find classes from library project

So I'm getting a NoSuchMethodError when running my Activity/instrumentation tests from Android Studio, on the code line which tries to call a method in a library module from the unit test.

So this is my test:

public class MainActivityTest extends ActivityInstrumentationTestCase2 {
    public void testMainActivity() {
        final MainActivity target = (MainActivity) getActivity();
        MyLibarary.someStaticMethod(); // yields java.lang.NoSuchMethodError
}

What's the deal here? I've defined my library as a "compile"-dependency in build.gradle, and it's compiling just fine. The library is also invoked from the main app classes without problems. It's only when I call it from the tests it fails. My app and my tests are in the same module.

I've tried running the clean task, the assembleDebug and assembleDebugTest tasks manually. No avail.

Project structure:

Root
 |---MyApp 
 |     |---src/main/...
 |     |---src/androidTest/...
 |----MyLibrary

Running Android Studio v1.0.2 Gradle build tools v1.0.0 Running as an "Android Test" on module "MyApp" from the Run/Debug configurations of AS with default Instrumentation test runner.

like image 838
Nilzor Avatar asked Jan 30 '15 14:01

Nilzor


2 Answers

Ok this one was a bit tricky. Keyword: proguard minify. Since the newly implemented method so far only was used by the instrumentation test, proguard didn't pick up on its usage and therefore removed it from the DEX in the proguardDebugTest build step.

Solution: Either disable minification in the debug build (In gradle: android.buildTypes.debug.minifyEnabled false), or use the method in the main app.

like image 139
Nilzor Avatar answered Nov 15 '22 01:11

Nilzor


Not really up-to-date with Gradle. But i think we are supposed to specify the testCompile or the androidTestCompile dependency as well in the build.gradle if trying to write instrumentation tests.

Helpful Links:

http://gradle.org/docs/current/userguide/java_plugin.html

Specifying test dependencies with the Gradle Android build system

Hope this helps

like image 41
user2511882 Avatar answered Nov 14 '22 23:11

user2511882