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

Moh*_*Ali 84 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
    }
}
Run Code Online (Sandbox Code Playgroud)


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

Paw*_*ski 204

正如Android插件3.0迁移指南所示:

  • 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"
    }
}
Run Code Online (Sandbox Code Playgroud)


Gol*_*hay 6

见下文:

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        def newApkName = applicationId + "-" + variant.versionName + "(" + variant.versionCode + ")" + ".apk";
        outputFileName = new File("${project.projectDir}/../outputs/apks/" + variant.name, newApkName);
    }
}
Run Code Online (Sandbox Code Playgroud)