4

我正在尝试使用以下代码自定义构建过程

   android.applicationVariants.all { variant ->
            def appName = "MyApplication.apk"

            variant.outputs.each { output ->
                output.outputFile = new File(output.outputFile.parent, appName)
            }
        }

但是从 android studio 3.0 开始,它无法正常工作,我遇到了错误

错误:(81, 0) 不再支持 getMainOutputFile。如果您需要确定输出的文件名,请使用 getOutputFileName。

4

2 回答 2

2

这样做:

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig getSigningConfig()
        android.applicationVariants.all { variant ->
            def date = new Date();
            def formattedDate = date.format('dd MMMM yyyy')
            variant.outputs.all {
                def newApkName
                newApkName = "MyApp-${variant.versionName}, ${formattedDate}.apk"
                outputFileName = newApkName;
            }
        }
    }
}
于 2017-06-14T06:20:23.637 回答
1

这在Android Gradle Plugin v3 迁移指南中有介绍:

新插件破坏了使用 Variant API 来操作变体输出。它仍然适用于简单的任务,例如在构建期间更改 APK 名称,如下所示:

// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${project.name}-${variant.name}-${variant.versionName}.apk"
    }
}

将有一个新的 api 用于比重命名输出文件名更复杂的用例。

于 2017-06-19T20:41:32.090 回答