unresolved reference : BuildConfig in Kotlin
Asked Answered
P

13

57

So I started a new android application,
here is my Android Studio info

Android Studio 3.6.1
Build #AI-192.7142.36.36.6241897, built on February 27, 2020
Runtime version: 1.8.0_212-release-1586-b04 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 7 6.1

I also added

buildConfigField "String", "MODE", "FREE"

in my product flavor.

When I'm now getting this in my MainActivity's onCreate,

BuildConfig

is unresolved reference.

Prison answered 31/3, 2020 at 2:35 Comment(0)
R
108

If you are in 2023 or 2024 and having this issue, add

android.defaults.buildfeatures.buildconfig=true

to your gradle.properties file.

Rewire answered 27/4, 2023 at 20:27 Comment(2)
Blog post about this: medium.com/androiddevelopers/…Piker
That's deprecated alreadyHeer
P
68

Starting from Android Studio Flamingo release and Android Gradle Plugin (AGP) 8.0 generating BuildConfig files is disabled by default and should be enabled manually only for modules that need it

Add this to required module's build.gradle:

android {
  ...
  buildFeatures {
    buildConfig = true
  }
}

Instead, if you need the old behavior for all modules, set the android.defaults.buildfeatures.buildconfig=true in your gradle.properties file

Permit answered 25/5, 2023 at 13:2 Comment(4)
Thanks this was the problem in my case.Eosin
Good news - it worked. Bad news - now I crave violence.Aurochs
Someone give this man a gold star. Correct answer FTW!!Hoplite
Addition step that works for me: Android Studio Hedgehog | 2023.1.1 Patch 2 1) Go to Refactor menu -> Migrate BuildConfig to Gradle Build Files 2) Go to Build menu -> Make selected modulesAquamarine
Z
29

Here are some simple steps which resolved this issue:

  1. Go to File menu > Build > Clean Project
  2. Now go to File menu > Build > Rebuild Project

(Above steps may resolve the issue, if not, follow step 3)

  1. Now go to the file where you are facing this issue(RetrofitClient.kt in my case) and manually add line import com.example.myapplication.BuildConfig

(Replace "com.example.myapplication" with your package name)

That's all.

Zins answered 17/4, 2021 at 4:59 Comment(1)
For anyone who still faces the issue after Step 3, try to invalidate your IDE's cache (File -> Invalidate Caches...). If the latter doesn't work, your last option would be to comment out (temporarily remove) any reference to BuildConfig in your project, make a successful build, then uncomment the references again.Transcendental
B
13

unresolved reference

  • Make sure you choose build variant. Like DEBUG mode.
  • Finally Clean-Rebuild-Restart IDE.

Don't

buildConfigField("String", "MODE", "FREE")

Do

buildConfigField("String", 'MODE', '"FREE"')

enter image description here

Beveridge answered 31/3, 2020 at 3:31 Comment(3)
Why does single quote instead of double quote make a difference?Corinnacorinne
I don't know, but I simply doesLark
The important details is '"FREE"': double quotes string wrapepd in single quotes. That is how you define a string in build.gradle.Fibriform
S
12

If you call the BuildConfig class from your module code, you must enable buildConfig in the android {} block in your module’s build.gradle.kts file starting from AGP v8.0. Otherwise the BuildConfig file isn’t automatically generated anymore.

android {
  buildFeatures {
    buildConfig = true
  }
}

More details on how to prepare your app build for Android Studio Flamingo can be found in this post from Android Developers.

Swarth answered 13/6, 2023 at 7:4 Comment(0)
I
2

This worked for me: Clean the project, then add this line to the gradle.properties file:

android.defaults.buildfeatures.buildconfig=true

Finally, rebuild the project and you will have the BuildConfig variable available.

P.D.: I am using Android Studio Girafle | 2022.3.1 (2023) and Gradle 8

Indemnification answered 23/9, 2023 at 19:54 Comment(1)
Yes, here it worked. Android Studio Iguana | 2023.2.1Townie
M
1

For me, it wasn't enough to clean the project for some reason. I had to delete the generated BuildConfig file and rebuild. After that, BuildConfig was automatically resolved again.

Meaghan answered 14/4, 2022 at 8:26 Comment(0)
C
0

I know it's been a while, but I found a reproducible solution to this problem...

Even though the "unresolved reference" error shows up in the IDE, everything compiles just fine. And as several people mentioned, cleaning and re-building the project doesn't fix the issue. Neither does the "Invalidate Caches" option in Android Studio.

What does work is temporarily adding a new new BuildConfig variable into the app gradle defaultConfig section. Something like:

buildConfigField "boolean", "TEST", "false"

And then rebuild the "unresolved reference" error goes away, and you can delete the BuildConfig variable you added, but the error still won't com back.

I should also mention that any time I clean the project, the error comes back, and I have to repeat this process.

Charlottcharlotta answered 1/8, 2022 at 0:27 Comment(0)
B
0

I had two distinct android studio projects with this BuildConfig.APPLICATION_ID unresolved problem. The first one was cured by the clean and build sequence. The other one wasn't. Difference was an import androidx.viewbinding.BuildConfig on the latter one. Removed it and then clean + build removed the problem. I don't recall why the import was there, probably it had been used at some point.

Bang answered 26/10, 2022 at 11:34 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Poppo
N
0

For me, the issue was that another module which was a library module(named common), had the same namespace as my main app module. My app module's namespace was common.example.myapplication and my common module was using the same name. So I changed the namespace from

...
android {
    namespace = "com.example.app"
}
...

to

android {
    namespace = "com.example.common"
}
...
Niacin answered 7/4, 2023 at 15:10 Comment(0)
T
0

As of Android Studio 3.5, BuildConfig.APPLICATION_ID is deprecated and replaced with BuildConfig.LIBRARY_PACKAGE_NAME

Alternatively you can use Context.getPackageName()

Tend answered 23/4, 2023 at 14:23 Comment(0)
T
0

I had this issue for the last 3 weeks and all of the suggestions didn't work.

  • clean&rebuild
  • invalidate cache&restart
  • changing flavors
  • changing/adding buildConfigFields
  • trying out every combination of quotes " ' '"'
  • install fresh Android Studio version in parallel

When I realized that it was not only my main project but every project including my 1-Screen-Test app I choose to reset Android Studio like described here: How to reset Android Studio

After going through the setup everything is back to normal.

Titanesque answered 19/5, 2023 at 11:41 Comment(0)
A
0

For me, it was due to the wrong namespace written in build.gradle should be com.appName consistent throughout. Also in MainActivity.kt, getMainComponentName(): should return the right appName. Check if the package name are right throughout.

Ardellaardelle answered 4/3 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.