160

I am using Gradle to build my Android application. I am trying to use some flags based on the build type (release or debug).

My Gradle file looks like this:

android {
    buildTypes {
        debug {
            buildConfigField 'boolean', 'PREPROD', 'true'
            buildConfigField 'boolean', 'STAGING', 'false'
        }

        release {
            buildConfigField 'boolean', 'PREPROD', 'false'
            buildConfigField 'boolean', 'STAGING', 'false'
        }
    }
}

And if I try to call BuildConfig.PREPROD or BuildConfig.STAGING I get a "Cannot resolve symbol" error. The Gradle sync was successful, so I don't know if I forgot some steps in order to be able to use this feature?

The generated BuildConfig.java file is the following (in build/source/buildConfig/debug/com.example.myapp):

package com.example.myapp;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String PACKAGE_NAME = "com.example.myapp";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 400;
  public static final String VERSION_NAME = "";
}
12
  • Can you check that the proper BuildConfig class was created. It'd be located in build/source/buildconfig/debug/... Commented Mar 24, 2014 at 15:41
  • I added the generated BuildConfig.java to my question.
    – Gaëtan
    Commented Mar 24, 2014 at 15:56
  • What version of the plugin are you using? With 0.9.1 I don't see this (both booleans show up in the class). Commented Mar 24, 2014 at 16:40
  • 4
    You shouldn't have to sync (though Studio will ask you to do it because it doesn't know if you changed the model), but you will need to build, since the BuildConfig class is generated during the build based on what's in the build.gradle file. Commented Mar 24, 2014 at 17:12
  • 2
    Hi, I managing two flavour in application via config settings and like you I'm trying to create some config variable but for different flavour. I tried your way but not seeing any update in flavour/BuildConfig.java and because of that could not able to access variable even in Compile time. Any suggestion !
    – CoDe
    Commented Aug 9, 2015 at 7:40

34 Answers 34

111

Make sure your file is importing the right BuildConfig class. Sometimes if you have other project libraries or modules, it may import the wrong buildConfig. Make sure your import it's like this com.project.app.BuildConfig. I was Having the same issue and the problem was this, Hope it can help somebody.

7
  • how do I do this with build vairents as the full com.x.x changes? Commented Oct 11, 2018 at 11:36
  • @Zapnologica build variants should have no impact on this. As long as you have your variables defined on each variant, on build.gradle file.
    – MRodrigues
    Commented Oct 12, 2018 at 8:09
  • 5 years later and I owe you a big thanks. In my case it was using algoliasearch.BuildConfig without me even importing it
    – dustytrash
    Commented Jan 25, 2020 at 8:21
  • Wow, also saved me some headache. In my case, it was using: com.google.firebase.BuildConfig Commented Mar 12, 2020 at 15:13
  • Worked for me like a charm, had changed my package name and that is what caused the problem. Updating the import to match package name then adding .BuildConfig fixed it for me. Thanks
    – cherucole
    Commented Mar 13, 2020 at 8:01
99

Add below in build.gradle(Module:App) and Rebuild Project

android {
    buildFeatures {
        buildConfig = true
    }
}
3
  • 9
    who at Google keeps changing fundamentals and not telling anyone?! The worst part is the modern era build tools will still generate BuildConfig files, it just isn't visible to your source code for no reason without this flag smdh.
    – CCJ
    Commented Dec 6, 2023 at 2:53
  • I have a fully generated BuildConfig with the 'missing' fields after updating Gradle to 8, and this solution still doesn't work to make those fields visible in a running app, even after cleaning and invalidating. I'm really not sure what causes the generated file to not be copied into the apk.
    – Kabliz
    Commented May 31 at 0:10
  • So, You must Have to hit on google search bar ! excellent trick ! Commented Jul 11 at 15:44
53

I was getting the same bug. Try to click on synchronize, on the right side of save. Do that after gradle sync.

4
  • 10
    Where is the synchronize?
    – Lucky_girl
    Commented Feb 2, 2018 at 17:29
  • 2
    File > "Sync Project with Gradle Files". Commented Jan 8, 2021 at 21:13
  • 1
    Thank you sir! Although it wasn't the synchronize button that solve my issue, i clicked Invalidate Caches / Restart and everything goes back to normal.
    – Keith Mak
    Commented Apr 29, 2021 at 4:18
  • 2
    Invalidate caches and restart didn't do it for me. I had to clean and then rebuild, and it works
    – SaPropper
    Commented Oct 17, 2021 at 20:25
43

In my case, in Android Studio:

  1. Build -> Clean Project;

  2. File -> Invalidate Caches / Restart...

  3. Build -> Rebuild Project

3
  • 1
    This is the only solution which worked for me! Commented May 18, 2021 at 8:58
  • 1
    idk what happens.. but it works
    – amerw
    Commented Nov 4, 2022 at 2:41
  • 1
    this bug is annoying
    – EugeneS
    Commented Nov 8, 2023 at 13:06
