Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - gradle testProguardFile - what is its purpose relating to unit tests

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.

like image 685
j2emanue Avatar asked Aug 13 '15 12:08

j2emanue


People also ask

How do you run a test case using Gradle command?

Use the command ./gradlew test to run all tests.

How do I test Gradle tasks?

Within IntelliJ IDEA, find the check task in the Gradle tool window under verification. Double click it to run. Check is also run when you run the build task, thanks to a task dependency setup by the base plugin. Gradle runs the assemble task then check.

How do you test build Gradle?

End-to-end tests for Gradle plugins stand up a build script, apply the plugin under test and execute the build with a specific task. The outcome of the build (e.g. standard output/error or generated artifacts) verifies the correctness of the functionality.


2 Answers

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.

like image 169
d4vidi Avatar answered Oct 12 '22 15:10

d4vidi


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.

like image 21
j2emanue Avatar answered Oct 12 '22 15:10

j2emanue