Gradle配置打包文件名

前段时间从AS 2.4到AS 3.0 Beta版本的时候,由于Gradle升级了,导致我原有的文件配置代码没法正常的使用。这两天又遇到这个问题了。所以记录一下。

以前应该都是这么写的:

applicationVariants.all { variant -\>
variant.outputs.all { output -\>
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('release.apk')) {
def fileName = “YourApp_${defaultConfig.versionCode}_v${defaultConfig.versionName}.apk"

//                            output.outputFile = new File(outputFile.parent, fileName)

//                            def fileName = outputFile.name.replace("app", "xx00_${defaultConfig.versionCode}")

outputFileName = new File(outputFile.parent, fileName)
}

if (outputFile != null && outputFile.name.endsWith('release-unsigned.apk')) {

def fileName = “YourApp_${defaultConfig.versionCode}_v${defaultConfig.versionName}_unsigned.apk"

output.outputFile = new File(outputFile.parent, fileName)
}
}
}

但是这样在新版本的Gradle里面会报错,Like this。按照底下答案给的Ref,我找到了,官方的文档,实测使用官方的文档解决了我的问题。

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

上面需要注意将each -> all,然后下面的outputFileName就原样的输出就可以了。

    原文作者:Domon_Lee
    原文地址: https://www.jianshu.com/p/adb92ff9c0a3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