Nilzor
Nilzor

Reputation: 18583

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.

Upvotes: 5

Views: 3940

Answers (2)

Nilzor
Nilzor

Reputation: 18583

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.

Upvotes: 8

user2511882
user2511882

Reputation: 9152

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

Upvotes: 1

Related Questions