2

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?

2
  • 1
    Are you importing the correct BuildConfig? For example, perhaps maybe your module has not enabled BuildConfig to be generated, and you are importing a BuildConfig from another module. Commented Sep 24 at 15:18
  • Thank you for your answer. I had forgotten to set BuildFeatures { buildConfig = true} in my build.gradle. After enabling that and making sure I used the correct buildConfig from my app module, it works now. Commented Sep 24 at 19:15

1 Answer 1

2

You need to set buildConfig = true in order to add/generate buildConfigField in your app gradle file...

android {
    ##namespace 'xxx.xxx.xxx.xxx'
    ##compileSdk 36
    buildFeatures {
       buildConfig true
    }
    
}

Happy coding ....

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.