简体   繁体   中英

Testing release and proguarded version of Android app - NoSuchMethodError

I'm trying to test release build of Android app with Proguard turned on. I've created additional Proguard rules file for test dependencies which is included in the release rules file.

-keep class android.test.** { *; }
-dontwarn android.test.**

# Reuse the release ProGuard mapping
-applymapping proguard.map
-dontshrink
-dontoptimize

Everything compiles fine but it fails at runtime.

java.lang.NoSuchMethodError: android.test.AndroidTestRunner.addTestListener
    at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.start(ProGuard:135)
    at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner$BridgeTestRunner.start(ProGuard:249)
    at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:389)
    at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onCreate(ProGuard:114)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4702)
    at android.app.ActivityThread.access$1600(ActivityThread.java:172)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5586)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
    at dalvik.system.NativeStart.main(Native Method)

I'm using Espresso for testing, the instrumentation runner is defined in build.gradle

defaultConfig {
    testApplicationId 'package.name.test'
    testInstrumentationRunner 'com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner'
}

Any chance to run tests for proguarded release build? I've found topic back from May and it looks like it's impossible. For me it looks like it's just wrong Proguard configuration (missing method), but the line -keep class android.test.** { *; } -keep class android.test.** { *; } should fix such issue. Am I missing something?

Im trying to help Sir.

instead of that

-keep class android.test.** { *; } 
-dontwarn android.test.**

add this to it

-keep class android.test.** { *; }
-keep class junit.** { *; }
-dontwarn android.test.**
-dontwarn junit.**

actually this is a hard "guess-a-code" hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM