在我的Android Studio项目中,有两个带有一些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_VERSION和SERVER_URL没有正确生成为字符串类型,它们没有引号。

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


当前回答

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

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"'

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

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

其中CONSTANT_VALUE在gradle中定义。属性或其他地方:

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是一个字符串!

在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”中。属性”作为一种变通方法,将潜在的多个丑陋的行变成一个丑陋的行。

像这样:

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)}" }
...