Reputation: 1033
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.
Upvotes: 72
Views: 45295
Reputation: 1430
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 Deprecatedandroid.defaults.buildfeatures.buildconfig=true
in your gradle.properties
file
Upvotes: 78
Reputation: 500
I got this solved with mismatching package-name
and the namespace
along with applicationId
in /android/app/build.gradle
Make sure that you change the following in /android/app/build.gradle
{
.....
android {
namespace "enter.your.package.name.here"
defaultConfig {
applicationId "enter.your.package.name.here"
......
}
Upvotes: 0
Reputation: 32472
My project is a React Naive one. Its version is 0.75.3
and in this version, Kotlin got maintained. In my case solution was consistent with namespace
and the imported package in the MainApplication.kt
and MainActivity.kt
, pay attention to the following:
// app/build.gradle
namespace "co.uk.example" // this one
defaultConfig {
// MainApplication.kt
package co.uk.exmaple // <== this one
import android.app.Application
// MainActivity.kt
package co.uk.example // <== this one
import android.app.Application
All of the above three must be exactly the same
Upvotes: 1
Reputation: 11
you shoulld clean the project and rebuild in debug mode
cd android
./gradlew clean
./gradlew assembledebug
then you can find BuildConfig.java in android/app/build/generated/source/buildConfig///BuildConfig.java
Upvotes: 1
Reputation: 31
Add this line to your gradle.properties file
android.defaults.buildfeatures.buildconfig=false
...Note that the BuildConfig
should be set to false
for the current gradle/kotlin
version. (...BuildCofig=true
) is deprecated.
Upvotes: 2
Reputation: 3844
If you are in 2023 or 2024 and having this issue, add
android.defaults.buildfeatures.buildconfig=true
to your gradle.properties
file.
If you want more granular control, as in enabling this for specific modules, add this:
android {
...
buildFeatures {
buildConfig = true
}
}
to every build.gradle.kts
file you feel necessary.
Upvotes: 145
Reputation: 9282
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.
Upvotes: 2
Reputation: 41
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
Upvotes: 3
Reputation: 737
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.
Upvotes: 14
Reputation: 51
I had this issue for the last 3 weeks and all of the suggestions didn't work.
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.
Upvotes: 0
Reputation: 344
As of Android Studio 3.5, BuildConfig.APPLICATION_ID
is deprecated and replaced with BuildConfig.LIBRARY_PACKAGE_NAME
Alternatively you can use Context.getPackageName()
Upvotes: 0
Reputation: 417
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"
}
...
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 6810
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.
Upvotes: 0
Reputation: 53
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.
Upvotes: 1
Reputation: 6169
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.
Upvotes: 35
Reputation: 75778
unresolved reference
build variant
. Like DEBUG
mode.Clean-Rebuild-Restart
IDE.Don't
buildConfigField("String", "MODE", "FREE")
Do
buildConfigField("String", 'MODE', '"FREE"')
Upvotes: 13