40

Happened to me because I did not declared a String field properly.

I forgot the escaping characters. Changing from :

buildConfigField "String", "FOO", "foo"

to

buildConfigField "String", "FOO", "\"foo\""

solved the problem.

4
  • 1
    This was it for me! Thanks! Sometimes it really is those damn little things :)
    – MacD
    Commented Jul 9, 2020 at 13:44
  • 1
    Why is it necessary to escape string? Commented Aug 6, 2021 at 15:46
  • 1
    "If type is String, then value should include quotes.", see : developer.android.com/reference/tools/gradle-api/4.2/com/…
    – Louis
    Commented Aug 6, 2021 at 15:55
  • That wasn't my problem, but knowing that this came from a configuration file somewhere was enough of a hint to figure it out. I searched all files for the property and found it. Thanks! Commented Aug 29, 2023 at 3:47
32

Changes in gradle build files don't refresh the generation of the BuildConfig class, even if you click the yellow "sync now" bar at the top. A full clean will force generating this file.

In Android Studio: Build -> Clean Project

1
  • Nothing else works (invalidate caches & restart; sync project files with gradle files) except Clean Project, thanks
    – mikep
    Commented Jun 27, 2022 at 8:53
24

Same happened to me, fixed when running this in the project root:

./gradlew assembleDebug
0
20

Make sure you add your parameter also to the defaultConfig. You are probably running the default buildVarient while your parameter is defined in a speciffic buildVariant.

in the build gradle file use this:

 defaultConfig {
        buildConfigField('String' , 'myParameter', 'someValue')
    }

Then, in the code use this:

String myParam= BuildConfig.myParameter;

Hope this helps, M.A :)

2
  • error: cannot find symbol jsonObject.put(PayuConstants.VERSION_KEY, BuildConfig.VERSION_NAME); I am getting this error Commented May 10, 2021 at 11:55
  • Some of the strangest behaviors really; after this, mine worked. Commented Sep 23, 2023 at 19:05
18

You have to choose the desired build variant first to be able to access its buildConfigField

enter image description here

4
  • This helped me. I already had a build variant selected, but changing it caused a rebuild that made the error go away.
    – LarsH
    Commented Mar 3, 2020 at 14:54
  • 1
    Use this it wil work android{ buildFeatures{ buildConfig = true } } Commented Jan 27, 2021 at 17:27
  • 1
    Instead of selecting build type from here I changed the name of build type to what is selected here. Commented Feb 23, 2021 at 22:22
  • @NithinRaja got it right, that's the one
    – Odaym
    Commented Dec 26, 2023 at 0:10
14

if this type of problem happens just clean and rebuild you project.

thanks.

0
14

I tried everything here and nothing worked for me. I initially had an issue that one of the methods of BuildConfig that I was referring to wasn't being detected, so I tried some of the solutions here. That only broke my project even more (or so I thought). I tried changing my build type and ended up having the problem the OP mentioned (which was a problem I did not have. I came here looking for solution to another problem and ended up creating the OPs problem for myself facepalm).

So the solution that fixed things for me was, just go to

Build > Make Module 'app'.

And regardless of how many red mark indications I had, it just fixed everything for me. I wasn't even able to import the BuildConfig of my project after I broke it by trying the solutions here, to a problem I did not have (I'm dumb yea, idk what I'm doing). I think doing a clean build broke things further. But this solved everything for me.

Source: https://tinaciousdesign.com/blog/buildconfig-debug-always-returns-false-android/

TLDR: BuildConfig is a generated class in your project. If Android Studio can’t find it, it’ll prompt you to import it. Don’t import it or you’ll get the lint warning. Run the project as usual, ignoring the red. Your problem should be solved

1
  • Thanks bro. None of other answers worked for me. Im using Using flutter and i need to set a flavor for that import is not working for me.
    – SoloWolf93
    Commented Jan 4, 2022 at 8:24
9

If Build config is not getting resolved in build Gradle file use the below code and sync it will work as expected

android{
buildFeatures{
    buildConfig = true
}}
1
8
  1. Build -> Clean Project;
  2. File -> Invalidate Caches / Restart
  3. Build -> Rebuild Project
  4. Sync Gradle Project with Project Files

This one worked for me.

2
  • 1
    Over 9 years and a multitude versions of android and android studio this terrible error still persists. And this seems to be the only fix. (maybe just rebuild may work sometimes) Commented Aug 29, 2023 at 14:16
  • Doing just: Build -> Rebuild Project, worked for me. Thanks Daxxtropezz
    – Sunny Jha
    Commented Nov 12, 2023 at 16:30
8

Add the below line in gradle.properties file it will be work very fine when you update your project with Android 13:

