由于使用了Kotlin,Google libs等(基本的Android开发问题),我已接近dex限制。因此,我启用了proguard,现在似乎无法将其用于仪器测试。
这是堆栈跟踪:
E/TestExecutor: Fatal exception when running tests
java.lang.RuntimeException: failure in processEncodedAnnotation
at java.lang.Class.getDeclaredAnnotations(Native Method)
at java.lang.Class.getAnnotations(Class.java:297)
at org.junit.runner.Description.createSuiteDescription(Description.java:124)
at org.junit.internal.runners.ErrorReportingRunner.getDescription(ErrorReportingRunner.java:28)
at org.junit.runners.Suite.describeChild(Suite.java:123)
at org.junit.runners.Suite.describeChild(Suite.java:27)
at org.junit.runners.ParentRunner.shouldRun(ParentRunner.java:434)
at org.junit.runners.ParentRunner.filter(ParentRunner.java:382)
at org.junit.runner.manipulation.Filter.apply(Filter.java:97)
at android.support.test.internal.runner.TestRequestBuilder$LenientFilterRequest.getRunner(TestRequestBuilder.java:387)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at com.isupatches.test.CustomTestRunner.onStart(CustomTestRunner.kt:14)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
我了解这是在创建套件描述和启动自定义测试运行器时处理注释的问题,但是我不知道为什么,而且stacktrace不太有用。
我将JUnit5用于单元测试,而不用于仪表测试。我也在使用Dagger2和Kotlin。
我已经清理并重建了,现在我要问我是否缺少明显的东西。
defaultConfig {
applicationId "com.isupatches.test"
minSdkVersion rootProject.ext.min_sdk_version
targetSdkVersion rootProject.ext.max_sdk_version
versionCode rootProject.ext.version_code
versionName rootProject.ext.version_name
testInstrumentationRunner "com.isupatches.test.CustomTestRunner"
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath false // Necessary to make kapt work
}
}
}
这是我的自定义测试运行程序,其中super.onStart()是堆栈跟踪中的行:
class CustomTestRunner : AndroidJUnitRunner() {
override fun onStart() {
SystemAnimations.disableAll(InstrumentationRegistry.getTargetContext())
super.onStart()
}
override fun finish(resultCode: Int, results: Bundle) {
SystemAnimations.restoreAll(InstrumentationRegistry.getTargetContext())
super.finish(resultCode, results)
}
}
我的proguard设置:
buildTypes {
debug {
testCoverageEnabled true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testProguardFile file("proguard-test-rules.pro")
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
保护规则:
-dontoptimize
-dontpreverify
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-keepattributes *Annotation*, LineNumberTable, SourceFile
-keep class com.isupatches.test.** { *; }
-dontwarn com.isupatches.test.**
-keep class kotlin.Metadata
-keep class kotlin.reflect.** { *; }
-dontwarn kotlin.reflect.**
-keep class kotlin.jvm.internal.** { *; }
-dontwarn kotlin.jvm.internal.**
-keep class com.google.errorprone.annotations.** { *; }
-dontwarn com.google.errorprone.annotations.*
-keep class com.google.devtools.build.android.desugar.runtime.ThrowableExtension {
public <methods>;
}
-dontwarn com.google.devtools.build.android.desugar.runtime.ThrowableExtension
-keep class com.metova.cappuccino.animations.SystemAnimations {
public <methods>;
}
测试proguard规则:
-include proguard-rules.pro
-dontobfuscate
-dontnote **
-keep class org.xmlpull.v1.** { *; }
-dontwarn org.xmlpull.v1.*
-keep class sun.misc.** { *; }
-dontwarn sun.misc.*
-keep class org.mockito.internal.creation.bytebuddy.** { *; }
-dontwarn org.mockito.internal.creation.bytebuddy.*
-keep class java.lang.instrument.** { *;}
-dontwarn java.lang.instrument.*
-keep class net.bytebuddy.jar.asm.tree.** { *; }
-dontwarn net.bytebuddy.jar.asm.tree.*
-keep class libcore.io.** { *; }
-dontwarn libcore.io.*
-keep class org.ccil.cowan.tagsoup.Parser
-dontwarn org.ccil.cowan.tagsoup.Parser
-keep class com.google.android.apps.common.testing.testrunner.web.** { *; }
-dontwarn com.google.android.apps.common.testing.testrunner.web.*
-keep class edu.umd.cs.findbugs.annotations.** { *; }
-dontwarn edu.umd.cs.findbugs.annotations.*
-keep class org.newsclub.net.unix.** { *; }
-dontwarn org.newsclub.net.unix.*
-keep class java.lanag.management.** { *; }
-dontwarn java.lang.management.*
-keep class net.bytebuddy.jar.asm.commons.** { *; }
-dontwarn net.bytebuddy.jar.asm.commons.*
-keep class com.metova.cappuccino.** { *; }
-dontwarn com.metova.cappuccino.*
-keep class org.junit.** { *; }
-keep class org.junit.runners.** { *; }
-dontwarn org.junit.**
-keep class android.support.test.runner.** { *; }
-keep class android.support.test.internal.runner.** { *; }