29

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.

Edric
  • 24,639
  • 13
  • 81
  • 91
MaChee Neraid
  • 603
  • 1
  • 7
  • 17

11 Answers11

48

If you are in 2023 and having this issue, add

android.defaults.buildfeatures.buildconfig=true

to your gradle.properties file.

Ivan Morgillo
  • 2,837
  • 4
  • 30
  • 46
  • 4
    Blog post about this: https://medium.com/androiddevelopers/5-ways-to-prepare-your-app-build-for-android-studio-flamingo-release-da34616bb946 – JonasVautherin May 24 '23 at 11:50
22

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.

Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46
  • 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. – yuroyami Feb 11 '23 at 15:21
18

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

eugeneek
  • 812
  • 9
  • 24
11

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

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
5

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.

Michal
  • 15,429
  • 10
  • 73
  • 104
neo
  • 618
  • 1
  • 10
  • 29
0

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.

MrVanes
  • 43
  • 6
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.

user496854
  • 6,461
  • 10
  • 47
  • 84
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.

  • 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](/help/how-to-answer). – Community Nov 01 '22 at 02:13
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"
}
...
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()

Sergey
  • 324
  • 4
  • 9
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.

IeNary
  • 21
  • 3