小编典典

如何生成字符串类型的 buildConfigField

all

在我的项目中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_VERSIONandSERVER_URL没有正确生成为 String 类型,它们没有引号。

我不确定为什么它会以这种方式生成。请让我知道如何解决此问题。


阅读 93

收藏
2022-07-13

共1个答案

小编典典

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

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

引号中的字段名称,转义引号中的字段值。

2022-07-13