TExcelTAD
Home › Mobile Build Pipelines: Gradle/AGP, Xcode, SwiftPM & Fastlane › buildConfigField, resValue & Placeholders

buildConfigField, resValue & Placeholders

Variants usually differ in a few values: the backend URL, a feature switch, the app's display name, a key for a crash reporter. AGP has three ways to inject such values from the build script, one per place they're consumed:

Mechanism Ends up in Read it from
buildConfigField(type, name, value) a generated Java class BuildConfig code: BuildConfig.API_URL
resValue(type, name, value) a generated resource file code (R.string.x), layouts, the manifest (@string/x)
manifestPlaceholders["key"] = value ${key} in AndroidManifest.xml the manifest merger

All three can be set in defaultConfig, in a build type and in a flavor; the most specific one wins.

BuildConfig is off by default

Since AGP 8, the BuildConfig class is only generated when you ask for it. Declaring a field without enabling the feature stops the build while it's still being configured, which is often the first thing you'll see when you upgrade an old project:

bash · try it yourself
mkdir -p hello/app/src/main/java/com/example/hello && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application android:label="Hello" /></manifest>' > app/src/main/AndroidManifest.xml
cat > app/src/main/java/com/example/hello/Api.java <<'JAVA'
package com.example.hello;

public final class Api {
    public static String baseUrl() { return BuildConfig.API_URL; }
}
JAVA
gradle wrapper > /dev/null   # first: a failing build script would stop it too
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }

android {
    namespace = "com.example.hello"
    compileSdk = 36
    defaultConfig {
        applicationId = "com.example.hello"
        minSdk = 24
        targetSdk = 36
        versionCode = 1
        versionName = "1.0"
        buildConfigField("String", "API_URL", "\"https://api.example.com\"")
    }
}
KTS
./gradlew -q assembleDebug 2>&1 | grep -A4 'What went wrong'
* What went wrong:
A problem occurred configuring project ':app'.
>                     defaultConfig contains custom BuildConfig fields, but the feature is disabled.
                      To enable the feature, add the following to your module-level build.gradle:
  `android.buildFeatures.buildConfig = true`

The message names the fix: buildFeatures { buildConfig = true }, per module that needs it (older projects set android.defaults.buildfeatures.buildconfig=true in gradle.properties for all modules instead). Code that uses BuildConfig in a module with no custom fields and the feature off fails later, in javac, with cannot find symbol.

All three, per build type

Now the whole set: a staging URL for debug builds, a different app name, and a crash-reporter key that differs per build type. resValue also needs its build feature turned on:

bash · try it yourself
mkdir -p hello/app/src/main/java/com/example/hello && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
cat > app/src/main/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:label="@string/app_name">
        <meta-data android:name="crash.reporter.key" android:value="${crashKey}" />
    </application>
</manifest>
XML
cat > app/src/main/java/com/example/hello/Api.java <<'JAVA'
package com.example.hello;

public final class Api {
    public static String baseUrl() { return BuildConfig.API_URL; }
}
JAVA
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }

android {
    namespace = "com.example.hello"
    compileSdk = 36
    defaultConfig {
        applicationId = "com.example.hello"
        minSdk = 24
        targetSdk = 36
        versionCode = 7
        versionName = "1.2.0"
        buildConfigField("String", "API_URL", "\"https://api.example.com\"")
        buildConfigField("boolean", "NEW_CHECKOUT", "false")
        resValue("string", "app_name", "Hello")
        manifestPlaceholders["crashKey"] = "prod-1234"
    }
    buildTypes {
        debug {
            applicationIdSuffix = ".debug"
            buildConfigField("String", "API_URL", "\"https://staging.example.com\"")
            buildConfigField("boolean", "NEW_CHECKOUT", "true")
            resValue("string", "app_name", "Hello (debug)")
            manifestPlaceholders["crashKey"] = "dev-5678"
        }
    }
    buildFeatures { buildConfig = true; resValues = true }
}
KTS
gradle wrapper > /dev/null
./gradlew -q assembleDebug assembleRelease
cat app/build/generated/source/buildConfig/debug/com/example/hello/BuildConfig.java
grep -h 'string name' app/build/generated/res/resValues/*/values/gradleResValues.xml
for t in debug release; do
  echo "$t: $(grep -o 'android:value="[^"]*"' app/build/intermediates/merged_manifests/$t/process${t^}Manifest/AndroidManifest.xml)"
done
/**
 * Automatically generated file. DO NOT MODIFY
 */
package com.example.hello;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.example.hello.debug";
  public static final String BUILD_TYPE = "debug";
  public static final int VERSION_CODE = 7;
  public static final String VERSION_NAME = "1.2.0";
  // Field from build type: debug
  public static final String API_URL = "https://staging.example.com";
  // Field from build type: debug
  public static final boolean NEW_CHECKOUT = true;
}
    <string name="app_name" translatable="false">Hello (debug)</string>
    <string name="app_name" translatable="false">Hello</string>
debug: android:value="dev-5678"
release: android:value="prod-1234"

Things to notice in the generated class:

  • The value is Java source. buildConfigField("String", "API_URL", "\"https://…\"") needs the inner quotes, because the third argument is pasted into the class as is. Forgetting them is the classic mistake (it produces = https://api.example.com;, which doesn't compile).
  • Free fields. DEBUG, APPLICATION_ID, BUILD_TYPE, VERSION_CODE and VERSION_NAME come for free; with flavors you also get FLAVOR and one field per dimension.
  • Comments name the source (// Field from build type: debug), which helps when three levels set the same field.
  • BuildConfig.DEBUG is Boolean.parseBoolean("true") rather than a literal true. That's deliberate: a literal would be inlined by javac into library code compiled against it; R8 still folds it in release builds.

Which one to use

You need… Use
a constant in code (URL, feature switch, build time info) buildConfigField
a string/color/bool visible to layouts, other resources or the manifest resValue
a value inside the manifest (a library's API key meta-data, a deep-link host, an authority) manifestPlaceholders

Not for secrets

Everything injected this way ends up in the APK, readable by anyone with unzip and a dex viewer. A crash reporter's public key is fine; a server secret is not, no matter how it's injected. Keep real secrets on your server. And don't commit even harmless per-environment values to the build script if they differ per developer: read them from a Gradle property instead, as in Signing Configs:

kotlin
val apiUrl = providers.gradleProperty("apiUrl").orElse("https://api.example.com").get()
android { defaultConfig { buildConfigField("String", "API_URL", "\"$apiUrl\"") } }

Test yourself

Why does buildConfigField("String", "API_URL", "https://x") (without inner quotes) break the build?

  1. URLs are not allowed
  2. The value is pasted into Java source as is, so it becomes = https://x;
  3. String fields need resValue
  4. BuildConfig only allows booleans

The manifest contains android:value="${crashKey}". What supplies the value?

  1. buildConfigField
  2. resValue
  3. manifestPlaceholders
  4. local.properties

A module declares buildConfigField(...) but not buildFeatures { buildConfig = true }. What happens with AGP 9?

  1. The field is silently ignored
  2. The build fails during configuration, saying the BuildConfig feature is disabled
  3. R8 removes the field
  4. BuildConfig is generated anyway