FTM
FTM

Reputation: 2067

Unresolved reference while using BuildConfig

I am not being able to build my android project in Android Studio due to the following error:

Task :app:compileDebugKotlin FAILED e: {file name and line in code} Unresolved reference 'ANDROID_ID'.

This is the piece of code where the line with the error is:

class SomeActivity: ComponentActivity() {

    private val androidId = BuildConfig.ANDROID_ID

    // Rest of the code...
}

And this is my build.gradle.kts (:app) file:

import com.android.build.api.dsl.Packaging
import java.io.FileInputStream
import java.util.Properties

val keyPropertiesFile = rootProject.file("keys.properties")
val keyProperties = Properties()
keyProperties.load(FileInputStream(keyPropertiesFile))

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.compose)
    id("com.google.gms.google-services")
}

android {
    // some properties ...

    buildFeatures {
        buildConfig = true
    }

    defaultConfig {
        // some properties ...

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

        buildConfigField("String", "ANDROID_ID", keyProperties.getProperty("ANDROID_ID"))
    }

    // other stuff ...
}

dependencies {
    // dependencies ...
}

And this is my keys.properties file which is located at the root of the project:

ANDROID_ID = "my ID"

I don't understand why Android Studio isn't being able to build the project. And, the weird thing is, when I delete private val androidId = BuildConfig.ANDROID_ID from my code, build it, and debug it, during debugging I can print the value of BuildConfig.ANDROID_ID. In other words, the configuration seems to be correct, but Android Studio refuses to build my code.

What is going on and what can I do?

Upvotes: 1

Views: 35

Answers (1)

FTM
FTM

Reputation: 2067

As pointed out by @jokuskay, my code was using the incorrect BuildConfig class. For some reason, I was importing Google's Firebase BuildConfig into my activity with the line of code import com.google.firebase.BuildConfig. That was a mistake and simply deleting this line fixed the issue.

Thanks @jokuskay!

Upvotes: 0

Related Questions