ProGuard的摇篮调试版本,但没有测试摇篮、版本、测试、ProGuard

2023-09-06 02:09:47 作者:伴我久还是放开手﹌

我用启用的ProGuard的调试版本:

I enabled proguard for the debug build using:

android {
    buildTypes {
        debug {
            runProguard true
            proguardFile 'proguard-debug.txt'
        }
        release {
            runProguard true
            proguardFile 'proguard-project.txt'
            zipAlign true
        }
    }
}

我遇到我这样做的问题是,摇篮构建希望proguardDebugTest任务期间ProGuard的测试也是如此。我似乎无法修改才能访问这个特殊的任务。有没有一种方法可以让我ProGuard的调试APK,但不是测试APK?

The problem I'm experiencing when I do this is that the gradle build wants to proguard the tests during the proguardDebugTest task as well. I can't seem to modify to get access to this particular task. Is there a way I can proguard the debug apk but not the test apk?

推荐答案

gradle.projectsEvaluated {
    proguardDebugTest.enabled = false
}

在你的构建脚本。

it in your build script.

有两件事要知道在这里:

There are two things to know here:

常规摇篮功能,启用/禁用任务的。 在Android的摇篮插件任务的具体递延创作 afterEvaluate ,所以你还需要推迟禁用任务,以 afterEvaluate 。 The general Gradle feature to enable / disable tasks. The Android Gradle plugin specific deferred creation of tasks in afterEvaluate, so you need to also defer disabling of the task to afterEvaluate.

一个小提醒:这将禁用任务,但失败的构建。这是因为:preDexDebugTest任务惯于使用ProGuard上运行。最好的解决办法我发现迄今是有调试的特定ProGuard的配置。更多细节这里。创建一个单独的ProGuard的配置文件,包括像这样正规的ProGuard文件:

One small note: It disables the task but fails the build. This is because the :preDexDebugTest task wont run with proguard on. The best solution i've found so far is to have debug specific proguard config. More details here. Create a separate proguard config file, include the regular proguard file like so:

-include proguard.cfg

和增加测试的配置。对我来说,它是:

and add test config. For me it was:

-dontwarn org.mockito.**
-dontwarn sun.reflect.**
-dontwarn android.test.**