unresolved reference : BuildConfig in Kotlin

Viewed 8907

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.

4 Answers

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.

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

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.

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.

Related