5

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.

6
  • 1
    In Eclipse>>project properties>>Android>> make sure that the project you are testing is referenced under Library. 'is library' can remain unchecked Commented Jan 30, 2015 at 14:18
  • It's not really named either of those - I anonymized the code for SO. But that's not the problem - the code wouldn't have compiled if I had a syntax error. It compiles, but fails runtime Commented Jan 30, 2015 at 14:21
  • Sorry, I edited my comment while you were responding. Most posts on the subject say that you are finding two versions of the library, once during compile and another during runtime. Commented Jan 30, 2015 at 14:23
  • I should probably stop commenting. I just saw you are using Studio and not eclipse. Doh! Commented Jan 30, 2015 at 14:26
  • I havent tried it myself but you could try adding a dependency in test configuration in android studio? Commented Feb 5, 2015 at 18:40

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ahh interesting. Didn't release the method causing the issue was not related to instrumentationTestCase. Good catch though!
1

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

1 Comment

Only dependencies exlusive to the tests, like junit, is supposed to be specified this way. My main app already has a dependeny through "compile" to the library, so it isn't necessary to reference it again. Solution was related to proguard though - see my other answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.