Error:(81, 0) getMainOutputFile is no longer supported. Use getOutputFileName if you need to determine the file name of the output.

Error:(81, 0) getMainOutputFile is no longer supported. Use getOutputFileName if you need to determine the file name of the output.

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

   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。

就这样吧:

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;
            }
        }
    }
}

这在 Android Gradle Plugin v3 migration guide:

Using the Variant API to manipulate variant outputs is broken with the new plugin. It still works for simple tasks, such as changing the APK name during build time, as shown below:

// 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用于比重命名输出文件名更复杂的用例。