Android - gradle testProguardFile - what is its pu

2019-06-24 00:28发布

Can someone help me understand the use of testProguardFile. Lets say i have debug buildType and its configured like this in gradle build file:

// 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')
    }

then why am i supplying another proguard rules file called test-proguard-rules.pro for testing ? If i understand correctly, when i do instrumentation testing a separate apk is generated but what if i'm doing just unit test, is it also the case ?

what i would like to be able to do is run "unit tests" (not instrumentation tests) but have the unit test apk use the proguard rules that i have defined in my project build settings.

2条回答
再贱就再见
2楼-- · 2019-06-24 00:51

As I try to explain in my blog post - Configuring ProGuard for Android Instrumentation Tests:

Initially, there are roughly 2 main, distinct Gradle tasks for running instrumentation tests: assembleRelease and assembleAndroidTest. The former executes what's needed in order to generate the app .apk. The latter triggers the assembly of an instrumentation testing .apk, which gets installed on the test device alongside the app when instrumentation tests are run (both are run in the context of the same OS process).

While proguardFiles sets the list of ProGuard rule files to consider when generating the app .apk, testProguardFiles does the same with respect to the instrumentation .apk.

So, while the naming is confusing, testProguardFiles has no effect on unit tests, but it's important for instrumentation tests running in ProGuard-enabled projects.

查看更多
我命由我不由天
3楼-- · 2019-06-24 00:57

Now i see what it is testProguadFile ('some proguard file') gives your test apk a proguard file to use when testing. This allows us test our app using a proguard file. The test apk generated when you run a test will be obfuscated with proguard and then the test will run. This is a good way to test for any anomalies proguard could create in your app.

查看更多
登录 后发表回答