Gradle buildConfigField BuildConfig cannot resolve symbol
Asked Answered
M

33

147

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 = "";
}
Mi answered 24/3, 2014 at 8:47 Comment(12)
Can you check that the proper BuildConfig class was created. It'd be located in build/source/buildconfig/debug/...Belittle
I added the generated BuildConfig.java to my question.Minette
What version of the plugin are you using? With 0.9.1 I don't see this (both booleans show up in the class).Belittle
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?Minette
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.Belittle
Yes you are right, building the project is enough. It yorks perfectly now. Thank you!Minette
This happens to me as well using the latest plugin and building the project. Have you encountered this issue again ?Gumption
No, never had this issue again.Minette
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 !Cu
Thanks @XavierDucrohet. I had this error after importing from GitHub, which came down with no build folder. A Build * Rebuild resolved this.Cabbageworm
You can get proper solution via #76388303Af
In the Menu, Try Build => Make Project. For me, it was as easy as that :-)Aerosol
I
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.

Imperil answered 23/9, 2015 at 11:47 Comment(7)
how do I do this with build vairents as the full com.x.x changes?Lucic
@Lucic build variants should have no impact on this. As long as you have your variables defined on each variant, on build.gradle file.Imperil
5 years later and I owe you a big thanks. In my case it was using algoliasearch.BuildConfig without me even importing itLezlielg
Wow, also saved me some headache. In my case, it was using: com.google.firebase.BuildConfigAstrionics
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. ThanksDownspout
error: cannot find symbol jsonObject.put(PayuConstants.VERSION_KEY, BuildConfig.VERSION_NAME); I am getting this error. What I have to do exactly ?Rochellrochella
Let Luck Be With You!!Guardi
U
79

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

android {
    buildFeatures {
        buildConfig = true
    }
}
Unlive answered 14/2, 2021 at 13:37 Comment(1)
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.Styracaceous
S
53

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

Sanguinaria answered 27/8, 2014 at 2:54 Comment(4)
Where is the synchronize?Solorio
File > "Sync Project with Gradle Files".Hephzibah
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.Rood
Invalidate caches and restart didn't do it for me. I had to clean and then rebuild, and it worksMersey
G
40

In my case, in Android Studio:

  1. Build -> Clean Project;

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

  3. Build -> Rebuild Project

Grillroom answered 24/9, 2020 at 9:9 Comment(3)
This is the only solution which worked for me!Rollerskate
idk what happens.. but it worksHandal
this bug is annoyingMemorandum
M
39

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.

Made answered 19/10, 2017 at 14:0 Comment(4)
This was it for me! Thanks! Sometimes it really is those damn little things :)Underbred
Why is it necessary to escape string?Carrol
"If type is String, then value should include quotes.", see : developer.android.com/reference/tools/gradle-api/4.2/com/…Made
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!Seidler
E
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

Ethmoid answered 6/3, 2016 at 8:20 Comment(1)
Nothing else works (invalidate caches & restart; sync project files with gradle files) except Clean Project, thanksBulrush
F
24

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

./gradlew assembleDebug
Floorer answered 27/2, 2020 at 9:36 Comment(0)
B
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 :)

Braxy answered 19/10, 2016 at 9:53 Comment(2)
error: cannot find symbol jsonObject.put(PayuConstants.VERSION_KEY, BuildConfig.VERSION_NAME); I am getting this errorRochellrochella
Some of the strangest behaviors really; after this, mine worked.Trimetrogon
M
18

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

enter image description here

Money answered 14/3, 2018 at 10:31 Comment(4)
This helped me. I already had a build variant selected, but changing it caused a rebuild that made the error go away.Ibeam
Use this it wil work android{ buildFeatures{ buildConfig = true } }Brabazon
Instead of selecting build type from here I changed the name of build type to what is selected here.Micrococcus
@NithinRaja got it right, that's the oneSelie
E
14

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

thanks.

Expound answered 13/4, 2020 at 17:29 Comment(0)
H
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

Halibut answered 24/3, 2021 at 18:31 Comment(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.Lazarolazaruk
G
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.

Guttersnipe answered 26/12, 2019 at 16:38 Comment(0)
F
6
  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.

Federalese answered 14/1, 2023 at 4:9 Comment(2)
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)Rombert
Doing just: Build -> Rebuild Project, worked for me. Thanks DaxxtropezzMarquita
B
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!

Billy answered 27/7, 2015 at 20:16 Comment(0)
B
4

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
}}
Brabazon answered 27/1, 2021 at 17:23 Comment(1)
Incorrect indentation here. This answer is better: https://mcmap.net/q/157873/-gradle-buildconfigfield-buildconfig-cannot-resolve-symbolRollerskate
B
3

Use this for Android X

import androidx.multidex.BuildConfig;

it's work for me.

Bucovina answered 18/5, 2020 at 12:12 Comment(1)
Great work. This solved the issue after I migrate to androidXRigi
F
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.

Fictitious answered 7/4, 2016 at 14:47 Comment(2)
I had done some refactoring and didn't completely change all instances; this answer pointed me in the right direction. Thanks!Frizz
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.Outpatient
P
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!

Potheen answered 31/10, 2016 at 8:34 Comment(0)
E
2

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
Evangelicalism answered 6/9, 2023 at 21:15 Comment(0)
N
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.

Neocene answered 20/8, 2019 at 19:24 Comment(0)
R
1

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

or use short key : Ctrl+Shift+F9

Reeher answered 7/11, 2020 at 11:25 Comment(0)
K
1

Remove generated BuildConfig and Sync + Rebuild project

enter image description here

Kantian answered 10/5, 2023 at 12:5 Comment(2)
Think my clean had already removed the file? Rebuild was the right thing to do!Rombert
Clean only not working when add new buildConfigField, so in some cases you need make it manually.Kantian
H
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.

Haematocele answered 30/10, 2019 at 10:11 Comment(0)
I
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.

Igbo answered 25/12, 2019 at 18:12 Comment(1)
I removed one of BuildConfig.java file and then project build successfully and start debugging.Igbo
V
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

Varion answered 17/2, 2020 at 14:17 Comment(0)
K
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!

Knowledge answered 26/3, 2020 at 0:46 Comment(0)
N
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.

Necessary answered 23/8, 2021 at 15:48 Comment(0)
D
0

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

Dopey answered 14/2, 2022 at 12:27 Comment(0)
O
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

Offend answered 10/5, 2022 at 7:42 Comment(0)
D
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

Dehisce answered 15/6, 2022 at 18:23 Comment(0)
H
0

For anyone landing here after updating your package name in Android Studio, the auto-refactor misses some places that need updating. Especially if you're using React Native.

You'll want to search your entire project for the old package name (In VS Code, the default hotkey is CMD + SHIFT + F or CTRL + SHIFT + F for Mac and Windows, respectively) and update it to the new one. Keep in mind, at least some of these are case-sensitive and many instances of your project name actually need to be lowercase, so don't just auto-replace everything.

Harwilll answered 4/10, 2023 at 23:11 Comment(0)
V
0

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

Vlaminck answered 6/3 at 15:6 Comment(0)
C
-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!

Carmacarmack answered 13/1, 2021 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.