BuildConfig 未正确创建(Gradle Android)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20706451/
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
BuildConfig not getting created correctly (Gradle Android)
提问by Innova
I am trying to convert our Android application to a gradle build. I have the project and it's libraries building successfully. I am now trying to create separate apks for our various environments (dev/test/prod have different urls for the restful services they consume).
我正在尝试将我们的 Android 应用程序转换为 gradle 版本。我有这个项目,它的库构建成功。我现在正在尝试为我们的各种环境创建单独的 apk(dev/test/prod 对它们使用的宁静服务有不同的 url)。
In searching around, the best way that I feel to do this is with making different BuildConfig for each environment. This is what I tried:
在四处寻找时,我觉得最好的方法是为每个环境制作不同的 BuildConfig。这是我尝试过的:
import java.util.regex.Pattern
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:+'
}
}
apply plugin: 'android'
task('increaseVersionCode') << {
def manifestFile = file("AndroidManifest.xml")
def pattern = Pattern.compile("versionCode=\"(\d+)\"")
def manifestText = manifestFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
def manifestContent = matcher.replaceAll("versionCode=\"" + ++versionCode + "\"")
manifestFile.write(manifestContent)
}
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig') {
task.dependsOn 'increaseVersionCode'
}
}
dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile files('libs/commons-io-2.4.jar',
'libs/google-play-services.jar',
'libs/gson-2.2.4.jar',
'libs/universal-image-loader-1.8.6.jar',
'libs/wakeful-1.0.1.jar')
compile project(':pulltorefresh_lib')
compile project(':edgeeffect_lib')
compile project(':viewpagerindicator_lib')
}
android {
buildToolsVersion "18.1.1"
compileSdkVersion "Google Inc.:Google APIs:18"
defaultConfig {
minSdkVersion 14
targetSdkVersion 18
}
buildTypes {
debug {
packageNameSuffix ".debug"
}
dev.initWith(buildTypes.debug)
dev {
buildConfigField "String", "URL_SEARCH", "\"https://dev-search.example.com\";"
buildConfigField "String", "URL_CONNECT", "\"https://dev-connect.example.com\";"
buildConfigField "String", "URL_SVC_NEWSLIST", "\"https://dev-mobilenews.example.com/newslist\";"
buildConfigField "String", "URL_SVC_NEWSDETAIL", "\"https://dev-mobilenews.example.com/newsdetail\";"
buildConfigField "String", "URL_SVC_REGISTERENDPOINTS", "\"https://dev-mobilenews.example.com/registerendpoints\";"
}
prod.initWith(buildTypes.release)
prod {
buildConfigField "String", "URL_SEARCH", "\"https://search.example.com\";"
buildConfigField "String", "URL_CONNECT", "\"https://connect.example.com\";"
buildConfigField "String", "URL_SVC_NEWSLIST", "\"https://mobilenews.example.com/newslist\";"
buildConfigField "String", "URL_SVC_NEWSDETAIL", "\"https://mobilenews.example.com/newsdetail\";"
buildConfigField "String", "URL_SVC_REGISTERENDPOINTS", "\"https://mobilenews.pdc-np-cf.lmig.com/registerendpoints\";"
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
The problem is that my BuildConfig.java doesn't seem to get the static variables injected, therefore I get errors similar to:
问题是我的 BuildConfig.java 似乎没有注入静态变量,因此我收到类似于以下内容的错误:
/Users/path/to/project/MainActivity.java:348: error: cannot find symbol
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(BuildConfig.URL_SEARCH)));
^
symbol: variable URL_SEARCH
location: class BuildConfig
/Users/path/to/project/MainActivity.java:359: error: cannot find symbol
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(BuildConfig.URL_CONNECT)));
^
symbol: variable URL_CONNECT
location: class BuildConfig
/Users/path/to/project/MainActivity.java:600: error: cannot find symbol
HttpPost httpPost = new HttpPost(BuildConfig.URL_SVC_REGISTERENDPOINTS);
^
symbol: variable URL_SVC_REGISTERENDPOINTS
location: class BuildConfig
/Users/path/to/project/service/AlarmNotificationService.java:145: error: cannot find symbol
String requestUrl = BuildConfig.URL_SVC_NEWSLIST + "?"
^
symbol: variable URL_SVC_NEWSLIST
location: class BuildConfig
/Users/path/to/project/service/NewsService.java:240: error: cannot find symbol
String requestUrl = BuildConfig.URL_SVC_NEWSLIST + "?"
^
symbol: variable URL_SVC_NEWSLIST
location: class BuildConfig
/Users/path/to/project/service/NewsService.java:530: error: cannot find symbol
HttpPost httpPost = new HttpPost(BuildConfig.URL_SVC_NEWSDETAIL);
^
symbol: variable URL_SVC_NEWSDETAIL
location: class BuildConfig
6 errors
My build/source/buildConfig/debug/com/.../BuildConfig.java file contains:
我的 build/source/buildConfig/debug/com/.../BuildConfig.java 文件包含:
/**
* Automatically generated file. DO NOT MODIFY
*/
package com....;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String PACKAGE_NAME = "com.....debug";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 5;
}
What am I doing wrong?
我究竟做错了什么?
回答by Peter Knut
Please, be sure that you are building "dev" or "prod" variant. There is no BuildConfig definition in default "debug" and "release" variant. In Android Studio, you can select current variant in bottom left corner:
请确保您正在构建“dev”或“prod”变体。默认的“调试”和“发布”变体中没有 BuildConfig 定义。在 Android Studio 中,您可以在左下角选择当前变体:
To simplify your build.gradle file, you can define:
为了简化你的 build.gradle 文件,你可以定义:
buildTypes {
debug {
buildConfigField "String", "URL_SEARCH", "\"https://dev-search.example.com\""
// etc.
}
release {
buildConfigField "String", "URL_SEARCH", "\"https://search.example.com\""
// etc.
}
}
and then just use default "debug" and "release" variants.
然后只使用默认的“调试”和“发布”变体。
At last, delete semicolon (sign: ';') from the value of buildConfigField parameter.
最后,从 buildConfigField 参数的值中删除分号(符号:';')。
回答by savepopulation
I had same issue and fixed it like below:
我有同样的问题并修复它如下:
buildConfigField 'String', 'BASE_URL', '"https://api.example.com"'
回答by Litome
Just in case that helps somebody else, in my case it was a missing import:
import uk.co.yourpackage.yourapp.BuildConfig;
以防万一对其他人有帮助,就我而言,这是一个缺失的导入:
import uk.co.yourpackage.yourapp.BuildConfig;
Somehow, nowhere in the doc does it mention you need that include! Made me think it was automatically imported somehow but it ISN'T. not for me at least... So much time lost... Hope that helps another newbie like me!
不知何故,文档中没有任何地方提到您需要包含!让我认为它是以某种方式自动导入的,但它不是。至少不适合我......浪费了很多时间......希望能帮助像我这样的新手!
回答by Mirza Ahmed Baig
first do
先做
File -> Invalidate Caches/Restart... -> Invalidate and Restart
then do
然后做
Build -> Clean Project
then do
然后做
Build - > Rebuild Project
回答by bartonstanley
Here is what fixed this for me:
这是为我解决的问题:
File -> Invalidate Caches/Restart... -> Invalidate and Restart
回答by chisom onwuegbuzie
In the project.ext.envConfigFiles array, ensure you set the debug to the right .env file you are using for development
在 project.ext.envConfigFiles 数组中,确保将调试设置为用于开发的正确 .env 文件
回答by Jmz
If you are modifying your Environment Variables, and they are not reflecting correctly in Android studio try doing:
如果您正在修改环境变量,并且它们在 Android Studio 中没有正确反映,请尝试执行以下操作:
Build -> Clean Project
File -> Invalidate Caches/Restart... -> Invalidate and Restart
回答by straya
I had similar problem related to build types being setup using .initWith(someotherbuildtype), BuildConfig was not being created properly. I had to switch to the parent build variant and build that first, then the build types that initWith the parent built fine.
我遇到了与使用 .initWith(someotherbuildtype) 设置构建类型相关的类似问题,未正确创建 BuildConfig。我必须切换到父构建变体并首先构建它,然后是 initWith 父构建好的构建类型。
回答by Rifat
Try to do
试着做
Build -> Clean Project
构建 -> 清理项目
then do
然后做
Build - > Rebuild Project
构建 -> 重建项目
If it doesn't work, try
如果它不起作用,请尝试
File -> Invalidate Caches/Restart... -> Invalidate and Restart
File -> Invalidate Caches/Restart... -> Invalidate and Restart
then do
然后做
Build -> Clean Project
构建 -> 清理项目
then do
然后做
Build - > Rebuild Project
构建 -> 重建项目