134

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 = "";
}
Gaëtan
  • 11,912
  • 7
  • 35
  • 45
  • Can you check that the proper BuildConfig class was created. It'd be located in `build/source/buildconfig/debug/...` – Xavier Ducrohet Mar 24 '14 at 15:41
  • I added the generated `BuildConfig.java` to my question. – Gaëtan Mar 24 '14 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). – Xavier Ducrohet Mar 24 '14 at 16:40
  • Yes, I am using the version 0.9.1 of the plugin. But I just noticed that if I run my application, the fields are available in `BuildConfig.java`. Is this the normal behaviour? First sync Gradle, and then run the project to have access to the new fields? – Gaëtan Mar 24 '14 at 16:48
  • 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. – Xavier Ducrohet Mar 24 '14 at 17:12
  • Yes you are right, building the project is enough. It yorks perfectly now. Thank you! – Gaëtan Mar 24 '14 at 17:45
  • This happens to me as well using the latest plugin and building the project. Have you encountered this issue again ? – Bogdan Zurac Jun 13 '14 at 09:15
  • No, never had this issue again. – Gaëtan Aug 02 '14 at 17:03
  • 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 Aug 09 '15 at 07:40
  • Thanks @XavierDucrohet. I had this error after importing from GitHub, which came down with no build folder. A Build * Rebuild resolved this. – Sam Aug 10 '21 at 05:15
  • You can get proper solution via https://stackoverflow.com/questions/76387803/buildconfig-cannot-resolve-solution – Jasani Chirag Jun 02 '23 at 07:06

30 Answers30

108

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.

MRodrigues
  • 1,927
  • 1
  • 17
  • 22
52

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

brunodles
  • 913
  • 8
  • 9
38

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.

Louis
  • 1,913
  • 2
  • 28
  • 41
  • 1
    This was it for me! Thanks! Sometimes it really is those damn little things :) – MacD Jul 09 '20 at 13:44
  • 1
    Why is it necessary to escape string? – IgorGanapolsky Aug 06 '21 at 15:46
  • 1
    "If type is String, then value should include quotes.", see : https://developer.android.com/reference/tools/gradle-api/4.2/com/android/build/api/variant/BuildConfigField#value – Louis Aug 06 '21 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! – thebiggestlebowski Aug 29 '23 at 03:47
36

In my case, in Android Studio:

  1. Build -> Clean Project;

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

  3. Build -> Rebuild Project

iminiki
  • 2,549
  • 12
  • 35
  • 45
djzhao
  • 934
  • 10
  • 9
36

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

android {
    buildFeatures {
        buildConfig = true
    }
}
shCoder
  • 35
  • 1
  • 1
  • 6
Ketan Ramani
  • 4,874
  • 37
  • 42
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

user465363
  • 531
  • 5
  • 10
  • Nothing else works (invalidate caches & restart; sync project files with gradle files) except `Clean Project`, thanks – mikep Jun 27 '22 at 08:53
24

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

./gradlew assembleDebug
svprdga
  • 2,171
  • 1
  • 28
  • 51
18

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 :)

MoranAk
  • 191
  • 1
  • 5
18

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

enter image description here

Mohammad Alotol
  • 1,409
  • 15
  • 23
13

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

thanks.

mehmoodnisar125
  • 1,469
  • 18
  • 14
13

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

Max
  • 183
  • 2
  • 8
  • 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 Jan 04 '22 at 08:24
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.

Ankit Kante
  • 2,029
  • 1
  • 19
  • 41
tshele litabe
  • 126
  • 1
  • 3
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!

Litome
  • 653
  • 8
  • 12
3

Use this for Android X

import androidx.multidex.BuildConfig;

it's work for me.

3
  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.

  • 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) – FrankKrumnow Aug 29 '23 at 14:16
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.

Renan Ferrari
  • 2,449
  • 5
  • 21
  • 26
  • I had done some refactoring and didn't completely change all instances; this answer pointed me in the right direction. Thanks! – Cezille07 Apr 22 '19 at 06: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 May 28 '20 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!

RRK
  • 375
  • 3
  • 8
2

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
}}
Nithin Raja
  • 1,144
  • 12
  • 11
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.

Jack'
  • 1,722
  • 1
  • 19
  • 27
1

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

or use short key : Ctrl+Shift+F9

Ali Rezaiyan
  • 3,011
  • 3
  • 16
  • 27
1

Remove generated BuildConfig and Sync + Rebuild project

enter image description here

Houssin Boulla
  • 2,687
  • 1
  • 16
  • 22
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.

Draško
  • 2,119
  • 4
  • 41
  • 73
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.

Alok Kumar
  • 581
  • 1
  • 8
  • 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

Community
  • 1
  • 1
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
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!

fattire
  • 6,823
  • 3
  • 25
  • 38
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.

Yohanim
  • 3,319
  • 9
  • 52
  • 92
0

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

ouflak
  • 2,458
  • 10
  • 44
  • 49
ArunLa
  • 1
  • 1
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

BenjaBoy
  • 454
  • 3
  • 16
0

Make sure that your package name matches your app id in Build.Gradle file

Just Change it & Sync, it is going to do the necessary import for you.

Good luck

-3

I would like to add another solution for this issue. I've tried every answer on this post but nobody mentioned to reboot their machine right after you updated your system variables. Works like a charm!

Jim Ovejera
  • 739
  • 6
  • 10