I'm defining a particular field in the BuildConfig
for getting the URL
during runtime. So, for each build type, I use a different string:
prod {
buildConfigField "String", "BASE_URL", "\"abc.com\""
}
debug {
buildConfigField "String", "BASE_URL", "\"efg.com\""
}
Is it possible to define a different URL while running the android tests? I tried putting this setting under sourceSets->androidTest
, but it's not accepted.
What is buildConfigField? When you select a build variant at Android studio, it creates a build folder in your app's folder system. It's name is BuildConfig.java (You can see file place at bottom image) BuildConfig.java.
As I mentioned in above we can use buildConfigField to define custom fields. In this String is a data type and “S3_STORAGE” is the field name. Same as the custom field values we can define resource values inside build. gradle file.
In recent versions of the Android Developer Tools (ADT) for Eclipse, there's a class called BuildConfig which is automatically generated by the build. This class is updated automatically by Android's build system (like the R class), and it contains a static final boolean called DEBUG, which is normally set to true.
You have to pass it as a parameter to connectedAndroidTest
task.
android {
...
buildTypes {
prod {
buildConfigField "String", "BASE_URL", "\"${getBaseUrl("abc.com")}\""
}
debug {
buildConfigField "String", "BASE_URL", "\"${getBaseUrl("efg.com")}\""
}
}
}
def getBaseUrl(String fallback) {
return project.hasProperty("base_url") ? project.getProperties().get("base_url") : fallback
}
Then passing parameters via -P
:
./gradlew connectedDebugAndroidTest -Pbase_url="xxx.com"
./gradlew connectedProdAndroidTest -Pbase_url="yyy.com"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With