In Android Studio I created two configurations:
How can I determine in code which configuration I selected?
I know that there is buildConfigField
in /app/build.gradle
but the names of the buildTypes
do not correspond to the configuration
names, so I wonder how does that all fit together.
android {
...
defaultConfig {
...
}
buildTypes {
debug {
...
buildConfigField 'boolean', 'DEBUG', 'true'
}
release {
...
}
}
}
I assume that in Android Studio a configuration
corresponds to a schema
in Xcode and a buildConfigField
corresponds to the Environment Variable
in Xcode (me coming from iOS world).
How can I determine in code which configuration I selected?
You don't, insofar as a run configuration is an IDE thing, not an Android thing.
what I want is to define environment variable values that I can use in code, e.g. in a debug build variant I want to connect to the development database, in a release build variant I want to connect to the production database
None of that has anything to do with run configurations. Run configurations are for configuring what is to be run:
app
app
something
librarydebug
versus release
are build types, one dimension of the build variant. You choose which build variant the run configurations use via the Build Variants tool, docked by default on the lower-left side of the Android Studio IDE window.
To have different code behavior based upon debug
versus release
, you can:
Examine BuildConfig.BUILD_TYPE
, which will be either debug
or release
Use buildConfigField
to inject values into BuildConfig
from Gradle, based upon build type and/or product flavor
Use resConfig
to inject values into resources, such as string resources
Use custom source sets per build type (e.g., src/main/
for your common code, src/debug/
for debug-specific code, src/release/
for release-specific code)