Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unresolved reference : BuildConfig in Kotlin

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.

like image 681
MaChee Neraid Avatar asked Mar 31 '20 02:03

MaChee Neraid


People also ask

What is unresolved reference error in Kotlin?

To conclude, the unresolved reference error happens when Kotlin has no idea what the keyword you’re typing in the file points to. It may be a function, a variable, or another construct of the language that you’ve yet to declare in the code.

Is it possible to reference Kotlin objects and functions in buildsrc?

This repository has been archived by the owner. It is now read-only. The user should be able to reference objects and functions defined in his buildSrc/src/main/kotlin directory. I have a working kotlin dsl gradle build: kotlin-dsl-gradle-experiment I've written a function in my buildSrc/src/main/kotlin/test.kt file as follows:

Why does my Android application say unresolved reference?

When you’re writing code for Android application and Kotlin, you may frequently encounter a static error from your IDE saying unresolved reference for a specific keyword or variable. For example, you can produce the error by calling a custom function that you haven’t defined in your code yet: Or when you assign a variable without declaring them:

Is Kotlin DSL Gradle read-only?

It is now read-only. The user should be able to reference objects and functions defined in his buildSrc/src/main/kotlin directory. I have a working kotlin dsl gradle build: kotlin-dsl-gradle-experiment


3 Answers

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

like image 59
IntelliJ Amiya Avatar answered Nov 15 '22 13:11

IntelliJ Amiya


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.

like image 26
Rahul Sharma Avatar answered Nov 15 '22 12:11

Rahul Sharma


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.

like image 30
user496854 Avatar answered Nov 15 '22 13:11

user496854