将 gradle 更新到 7.0.3 后 buildConfigField 不工作

buildConfigField not working after updating gradle to 7.0.3

我将gradle 4.0.1更新为7.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.gradle 中,buildConfigField 声明如下:

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

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

}

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

编辑 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 风格构建它

不清楚构建工具如何确定 BuildConfig 中字段声明的顺序。但有效的是这个(注意 BuildConfig.BACKEND_HOST 而不仅仅是 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 字段(风格),然后再评估不太具体的字段(默认配置)。

此代码是从 OP 的问题中复制的,并通过引用 BACKEND_HOST static:

进行修改以进行编译
    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
    }