proguard gradle debug build but not the tests

眉间皱痕 提交于 2019-12-17 15:58:08

问题


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
        }
    }
}

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?


回答1:


Put

gradle.projectsEvaluated {
    proguardDebugTest.enabled = false
}

it in your build script.

There are two things to know here:

  • 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.

EDIT:

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.**



回答2:


runProguard is old. It was replaced with minifyEnabled

With minifyEnabled (and other changes in new versions of gradle) you will may encounter issues where the proguard config works for your debug apk but not for the instrumentation tests. The apk created for instrumentation tests will use its own proguard file, so changing your existing proguard file will have no effect.

In this case, you need to specify the proguard file to use on the instrumentation tests. It can be quite permissive because it's not affecting your debug and release builds at all.

    // inside android block
    debug {
        shrinkResources true  // removes unused graphics etc
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        testProguardFile('test-proguard-rules.pro')
    }



回答3:


Introduce a new build type "derived" from debug specific to the test app that disables ProGuard again like

android {
    buildTypes {
        debugTest.initWith(debug)
        debugTest {
            minifyEnabled false
        }
    }
}

and use that build type for the test app by assigning its name to the testBuildType property

android {
    testBuildType 'debugTest'
}


来源:https://stackoverflow.com/questions/21463089/proguard-gradle-debug-build-but-not-the-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!