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.
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.
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:
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:
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
unresolved reference
build variant
. Like DEBUG
mode.Clean-Rebuild-Restart
IDE.Don't
buildConfigField("String", "MODE", "FREE")
Do
buildConfigField("String", 'MODE', '"FREE"')
Here are some simple steps which resolved this issue:
(Above steps may resolve the issue, if not, follow step 3)
import com.example.myapplication.BuildConfig
(Replace "com.example.myapplication" with your package name)
That's all.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With