使用ProGuard + Dagger时,Firebase测试实验室失败

时间:2018-10-11 11:55:56

标签: android proguard android-espresso dagger-2 firebase-test-lab

在满足以下条件的情况下,仪器测试确实可以在本地仿真器和物理设备上通过,但在Firebase测试实验室上失败:

  • 已启用ProGuard的调试版本;
  • 同时具有Dagger和Espresso依赖项。

FTL显示了不同的测试问题:

1)对于API 26-28,它显示Instrumentation run failed due to 'java.lang.NoClassDefFoundError'Instrumentation run failed due to 'Process crashed.'

异常stacktrace看起来像这样,它并不总是显示在Firebase中,而是总是出现在logcat中:

Rejecting re-init on previously-failed class java.lang.Class<androidx.test.espresso.core.internal.deps.dagger.internal.Factory>:
java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/inject/Provider;

FATAL EXCEPTION: Instr: androidx.test.runner.AndroidJUnitRunner
Process: com.example.debug, PID: 11425
java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/inject/Provider;
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:453)
    at androidx.test.internal.runner.TestLoader.doCreateRunner(TestLoader.java:72)
    at androidx.test.internal.runner.TestLoader.getRunnersFor(TestLoader.java:104)
    at androidx.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:789)
    at androidx.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:544)
    at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:387)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.inject.Provider" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.example.debug.test-9kvw--JgNKzmuQurRdDbCQ==/base.apk", zip file "/data/app/com.example.debug-sz-oCUGs05zlEadCzyqsDA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.debug.test-9kvw--JgNKzmuQurRdDbCQ==/lib/arm64, /data/app/com.example.debug-sz-oCUGs05zlEadCzyqsDA==/lib/arm64, /system/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    ... 8 more

2)我还对API 21进行了一项测试,该测试在日志中仅包含一个NoClassDefFoundError。但是,此异常也出现在API 26-28上,但这是上面显示的异常的一部分。在不同的API级别上进行记录的方式也许只是有所不同。

java.lang.NoClassDefFoundError: androidx.test.espresso.core.internal.deps.dagger.internal.Factory

FATAL EXCEPTION: Instr: androidx.test.runner.AndroidJUnitRunner
Process: com.example.debug, PID: 5691
java.lang.NoClassDefFoundError: androidx.test.espresso.core.internal.deps.dagger.internal.Factory
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:308)
    at androidx.test.internal.runner.TestLoader.doCreateRunner(Unknown Source)
    at androidx.test.internal.runner.TestLoader.getRunnersFor(Unknown Source)
    at androidx.test.internal.runner.TestRequestBuilder.build(Unknown Source)
    at androidx.test.runner.AndroidJUnitRunner.buildRequest(Unknown Source)
    at androidx.test.runner.AndroidJUnitRunner.onStart(Unknown Source)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)

build.gradle配置中的一些相关行:

android {
  defaultConfig {
    testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
  }

  buildTypes {
    debug {
      minifyEnabled true
      useProguard true
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules-debug.pro'
      testProguardFile 'proguard-rules-test.pro'
    }
  }
}

dependencies {
  testImplementation 'junit:junit:4.12'

  androidTestImplementation 'androidx.test:core:1.0.0-beta01'
  androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-beta01') {
    //  exclude module: 'javax.inject' - Exclusion doesn't help
  }
  // androidTestImplementation 'javax.inject:javax.inject:1' - Inclusion doesn't help either
  androidTestImplementation('androidx.test.ext:junit:1.0.0-beta01') {
    exclude group: "org.junit"
  }
  androidTestImplementation 'androidx.test:runner:1.1.0-beta01'
  androidTestImplementation 'androidx.test:rules:1.1.0-beta01'
  androidTestImplementation 'org.mockito:mockito-android:2.22.0'

  implementation 'com.google.dagger:dagger:2.16'
  kapt 'com.google.dagger:dagger-compiler:2.16'
}

proguard-rules-test.pro:

-ignorewarnings
-dontshrink
-dontoptimize
-dontobfuscate

禁用ProGuard或删除Dagger依赖性后,测试开始通过FTL。

1 个答案:

答案 0 :(得分:0)

所以我联系了Firebase支持人员,并得到了一个尴尬的简单解决方案。
将以下规则添加到proguard-rules-debug.pro

-keep class javax.inject.** { *; }

尚不清楚为什么在本地测试时不会出现此问题。
但是至少该解决方案有效。