Gradle buildConfigField BuildConfig cannot resolve character

I am using Gradle to create an Android application. I am trying to use some flags based on the type of assembly (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 the error "Cannot resolve character". Gradle sync was successful, so I don’t know if I forgot some steps to be able to use this function?

The generated BuildConfig.java file is BuildConfig.java follows (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 = ""; } 
+65
android gradle
Mar 24 '14 at 8:47
source share
10 answers

Make sure your file imports the correct BuildConfig class. Sometimes, if you have other project libraries or modules, it may import the wrong buildConfig. Make sure you import it as com.project.app.BuildConfig . I had the same problem and the problem was this, hope this can help someone.

+65
Sep 23 '15 at 11:47
source share

I was getting the same error. Try clicking sync , save on the right side. Do this after gradle synchronization .

+40
Aug 27 '14 at 2:54 on
source share

It happened to me because I did not declare the string field correctly.

I forgot about the screening characters. Change:

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

to

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

solved a problem.

+17
19 Oct '17 at 14:00
source share

Make sure you add your parameter also to the default Config file. You are probably using defaultVarient by default, while your parameter is defined in specisticic buildVariant.

in the build gradle file use this:

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

Then in code use this:

 String myParam= BuildConfig.myParameter; 

Hope this helps, MA :)

+13
Oct 19 '16 at 9:53 on
source share

Changes to gradle build files do not update the generation of the BuildConfig class, even if you click the yellow "sync now" panel at the top. A full cleanup will create this file.

In Android Studio: Build -> Clean Project

+7
Mar 06 '16 at 8:20
source share

The same problem forced me to play for more than a week. In my case, it was a simple import. I was looking for it, but somehow nowhere in the documents that I could find, it mentions that you need to import the BuildConfig class anywhere! And yet, this is logical. I continued to believe that this could be automatic. Well, this is NOT.

So try either:

  • click once on the BuildConfig. part BuildConfig. code that causes the error. A help message should appear in the blue bubble: " ? uk.co.package.app.BuildConfig? Alt + ENTER " Click on it, and Android Studio will automatically add the missing import. or
  • add import uk.co.package.app.BuildConfig; somewhere on your import list.

rebuild ... it works! well, it's all the same to me.

Hope this helps another novice novice novice like w140.

+5
Jul 27 '15 at 20:16
source share

You must first select the correct build option to access its buildConfigField

enter image description here

+4
Mar 14 '18 at 10:31
source share

Search for an entry of BuildConfig. I had a creepy import of a nonexistent BuildConfig and compiler instead of catching, which was pointing to a random line of code somewhere else!

+2
31 Oct '16 at 8:34
source share

In my case, the problem was that I just renamed the module of my project, but the links to the module name were not automatically updated on AndroidManifest.xml .

Manually renaming them and restoring the project solved the problem.

+1
Apr 7 '16 at 14:47
source share

My package was not relevant in my manifest. I checked the AndroidManifest.xml file and fixed the name of my package. This is how I solved this problem.

0
Aug 20 '19 at 19:24
source share



All Articles