Android Gradle buildConfigField BuildConfig 无法解析符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22604627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:14:23  来源:igfitidea点击:

Gradle buildConfigField BuildConfig cannot resolve symbol

androidgradlebuildconfig

提问by Ga?tan

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

我正在使用 Gradle 构建我的 Android 应用程序。我正在尝试根据构建类型(发布或调试)使用一些标志。

My Gradle file looks like this:

我的 Gradle 文件如下所示:

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.PREPRODor BuildConfig.STAGINGI 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?

如果我尝试打电话BuildConfig.PREPRODBuildConfig.STAGING收到“无法解析符号”错误。Gradle同步成功了,不知道是不是忘记了一些步骤才能使用这个功能?

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

生成的BuildConfig.java文件如下(在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 = "";
}

回答by MRodrigues

Make sure your file is importing the right BuildConfigclass. 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.

确保您的文件正在导入正确的BuildConfig类。有时,如果您有其他项目库或模块,则可能会导入错误的 buildConfig。确保您的导入类似于com.project.app.BuildConfig。我遇到了同样的问题,问题是这样的,希望它可以帮助某人。

回答by brunodles

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

我遇到了同样的错误。尝试单击保存右侧的同步。在gradle sync之后执行此操作

回答by Louis

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

发生在我身上是因为我没有正确声明 String 字段。

I forgot the escaping characters. Changing from :

我忘记了转义字符。更改自:

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

to

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

solved the problem.

解决了这个问题。

回答by user465363

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.

gradle 构建文件中的更改不会刷新 BuildConfig 类的生成,即使您单击顶部的黄色“立即同步”栏也是如此。完全清理将强制生成此文件。

In Android Studio: Build -> Clean Project

在 Android Studio 中:构建 -> 清理项目

回答by MoranAk

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.

确保您也将参数添加到 defaultConfig。您可能正在运行默认的 buildVariant,而您的参数是在特定的 buildVariant 中定义的。

in the build gradle file use this:

在构建 gradle 文件中使用这个:

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

Then, in the code use this:

然后,在代码中使用这个:

String myParam= BuildConfig.myParameter;
String myParam= BuildConfig.myParameter;

Hope this helps, M.A :)

希望这会有所帮助,妈妈 :)

回答by svprdga

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

我也发生了同样的情况,在项目根目录中运行时修复:

./gradlew assembleDebug

回答by Mohammad Alotol

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

您必须首先选择所需的构建变体才能访问其 buildConfigField

enter image description here

在此处输入图片说明

回答by Litome

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.

同样的问题已经让我发疯了一个多星期。就我而言,这是一个简单的导入丢失。我寻找了它,但不知何故在我找不到的文档中没有提到您需要在任何地方导入 BuildConfig 类!然而,这是合乎逻辑的。我继续相信它可能是自动的。好吧,它不是。

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.
  • BuildConfig.导致错误的代码部分单击一次。帮助消息应出现在蓝色气泡中,内容如下:“ ? uk.co.package.app.BuildConfig? Alt + ENTER” 单击它,Android Studio 会自动添加缺少的导入。或者
  • import uk.co.package.app.BuildConfig;在您的导入列表中添加某处。

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

重新构建...它的工作原理!好吧,无论如何它对我有用。

Hope that helps another gradle android newbie like me!

希望能帮助像我这样的另一个 gradle android 新手!

回答by Renan Ferrari

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.

就我而言,问题是我刚刚重命名了项目的一个模块,但该模块的名称引用并未在AndroidManifest.xml.

Manually renaming those and rebuilding the project solved the issue.

手动重命名这些并重建项目解决了这个问题。

回答by RRK

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!

搜索 BuildConfig 的出现。我有一个不存在的 BuildConfig 和编译器的流氓导入,而不是捕获指向其他地方的随机代码行!