androidandroid-studioandroid-buildconfig

How to determine selected `configuration` in Android?


In Android Studio I created two configurations:

enter image description here

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).


Solution

  • 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:

    debug 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: