How to determine selected `configuration` in Android?

374 views Asked by At

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

2

There are 2 answers

0
CommonsWare On BEST ANSWER

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:

  • the main app
  • tests for the main app
  • tests for the something library
  • etc.

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:

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

0
Jonas Köritz On

You can configure different resource sets. By default main and debug already exist. To determine at runtime which set was used to build the apk create a new resource file in each resource set, e.g.

app/src/main/res/values/resourceset.xml and app/src/debug/res/values/resourceset.xml

and place a single string or integer value inside like this:

<resources>
     <string name="resource_set">debug</string>
</resources>

and

<resources>
     <string name="resource_set">main</string>
</resources>

You may then use getString() to get the value for R.string.resource_set and you can detect which resource set was used.

I am using this technique to include different Google API client IDs depending on the resource set that was used (to enable debugging with Google APIs and release with another fingerprint then debug).