I’m trying to pass a local environment variable into my Android project via Gradle and then access it in my Kotlin code. The idea is to configure Firebase Emulator host dynamically.
In my app/build.gradle I defined the following:
android {
...
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
buildConfigField "String", "FIREBASE_EMULATOR_HOST", "\"\""
}
debug {
def firebaseHost = System.getenv("FIRE_BASE_EMULATOR_IP") ?: "10.0.2.2"
buildConfigField "String", "FIREBASE_EMULATOR_HOST", "\"${firebaseHost}\""
}
}
In my Kotlin code I want to use this fild like:
dataBase.useEmulator(BuildConfig.FIREBASE_EMULATOR_HOST, 9001)
However, Android Studio shows the error: Unresolved reference: FIREBASE_EMULATOR_HOST
My setup: Android Studio: Android Studio Narwhal 3 Feature Drop | 2025.1.3
Gradle: 8.9
sourceCompatibility JavaVersion.VERSION_19
jvmTarget = JavaVersion.VERSION_19.toString()
kotlinCompilerExtensionVersion '1.5.14'
What I tried
- Cleaning and rebuilding the project (Build > Clean Project, Build >
- Rebuild Project) Invalidating caches & restarting Android Studio
- Verifying the environment variable (System.getenv("FIRE_BASE_EMULATOR_IP") works fine)
- Ensuring the code is inside app/src/main/java/...
Why is the BuildConfig.FIREBASE_EMULATOR_HOST field not recognized in my code?
How can I correctly pass environment variables to Gradle and then access them in my Kotlin code using BuildConfig?
BuildConfig? For example, perhaps maybe your module has not enabledBuildConfigto be generated, and you are importing aBuildConfigfrom another module.