如何生成字符串类型的buildConfigField

在我的Android Studio项目中,有两个build configuration和一些buildConfigField:

    buildTypes {
def SERVER_URL = "SERVER_URL"
def APP_VERSION = "APP_VERSION"


debug {
buildConfigField "String", SERVER_URL, "http://dev.myserver.com"
buildConfigField "String", APP_VERSION, "0.0.1"
}


release {
buildConfigField "String", SERVER_URL, "https://myserver.com"
buildConfigField "String", APP_VERSION, "0.0.1"


minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

我得到的错误如下:

/path/to/generated/BuildConfig.java
Error:(14, 47) error: ';' expected
Error:(15, 47) error: ';' expected

生成的BuildConfig.java如下所示:

public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.mycuteoffice.mcoapp";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Fields from build type: debug
public static final String APP_VERSION = 0.0.1;
public static final String SERVER_URL = http://dev.mycuteoffice.com;
}

我认为APP_VERSIONSERVER_URL没有被正确生成为字符串类型,它们没有引号。

我不确定它为什么会以这样的方式产生。请让我知道我如何解决这个问题。

116873 次浏览

字符串类型的构建配置字段应该这样声明:

buildConfigField "String", "SERVER_URL", "\"http://dev.myserver.com\""

字段名加引号,字段值加转义引号。

转义你的字符串引号:

buildConfigField "String", 'SERVER_URL', "\"http://dev.myserver.com\""
buildConfigField "String", 'APP_VERSION', "\"0.0.1\""

为什么每个人都对双引号转义这么生气?看起来丑陋!这是Groovy,伙计们,你可以混合单引号和双引号:

buildConfigField "String", 'SERVER_URL', '"http://dev.myserver.com"'
buildConfigField "String", 'APP_VERSION', '"0.0.1"'

我也很困惑。但这里有一个意义- "String"定义了字段的类型,而字段值不会被自动引用,以允许我们在这里使用表达式:

buildConfigField "String", "TEST", "new Integer(10).toString()"

否则,这是不可能的。

如果你所说的“解决问题”是指不需要双引号字面量,那么我还没有遇到任何事情,因为它似乎是按照设计工作的。

我一直在尝试将字面量移动到“gradle.properties”作为一种变通方法,将潜在的多个丑陋的行变成一个丑陋的行。

像这样:

buildTypes {
def SERVER_URL = "SERVER_URL"
def APP_VERSION = "APP_VERSION"


def CONFIG = { k -> "\"${project.properties.get(k)}\"" }


debug {
buildConfigField "String", SERVER_URL, CONFIG("debug.server.url")
buildConfigField "String", APP_VERSION, CONFIG("version")
}


release {
buildConfigField "String", SERVER_URL, CONFIG("release.server.url")
buildConfigField "String", APP_VERSION, CONFIG("version")


minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

gradle.properties

version=0.1.1
...
debug.server.url=http://dev.myserver.com
...
release.server.url=http://myserver.com
...

进一步的想法:


def CONFIG = { b,k -> "\"${project.properties.get(b+'.'+k)}\"" }
def CONFIG_DEBUG = { k -> CONFIG('debug',k) }
def CONFIG_RELEASE = { k -> CONFIG('release',k) }

def CONFIG = { b,k -> "\"${project.properties.get(b+'.'+k)}\"" }
def CONFIG_INT = { b,k -> "${project.properties.get(b+'.'+k)}" }
...

使用

 buildConfigField "String", "FILE_NAME", "\"{$fileName}\""

为变量。在这里的引用

只有“my stuff”对我有用。我在my stuff中有各种奇怪的字符。

在app build。gradle中

def buildTimeAndVersion = releaseTime() + "-" + getSvnVersion()
buildTypes {
debug {
signingConfig signingConfigs.config
buildConfigField "String", 'BIULD_TIME', "\"${buildTimeAndVersion}\""
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
...
}


static def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getDefault())
}


def getSvnVersion() {
def pro = ("svnversion -c " + getBuildDir().parent).execute()
pro.waitFor()
def version = pro.in.text
Pattern p = Pattern.compile("(\\d+\\:)?(\\d+)\\D?")
Matcher m = p.matcher(version)
if (m.find()) {
version = m.group(m.groupCount())
}
try {
return version
} catch (e) {
println e.getMessage()
}
return 0
}

然后在BuildConfig中

public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "xxx.xxxxxxxx.xxx";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 53;
public static final String VERSION_NAME = "5.4.4";
// Fields from build type: debug
public static final String BIULD_TIME = "20181030-2595";
}

我们应该转义在Gradle属性或其他地方定义的Gradle常量:

buildConfigField "String", "CONSTANT_NAME", "\"${CONSTANT_VALUE}\""

其中CONSTANT_VALUE在我们的gradle.properties或其他地方定义:

CONSTANT_VALUE=string_goes_here

它同样适用于从我们的环境中获取常量:

buildConfigField "String", "CONSTANT_NAME", "\"${System.getenv('PATH')}\""

投票最多的解决方案适用于我们只需要手动添加字符串的情况,这个解决方案只是更进一步。

我需要buildConfigField和manifestPaceholder中的变量。 为了解决这个问题,我执行

def appAuthScheme= "appauth.myscheme"
buildConfigField 'String', 'APP_AUTH_SCHEME',"\"$appAuthScheme\""


manifestPlaceholders = [lowerApplicationId : applicationId.toLowerCase(),
appAuthRedirectScheme : appAuthScheme]

BuildConfig。APP_AUTH_SCHEME是一个字符串!

  1. gradle.properties中编写代码
YOUR_ACCESS_PARAMS = "YOUR_VALUE"
  1. 将代码写入应用程序级别build.gradle
android{
...
buildTypes.each{
it.buildConfigField 'String', 'ACCESS_PARAMS', YOUR_ACCESS_PARAMS
}
}
  1. 生成的BuildConfig.java如下:
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "xxx.xxxxxxxx.xxx";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0.0";
// Fields from build type: debug
public static final String ACCESS_PARAMS = "YOUR_VALUE";
}