1

我更新gradle 4.0.17.0.3是因为我需要新 gradle 的支持。

我让自动更新程序运行,完成后,当我运行代码时,出现以下错误:

C:\Users\me\Projects\proj\proj\proj\app\build\generated\source\buildConfig\stage\debug\proj\BuildConfig.java:22: error: illegal forward reference
public static final String APPLICATION_LIST_URL = BACKEND_HOST + "/page";

build.gradlebuildConfigField声明如下:

defaultConfig {
    applicationId "my.app.id"
    minSdkVersion 21
    versionCode getBuildTimestamp()
    versionName "2.0.0"

    buildConfigField 'String', 'APPLICATION_LIST_URL', 'BACKEND_HOST + "/page"'

}

我试过Invaldiate cache/restart了,我不知道我还能尝试什么。

EDIT BACKEND_HOST也被定义:

productFlavors {
    local {
        dimension "type"
        targetSdkVersion 30
        buildConfigField 'String', 'APK_DOWNLOAD_RESOLVE_URL', 'BACKEND_HOST + "DOES_NOT_EXIST"'
        ...
    }

    remote {
        dimension "type"
        targetSdkVersion 30
        applicationIdSuffix ".remote"
        buildConfigField 'String', 'APK_DOWNLOAD_RESOLVE_URL', 'BACKEND_HOST + "/remote/download"'
    }

    def backendRemote= '"https://myUrl"'

    android.applicationVariants.all {
        variant ->
            def appName = "myApp"
            def backendHost = backendRemote
            variant.resValue "string", "app_name", appName
            resValue "string", "app_version", "${appName} ${variant.versionName}"
            variant.buildConfigField "String", "AUTH_HOST", backendHost
            variant.buildConfigField "String", "BACKEND_HOST", backendHost
    }
}

我正在用remote味道建造它

4

1 回答 1

1

目前尚不清楚构建工具如何确定 BuildConfig 中字段声明的顺序。但有效的是这个(注意BuildConfig.BACKEND_HOST而不是 just BACKEND_HOST):

buildConfigField 'String', 'BACKEND_HOST', 'my.backend.host.com'
buildConfigField 'String', 'APPLICATION_LIST_URL', 'BuildConfig.BACKEND_HOST + "/page"'

https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html的第 8.3.3 章解释了哪些前向引用是合法的,哪些是非法的。

这是一个最小的代码示例,展示了如何在每种风格中定义 BACKEND_HOST:

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdk 30
        targetSdk 31
        versionCode 1
        versionName "1.0"

        buildConfigField 'String', 'APPLICATION_LIST_URL', 'BuildConfig.BACKEND_HOST + "/page"'
    }

    flavorDimensions "version"
    productFlavors {
        free {
            dimension "version"
            buildConfigField "String", "BACKEND_HOST", '"www.free.com"'
        }
        paid {
            dimension "version"
            buildConfigField "String", "BACKEND_HOST", '"www.paid.com"'
        }
    }

这是因为更具体的 BuildConfig 字段(风味)在不太具体的字段(defaultConfig)之前首先被评估。

BACKEND_HOST此代码是从 OP 的问题中复制的,并通过对静态的引用进行修改以进行编译:

    defaultConfig {
        applicationId "my.app.id"
        minSdkVersion 21
        versionCode getBuildTimestamp()
        versionName "2.0.0"

        buildConfigField 'String', 'APPLICATION_LIST_URL', 'BuildConfig.BACKEND_HOST + "/page"'
        buildConfigField "String", "BACKEND_HOST", '"www.paid.com"'
    }


    flavorDimensions "type"
    productFlavors {
        local {
            dimension "type"
            targetSdkVersion 30
            buildConfigField 'String', 'APK_DOWNLOAD_RESOLVE_URL', 'BuildConfig.BACKEND_HOST + "DOES_NOT_EXIST"'
        }

        remote {
            dimension "type"
            targetSdkVersion 30
            applicationIdSuffix ".remote"
            buildConfigField 'String', 'APK_DOWNLOAD_RESOLVE_URL', 'BuildConfig.BACKEND_HOST + "/remote/download"'
        }
    }

    def backendRemote= '"https://myUrl"'

    android.applicationVariants.all {
        variant ->
            def backendHost = backendRemote
            variant.buildConfigField "String", "AUTH_HOST", backendHost
            variant.buildConfigField "String", "BACKEND_HOST", backendHost
    }
于 2021-10-14T15:17:59.063 回答