gpt4 book ai didi

android - 尝试使用 proguard 运行测试

转载 作者:行者123 更新时间:2023-12-04 15:35:47 28 4
gpt4 key购买 nike

我正在尝试在启用了 proguard 的 apk 上运行单元测试。

这是我的 build.gradle:

  buildTypes {

debug {
debuggable true
minifyEnabled true
ext.alwaysUpdateBuildId = false

testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-test-rules.pro'
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

...
}

和我的 proguard-test-rules.pro
# Proguard rules that are applied to your test apk/code.
-ignorewarnings

-keepattributes *Annotation*

-dontnote junit.framework.**
-dontnote junit.runner.**

-keep androidx.test.**
-keep org.junit.**
-keep org.hamcrest.**
-keep com.nhaarman.mockitokotlin2.**
-keep androidx.arch.core.**
-keep com.android.support.test.uiautomator.**
-dontwarn androidx.test.**
-dontwarn android.test.**
-dontwarn org.junit.**
-dontwarn org.hamcrest.**
-dontwarn com.squareup.javawriter.JavaWriter
# Uncomment this if you use Mockito
-dontwarn org.mockito.**

-kepp com.google.truth.**

-keep class com.fireblocks.**
-keep @org.junit.runner.RunWith public class *

-keep class androidx.test.internal.runner.listener.InstrumentationRunListener
-dontwarn org.junit.runner.**
-keep class org.junit.runner.notification.RunListener

我的测试因以下异常而无法启动:
    java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/test/internal/runner/listener/InstrumentationRunListener;
at java.lang.Class.newInstance(Native Method)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6391)
at android.app.ActivityThread.access$1300(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.ClassNotFoundException: androidx.test.internal.runner.listener.InstrumentationRunListener
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at java.lang.Class.newInstance(Native Method) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6391) 
at android.app.ActivityThread.access$1300(ActivityThread.java:219) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/junit/runner/notification/RunListener;
at java.lang.Class.newInstance(Native Method) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6391) 
at android.app.ActivityThread.access$1300(ActivityThread.java:219) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.junit.runner.notification.RunListener" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.client.test-XpSnSNlzNnJvG9ggqH7a8Q==/base.apk", zip file "/data/app/com.fireblocks.client-bEwzAM7c2o8Q2TpJG3XUVw==/base.apk"],nativeLibraryDirectories=[/data/app/com.fireblocks.client.test-XpSnSNlzNnJvG9ggqH7a8Q==/lib/x86_64, /data/app/com.fireblocks.client-bEwzAM7c2o8Q2TpJG3XUVw==/lib/x86_64, /data/app/com.fireblocks.client.test-XpSnSNlzNnJvG9ggqH7a8Q==/base.apk!/lib/x86_64, /data/app/com.fireblocks.client-bEwzAM7c2o8Q2TpJG3XUVw==/base.apk!/lib/x86_64, /system/lib64, /system/product/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at java.lang.Class.newInstance(Native Method) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6391) 
at android.app.ActivityThread.access$1300(ActivityThread.java:219) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

我如何解决它?

最佳答案

似乎 testProguardFiles 被完全忽略了。
我不得不将一些规则移动到普通的 proguard 文件中,以便能够保留运行测试框架的代码。
不是最佳的,但现在可以了。

关于android - 尝试使用 proguard 运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59830368/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com