在gradle中更改apk文件名失败,使用gradle:3.0.0-alpha4

时间:2017-06-28 10:31:39

标签: android android-gradle

我正在尝试在我的项目中使用android构建工具“com.android.tools.build:gradle:3.0.0-alpha4”。在我的构建脚本中,我重命名输出apk,它在过去运行良好但似乎不再受支持。

android {

   productFlavors {
        flavorUnsigned {
            applicationVariants.all { variant ->
                variant.outputs.all { output ->
                    output.outputFile = new File(
                            output.outputFile.parent,
                            output.outputFile.name.replace("app-flavorUnsigned-release-unsigned.apk", "DemoApp-${variant.versionName}($variant.versionCode).apk"))
                    def mappingFile = "${rootDir}/app/build/outputs/mapping/${getCurrentFlavor()}/release/mapping.txt"
                    if (variant.getBuildType().isMinifyEnabled()) {
                        variant.assemble.doLast {
                            copy {
                                from "${mappingFile}"
                                into "${rootDir}/app/build/outputs/apk"
                            }
                        }
                    }
                }
            }
        }

    }
}

但是现在我在构建项目时遇到了这个错误

Error:Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=flavorUnsignedDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

2 个答案:

答案 0 :(得分:21)

如果您要将项目迁移到Android插件3.0.0-alpha1或更高版本,则应执行以下操作: 变体输出中的API更改:

// 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"
    }
}

阅读this页面,了解如何应用插件并使项目适应一些重大变化。

答案 1 :(得分:-1)

我用这种方法解决了这个问题。创建一个新项目,并将build.gradle的内容复制到现有项目中。