Gradle:无法获取未知 属性

Gradle: Could not get unknown property

已经定义了所需的存储库变量。

这是管道中的 Gradle 包装器命令

- step:
        name: Create Keystores and Google Play API key file
        script:
          - mkdir keys # create new directory
          - echo $DEBUG_KEYSTORE_FILE_BASE64 | base64 -d > keys/$DEBUG_KEYSTORE_FILE # decode base64 then use DEBUG_KEYSTORE_FILE as file name defined in repository variable
          - echo $KEYSTORE_FILE_BASE64 | base64 -d > keys/$KEYSTORE_FILE # decode base64 then use KEYSTORE_FILE as file name defined in repository variable
          - echo $GOOGLE_PLAY_API_KEY_BASE64 | base64 -d > keys/$GOOGLE_PLAY_API_KEY_FILE # decode base64 then use GOOGLE_PLAY_API_KEY_FILE as file name defined in repository variable
        artifacts:
          - keys/** # keep these artifacts for subsequent pipeline steps

- step:
        name: Build & Deploy (Internal)
        caches: # caching speed up subsequent execution https://support.atlassian.com/bitbucket-cloud/docs/cache-dependencies/
          - gradle
        script:
          # publish the aab file to the internal testing channel
          # the plugin publishes to the internal testing track by default
          - chmod +x gradlew
          - "./gradlew
                       -PANDROID_BUILD_COMPILED_SDK_VERSION=$ANDROID_BUILD_COMPILED_SDK_VERSION
                       -PANDROID_BUILD_MIN_SDK_VERSION=$ANDROID_BUILD_MIN_SDK_VERSION
                       -PANDROID_BUILD_TARGET_SDK_VERSION=$ANDROID_BUILD_TARGET_SDK_VERSION

                       -PDEBUG_KEYSTORE_FILE=../keys/$DEBUG_KEYSTORE_FILE
                       -PDEBUG_KEYSTORE_PASSWORD=$DEBUG_PASSWORD
                       -PDEBUG_KEYSTORE_KEY_ALIAS=$DEBUG_KEY_ALIAS
                       -PDEBUG_KEYSTORE_KEY_PASSWORD=$DEBUG_KEY_PASSWORD

                       -PAPPNAME_KEYSTORE_FILE=../keys/$KEYSTORE_FILE
                       -PAPPNAME_KEYSTORE_PASSWORD=$KEYSTORE_PASSWORD
                       -PAPPNAME_KEY_ALIAS=$KEYSTORE_KEY_ALIAS
                       -PAPPNAME_KEY_PASSWORD=$KEY_PASSWORD

                       -PGOOGLE_PLAY_API_KEY=../keys/$GOOGLE_PLAY_API_KEY

                       -PBUILD_NUMBER=$BITBUCKET_BUILD_NUMBER

                        clean :app:publishReleaseBundle"
        artifacts:
          - app/build/outputs/ # artifacts are files that are produced by a step https://support.atlassian.com/bitbucket-cloud/docs/use-artifacts-in-steps/

问题是 -PDEBUG_KEYSTORE_FILE=../keys/$DEBUG_KEYSTORE_FILE 在构建过程中有些被忽略并且不工作。

Build setup5s

chmod +x gradlew<1s

./gradlew -PANDROID_BUILD_COMPILED_SDK_VERSION=$ANDROID_BUILD_COMPILED_SDK_VERSION -PANDROID_BUILD_MIN_SDK_VERSION=$ANDROID_BUILD_MIN_SDK_VERSION -PANDROID_BUILD_TARGET_SDK_VERSION=$ANDROID_BUILD_TARGET_SDK_VERSION -PDEBUG_KEYSTORE_FILE=../keys/$DEBUG_KEYSTORE_FILE -PDEBUG_KEYSTORE_PASSWORD=$DEBUG_PASSWORD -PDEBUG_KEYSTORE_KEY_ALIAS=$DEBUG_KEY_ALIAS -PDEBUG_KEYSTORE_KEY_PASSWORD=$DEBUG_KEY_PASSWORD -PAPPNAME_KEYSTORE_FILE=../keys/$KEYSTORE_FILE -PAPPNAME_KEYSTORE_PASSWORD=$KEYSTORE_PASSWORD -PAPPNAME_KEY_ALIAS=$KEYSTORE_KEY_ALIAS -PAPPNAME_KEY_PASSWORD=$KEY_PASSWORD -PGOOGLE_PLAY_API_KEY=../keys/$GOOGLE_PLAY_API_KEY -PBUILD_NUMBER=$BITBUCKET_BUILD_NUMBER clean :app:publishFreeReleaseBundle39s

+ ./gradlew -PANDROID_BUILD_COMPILED_SDK_VERSION=$ANDROID_BUILD_COMPILED_SDK_VERSION -PANDROID_BUILD_MIN_SDK_VERSION=$ANDROID_BUILD_MIN_SDK_VERSION -PANDROID_BUILD_TARGET_SDK_VERSION=$ANDROID_BUILD_TARGET_SDK_VERSION
-PDEBUG_KEYSTORE_FILE=../keys/$DEBUG_KEYSTORE_FILE -PDEBUG_KEYSTORE_PASSWORD=$DEBUG_PASSWORD -PDEBUG_KEYSTORE_KEY_ALIAS=$DEBUG_KEY_ALIAS -PDEBUG_KEYSTORE_KEY_PASSWORD=$DEBUG_KEY_PASSWORD
-PAPPNAME_KEYSTORE_FILE=../keys/$KEYSTORE_FILE -PAPPNAME_KEYSTORE_PASSWORD=$KEYSTORE_PASSWORD -PAPPNAME_KEY_ALIAS=$KEYSTORE_KEY_ALIAS -PAPPNAME_KEY_PASSWORD=$KEY_PASSWORD
-PGOOGLE_PLAY_API_KEY=../keys/$GOOGLE_PLAY_API_KEY
-PBUILD_NUMBER=$BITBUCKET_BUILD_NUMBER
clean :app:publishFreeReleaseBundle
Downloading https://services.gradle.org/distributions/gradle-7.0-bin.zip
...........................................................................................................
Unzipping /root/.gradle/wrapper/dists/gradle-7.0-bin/2p9ebqfz6ilrfozi676ogco7n/gradle-7.0-bin.zip to /root/.gradle/wrapper/dists/gradle-7.0-bin/2p9ebqfz6ilrfozi676ogco7n
Set executable permissions for: /root/.gradle/wrapper/dists/gradle-7.0-bin/2p9ebqfz6ilrfozi676ogco7n/gradle-7.0/bin/gradle
Welcome to Gradle 7.0!
Here are the highlights of this release:
 - File system watching enabled by default
 - Support for running with and building Java 16 projects
 - Native support for Apple Silicon processors
 - Dependency catalog feature preview
For more details see https://docs.gradle.org/7.0/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)
FAILURE: Build failed with an exception.
* Where:
Build file '/opt/atlassian/pipelines/agent/build/app/build.gradle' line: 27
* What went wrong:
A problem occurred evaluating project ':app'.
> Could not get unknown property 'DEBUG_KEYSTORE_FILE' for SigningConfig_Decorated{name=debug, storeFile=/opt/android-sdk-linux/.android/debug.keystore, storePassword=android, keyAlias=AndroidDebugKey, keyPassword=android, storeType=pkcs12, v1SigningEnabled=true, v2SigningEnabled=true, enableV1Signing=null, enableV2Signing=null, enableV3Signing=null, enableV4Signing=null} of type com.android.build.gradle.internal.dsl.SigningConfig.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 39s

然而,为了进一步测试,我试图删除 -PANDROID_BUILD_COMPILED_SDK_VERSION=$ANDROID_BUILD_COMPILED_SDK_VERSION 参数,现在错误出现在 compileSdk

FAILURE: Build completed with 2 failures.
1: Task failed with an exception.
-----------
* Where:
Build file '/opt/atlassian/pipelines/agent/build/app/build.gradle' line: 13
* What went wrong:
A problem occurred evaluating project ':app'.
> Could not get unknown property 'ANDROID_BUILD_COMPILED_SDK_VERSION' for extension 'android' of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================
2: Task failed with an exception.
-----------
* What went wrong:
A problem occurred configuring project ':app'.
> compileSdkVersion is not specified. Please add it to build.gradle
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
==============================================================================
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/7.0/userguide/command_line_interface.html#sec:command_line_warnings
* Get more help at https://help.gradle.org
BUILD FAILED in 39s

有了这个,我假设参数工作正常,但在签名时,我自己的签名配置似乎被忽略了。

这是应用 Gradle 文件的样子。

android {
    compileSdk Integer.parseInt(ANDROID_BUILD_COMPILED_SDK_VERSION)

    defaultConfig {
        applicationId "com.app.name"
        minSdk Integer.parseInt(ANDROID_BUILD_MIN_SDK_VERSION)
        targetSdk Integer.parseInt(ANDROID_BUILD_TARGET_SDK_VERSION)
        versionCode project.hasProperty('BUILD_NUMBER') ? project['BUILD_NUMBER'].toInteger() : 14
        versionName "1.3.3"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        debug {
            storeFile file(DEBUG_KEYSTORE_FILE)
            storePassword DEBUG_KEYSTORE_PASSWORD
            keyAlias DEBUG_KEYSTORE_KEY_ALIAS
            keyPassword DEBUG_KEYSTORE_KEY_PASSWORD
        }
        release {
            storeFile file(APPNAME_KEYSTORE_FILE)
            storePassword APPNAME_KEYSTORE_PASSWORD
            keyAlias APPNAME_KEYSTORE_KEY_ALIAS
            keyPassword APPNAME_KEYSTORE_KEY_PASSWORD
        }
    }

    buildTypes {
        debug {
            debuggable true
            minifyEnabled true
            shrinkResources true
            multiDexEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

            signingConfig signingConfigs.debug
        }
        release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            multiDexEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

            signingConfig signingConfigs.release
        }
    }

    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    lintOptions {
        disable "MissingClass"
    }

    buildFeatures {
        viewBinding true
    }
}

play {
    serviceAccountCredentials.set(file(GOOGLE_PLAY_API_KEY_FILE))
}

更新: 似乎它不特定于调试签名,但也有发布我得到 Could not get unknown property 'APPNAME_KEYSTORE_FILE'

运行 它在 Android Studio 终端本地工作,但在管道中它不工作。

如果文件路径或位置有问题,则错误消息不应 Could not get unknown property

根据 https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html,在规范中为 Bamboo 变量添加 bamboo_ 前缀。例如 $DEBUG_KEYSTORE_FILE 变成 $bamboo_DEBUG_KEYSTORE_FILE.

先去掉

之间的间距
-PANDROID_BUILD_TARGET_SDK_VERSION=$ANDROID_BUILD_TARGET_SDK_VERSION

-PDEBUG_KEYSTORE_FILE=../keys/$DEBUG_KEYSTORE_FILE

下降到

-PBUILD_NUMBER=$BITBUCKET_BUILD_NUMBER

clean :app:publishReleaseBundle

那里应该没有 space,因为您指出 -PANDROID_BUILD_COMPILED_SDK_VERSION 正在工作。换行 spaces 会使命令不完整。

它将解决您当前的问题,但是您会遇到另一个问题,因为您的环境变量与命令任务和 Bitbucket 存储库变量页面不匹配。在命令或网站上更改您的变量名称。

在 BB 页面中没有名为 DEBUG_KEY_PASSWORD 的变量。

仔细检查命令任务和 Bitbucket 存储库变量中的其他变量和属性,您可能还遗漏了另一个。