BuildConfig未正确创build(Gradle Android)

我试图将我们的Android应用程序转换为gradle构build。 我有这个项目,它的图书馆build设成功。 我现在试图为我们的各种环境创build单独的apks(dev / test / prod拥有不同的url,用于他们所使用的restful服务)。

在四处search时,我觉得要做到这一点的最好方法是为每个环境制作不同的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'] } } } 

问题是我的BuildConfig.java似乎没有得到静态variables注入,因此我得到的错误类似于:

 /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 

我的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; } 

我究竟做错了什么?

请确保您正在构build“dev”或“prod”变体。 在默认的“debug”和“release”版本中没有BuildConfig定义。 在Android Studio中,您可以select左下angular的当前变体:

建立变体

为了简化你的build.gradle文件,你可以定义:

 buildTypes { debug { buildConfigField "String", "URL_SEARCH", "\"https://dev-search.example.com\"" // etc. } release { buildConfigField "String", "URL_SEARCH", "\"https://search.example.com\"" // etc. } } 

然后使用默认的“debugging”和“发布”变体。

最后,从buildConfigField参数的值中删除分号(符号:';')。

我有同样的问题,并修复它如下所示:

 buildConfigField 'String', 'BASE_URL', '"https://api.example.com"' 

以防万一,帮助别人,在我的情况下,这是一个缺less的import: import uk.co.yourpackage.yourapp.BuildConfig;

不知何故,在文档中没有提到你需要包括! 让我觉得它被自动导入某种方式,但它不是。 至less不是对我来说…这么多的时间输了…希望能帮助像我这样的新手!

我有类似的问题与build立使用.initWith(someotherbuildtype)build立types,BuildConfig没有被正确创build。 我不得不切换到父构build变体,并首先构build,然后构build父类的initWith生成types罚款。