android.defaults.buildfeatures.buildconfig=true
2
  • Deprecated in Android Studio Iguana.
    – FractalBob
    Commented May 20 at 6:00
  • This worked out for me, Thanks Lots
    – Tonnie
    Commented Jul 28 at 9:34
7

In Android Studio, - click Build then Clean Project - After cleaning click Rebuild Project. - When done, close your project. - Open your project again and run it, the error should not appear again, it has cleared.

5

This same problem has been driving me nuts for over a week. In my case, it was a simple import missing. I looked for it but somehow nowhere in the docs I could find does it mention that you need to import the BuildConfig class anywhere! And yet, it's logical. I went on believing it might be automatic. Well, it ISN'T.

So try either:

  • click once on the BuildConfig. part of code that causes the error. A help message should appear in a blue bubble saying something like: "? uk.co.package.app.BuildConfig? Alt + ENTER" Click on it and Android studio automatically adds the missing import. or
  • add import uk.co.package.app.BuildConfig; somewhere in the list of your imports.

re-build... it works! well, it did for me anyway.

Hope that helps another gradle android newbie like me!

3

Use this for Android X

import androidx.multidex.BuildConfig;

it's work for me.

1
  • 1
    Great work. This solved the issue after I migrate to androidX Commented Mar 16, 2021 at 11:36
2

In my case, the problem was that I had just renamed a module of my project, but that module's name references weren't automatically updated on the AndroidManifest.xml.

Manually renaming those and rebuilding the project solved the issue.

2
  • I had done some refactoring and didn't completely change all instances; this answer pointed me in the right direction. Thanks!
    – Cezille07
    Commented Apr 22, 2019 at 6:14
  • same issue. tried cleaning the project. restarting android studion. nothing worked the error was still there. Then i cleaned it one more time (Build > Clean Project) then i run debug by force. it was successful after the build and the error disappeared.
    – Chief
    Commented May 28, 2020 at 12:29
2

Search for occurrences of BuildConfig. I had a rogue import of non-existant BuildConfig and the compiler instead of catching that pointed at a random line of code somewhere else!

0
2

Remove generated BuildConfig and Sync + Rebuild project

enter image description here

2
  • Think my clean had already removed the file? Rebuild was the right thing to do! Commented Jun 6, 2023 at 8:08
  • Clean only not working when add new buildConfigField, so in some cases you need make it manually. Commented Jun 7, 2023 at 7:32
1

My package wasn't up to date in my manifest. I checked the file AndroidManifest.xml and corrected the name of my package. This is how i solved this problem.

1

Rebuild your module by right clicking on your module and select the Rebuild Module 'module name'

or use short key : Ctrl+Shift+F9

1

I tried all the solutions but nothing worked for me. So, I did some trial and error and found a solution for myself.

Build -> Recompile 'FileName.kt'

Voila, that solved my problem! It created and imported the BuildConfig class inside the FileName.kt

0

My 2 cents:

I had a correct import xxx.BuildConfig, AS would point to that line and to those lines where I had BuildConfig.SOME_VARIABLE, so I removed import line, rebuilt it, got an error saying that BuildConfig is unrecognised reference blah blah and after that I imported it and rebuilt it again.

0

I encountered same error during build(debugging) the old project in Android studio. When i investigated error i found that BuildConfig class was defined in 2 files. 1st was in BuildConfig.java and 2nd was in BuildConfig2.java.

I removed the one of file and i worked correctly.

1
  • I removed one of BuildConfig.java file and then project build successfully and start debugging.
    – Alok Kumar
    Commented Dec 25, 2019 at 18:13
0

In my case, I did a stupid mistake by COPYING AND PASTING THE SAME VARIANT. I just renamed the other variant and it worked.

enter image description here

0

Possible solution: If you're using the Android X billing library, perhaps by adding the following line in your build.gradle file:

implementation 'com.android.billingclient:billing:2.2.0'

Be aware that the Android billing library ALSO includes its own BuildConfig class which can confuse Android Studio's code editor. If this happens, it may auto-add the following import statement to one of your classes:

import com.android.billingclient.BuildConfig;

That's not the one you want to use, right? The one you want to be using (if you were doing a debug build) might be here:

./build/generated/source/buildConfig/debug/com/example/myapp/BuildConfig.java

So to fix, remove that import statement line, and rebuild to see if it can resolve to the right BuildConfig.java. If it can't, you may need to explicitly name it via an import com.example.myapp.BuildConfig.java to make it super clear you want the version of this file from your package and not any other.

Hope this helps!

0

In my case, the problem comes from

import com.android.volley.BuildConfig;

Make sure you have import the right BuildConfig from your package not from volley.

0

Check once your file is importing the correct BuildConfig class. If it is correct, then clean or File->InvalidateCaches and Restart.

0

In my case it happens after I added one extra level/folder to my package, Changed my com.xxx.BuildConfig to com.xxx.mynewfoldername.BuildConfig then works fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.