android-studio - Android Gradle 3.0.0-alpha2插件,无法设置只读属性的值 'outputFile'

标签 android-studio android-gradle-plugin

我正在使用此代码

applicationVariants.all { variant -> 
    variant.outputs.each { output ->
        def SEP = "_"
        def flavor = variant.productFlavors[0].name
        def buildType = 
        variant.variantData.variantConfiguration.buildType.name
        def version = variant.versionName
        def date = new Date()
        def formattedDate = date.format('ddMMyy_HHmm')
        def newApkName = PROJECT_NAME + SEP + flavor + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"
        def file = new File(newApkName)
        output.outputFile = file
    }
}


在构建新 apk 时更改 apk 文件的名称,但由于我使用 Android Studio 3.0 Canary 2 出现此错误:
无法设置只读属性“outputFile”的值....

最佳答案

Android plugin 3.0 migration guide建议:

  • 使用all()而不是each()
  • 如果您仅更改文件名(即您的情况),请使用 outputFileName 而不是 output.outputFile

指南中的示例:

// 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 = "${variant.name}-${variant.versionName}.apk"
    }
}

关于android-studio - Android Gradle 3.0.0-alpha2插件,无法设置只读属性的值 'outputFile',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44239235/

相关文章:

android - 渐变安卓: post-process APK

java - android.view.InflateException : Binary XML file line #27: android. support.design.widget.TextInputEditText 无法转换为 android.view.ViewGroup

android-studio - Android Studio无法使用Kotlin DSL打开Android项目

android - 有没有办法在 Android Studio 上创建具有 XXXHDPI 密度的模拟器?

android-studio - 项目同步失败|下载gradle失败|连接重置错误

java - 首次运行时无法构建 Android 项目

java - Unity3D 和 Android Studio 集成

ubuntu - 让 Ubuntu 14.10 Unity Launcher 启动 Android-Studio

android - 由于 multidex/android 错误,在 Travis 中构建失败?

java - Okio 上的 Lint 错误