I'd love to run my Android instrumented tests against the release build type. Here is what I did.
Changed the test build type as recommended here:
android {
testBuildType "release"
}
Created proguard-test-rules.pro
in the app/
directory and added it to the release build type section:
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-test-rules.pro'
}
}
proguard-test-rules.pro
contains the only line:
-dontwarn okio.**
However, when I run the command ./gradlew clean connectedMyFlavorReleaseAndroidTest
, I see the following errors in the console output:
Warning: okio.Okio: can't find referenced class java.nio.file.Files
So it looks like proguard-test-rules.pro
is ignored. Why?
Sure I can use this solution, but I just wonder why proguard-test-rules.pro
is ignored. I also tried proguardFile 'proguard-test-rules.pro'
which didn't work either.
My Gradle
classpath 'com.android.tools.build:gradle:3.0.1'
This took me quite a while to solve; it does seem that for whatever reason recent versions of gradle/proGuard seem to ignore that setting. (testProguardFile("your-file.pro"))
Found a really clean and simple way to do it using some conditional logic inside your release.
Below is the version in kotlin dsl, so build.gradle.kts but no doubt there will be the Groovy equivalent if using regular build.gradle:
getByName("release") {
signingConfig = signingConfigs.getByName("release")
isDebuggable = false
// any other release config as normal
isMinifyEnabled = !gradle.startParameter.taskNames.any { it.contains("AndroidTest") }
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
Explaning what this does:
isMinifyEnabled = !gradle.startParameter.taskNames.any { it.contains("AndroidTest") }
This is saying DON'T MINIFY if the gradle task that kicked off the build has "AndroidTest" in it somewhere e.g. assembleReleaseAndroidTest
Confirmed this still minifies the release apk, and now any minification for the test apk is totally turned off, and all unit tests now run fine on the release apk.