Gradle buildConfigField BuildConfig cannot resolve symbol

南楼画角 提交于 2019-11-28 18:14:49

问题


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 = "";
}

回答1:


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.




回答2:


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




回答3:


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:


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




回答5:


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




回答6:


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!




回答7:


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




回答8:


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!




回答9:


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.




回答10:


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.




回答11:


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.



来源:https://stackoverflow.com/questions/22604627/gradle-buildconfigfield-buildconfig-cannot-resolve-symbol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!