无法在项目已经评估后运行 Project.afterEvaluate(Action)。

huangapple go评论52阅读模式
英文:

Cannot run Project.afterEvaluate(Action) when the project is already evaluated

问题

以下是您提供的内容的翻译部分:

项目类型:Android

Gradle包装程序:gradle-7.5

Gradle插件:7.4.2

应用程序模块build.gradle:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.kapt'
}

android {
    ...

    defaultConfig {
        ...
    }

    flavorDimensions 'app', 'channel'
    productFlavors {
        dev {
            dimension 'app'
            applicationIdSuffix '.dev'
        }

        product {
            dimension 'app'
        }

        google {
            dimension 'channel'
        }

        android.applicationVariants.all { variant ->
            def dirPath = project.projectDir.absolutePath + '/build/outputs/apk/' + dirName + '/'
            def filePath = ''
            variant.outputs.all {
                outputFileName = "xxx.apk"
                filePath = outputFileName
            }

            variant.getAssembleProvider().get().doLast {
                if (variant.buildType.name == 'release') {
                    def cmd = 'cd .. && ./ossutilmac64 cp -f ' + dirPath + filePath + ' oss://android-test-sz/' + filePath
                    exec {
                        ExecSpec execSpec ->
                            executable 'zsh'
                            args '-c', cmd
                            standardOutput = out
                    }
                    println(out.toString())
                }
            }
        }
    }
}

dependencies {
    ...
}

当我运行Shell命令“./gradlew assembleDevGoogleRelease”来构建APK并使用Gradle 7上传APK到OSS时,不幸的是,构建失败了(在Gradle 6.x中是正常的)。

> Task :app:assembleDevGoogleRelease FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '...app/build.gradle' line: 222

* What went wrong:
Execution failed for task ':app:assembleDevGoogleRelease'.
> Cannot run Project.afterEvaluate(Action) when the project is already evaluated.

错误发生在第222行的“exec {”中。

非常感谢您的帮助。提前感谢。 :slight_smile:

英文:

Project type: Android

Gradle wrapper: gradle-7.5

Gradle plugin: 7.4.2

app module build.gradle:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id "org.jetbrains.kotlin.kapt"
}

android {
    ...

    defaultConfig {
        ...
    }

    flavorDimensions "app", "channel"
    productFlavors {
        dev {
            dimension "app"
            applicationIdSuffix ".dev"
        }

        product {
            dimension "app"
        }

        google {
            dimension "channel"
        }

        android.applicationVariants.all { variant ->
            def dirPath = project.projectDir.absolutePath + '/build/outputs/apk/' + dirName + '/'
            def filePath = ''
            variant.outputs.all {
                outputFileName = "xxx.apk"
                filePath = outputFileName
            }

            variant.getAssembleProvider().get().doLast {
                if (variant.buildType.name == 'release') {
                    def cmd = 'cd .. && ./ossutilmac64 cp -f ' + dirPath + filePath + ' oss://android-test-sz/' + filePath
                    exec {
                        ExecSpec execSpec ->
                            executable 'zsh'
                            args '-c', cmd
                            standardOutput = out
                    }
                    println(out.toString())
                }
            }
        }
    }
}

dependencies {
    ...
}

when I run shell “./gradlew assembleDevGoogleRelease” to build apk and upload apk to oss with Gradle 7, unfortunately, the build fails. (Gradle 6.x is ok)

> Task :app:assembleDevGoogleRelease FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '...app/build.gradle' line: 222

* What went wrong:
Execution failed for task ':app:assembleDevGoogleRelease'.
> Cannot run Project.afterEvaluate(Action) when the project is already evaluated.

The error line 222 is the “exec {” in doLast.

Any help is much appreciated. Thanks in advance. :slight_smile:

答案1

得分: 1

你应该将"android.applicationVariants.all"放在外部块中。

英文:

You should place the applicationVariants.all on the outside block

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.kapt'
}

android {
    // ...

    defaultConfig {
        // ...
    }

    flavorDimensions 'app', 'channel'
    productFlavors {
        dev {
            dimension 'app'
            applicationIdSuffix '.dev'
        }

        product {
            dimension 'app'
        }

        google {
            dimension 'channel'
        }
    }
}

android.applicationVariants.all { variant ->
    def dirPath = project.projectDir.absolutePath + '/build/outputs/apk/' + dirName + '/'
    def filePath = ''
    variant.outputs.all {
        outputFileName = "xxx.apk"
        filePath = outputFileName
    }

    variant.getAssembleProvider().get().doLast {
        if (variant.buildType.name == 'release') {
            def cmd = 'cd .. && ./ossutilmac64 cp -f ' + dirPath + filePath + ' oss://android-test-sz/' + filePath
            exec {
                executable 'zsh'
                args '-c', cmd
                standardOutput = out
            }
            println(out.toString())
        }
    }
}

huangapple
  • 本文由 发表于 2023年6月13日 16:05:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定