Android TDD Part 12 — Set test environment variables from Gradle

Evan Chen
3 min readAug 17, 2020

--

In this article, we'll introduce setting a test environment variable from Gradle.

ProductFlavor

ProductFlavor let you choose different build type when you build your APK. For example, if you have two web APIs, one for production, the other for staging. You can set a production and staging productFlavor. App use production web API when you build a production version, on the contrary, use staging web API. Now, we want to set a prod and mock productFlavors to get different data from the Repository.

Creates a flavor dimension named "mock" and adds "prod" product flavors.

android {
...
productFlavors {
mock {
applicationId "evan.chen.tutorial.tdd.productflavorssample.mock"
}
prod {
applicationId "evan.chen.tutorial.tdd.productflavorssample"
}
}
flavorDimensions "default"

Click Sync Now in the notification bar. After the sync completes, you can see 4 build variants.

Switch mode from Android to Project.

Create a mock and prod package in the src folder. When you choose mockDebug or mockRelease Build Variant, the Repository in the mock folder will be executed. When you choose prodDebug or prodRelease Build Variant, the Repository in the prod folder will be executed.

Repository in prod folder

class Repository {
fun getResult(): String {
return "Result from Remote"
}
}

Repository in mock folder

class Repository {
fun getResult(): String {
return "Result from Mock"
}
}

When you call repository.getResult on mock build variant, the result is "Result from Mock". When you call repository.getResult on prod build variant, the result is "Result from Prod".

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val repository = Repository()
val result = repository.getResult()
textView.text = result
}
}

Set string of resources

productFlavors {
mock {
applicationId "evan.chen.tutorial.tdd.productflavorssample.mock"
resValue "string", "name", "from mock"
}
prod {
applicationId "evan.chen.tutorial.tdd.productflavorssample"
resValue "string", "name", "from prod"
}
}

You can get the string on different product flavor.

textView2.text = resources.getString(R.string.name)

Create a sourceSets for test

A SourceSet represents a logical group of Java source and resource files. The following codes show adding a java source and resource in sourceSets.

sourceSets {
String sharedTestDir = 'src/sharedTest/java'
String fakeJsonDir = 'src/sharedTest/fakejson'

test {
java.srcDir sharedTestDir
resources.srcDirs += fakeJsonDir
}
androidTest {
java.srcDir sharedTestDir
resources.srcDirs += fakeJsonDir
}
}

Back to project, create a sharedTest folder. This folder only can run in test and androidTest.

Github:
https://github.com/evanchen76/AndroidProductflavorsample

Reference:
https://developer.android.com/studio/build/build-variants

Next: Android TDD Part 13 — Summary of testing

--

--