为了方便识别apk文件,一般我们都希望通过androoid studio打包的文件,会带上app的名字,时间,之类的,使用多渠道打包的时候,还希望带上渠道名称
以前我都这样做
//修改生成的apk名字android{ applicationVariants.all { variant -> if (variant.buildType.name.equals('release')) { variant.outputs.each { output -> def parent = './apk/' def buildName def releaseApkName def type = variant.buildType.name; releaseApkName = buildName + '_' + type + "_" + versionName + '_' + getDate() + '.apk' output.outputFile = new File(parent, releaseApkName) } } }}复制代码
但是gradle更新到3.0.0以后就不行了,会报错
Error:(26, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{ type=MAIN, fullName=release, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.Open File复制代码
再贴个图
这个错误大概意思就是outputFile这个引用现在是“read-only ”(只读)的,不能重新赋予新的对象。也就是说原来的讨论不让用了,经过多方Google,我找到了这个方法
现在这样做
android{ applicationVariants.all { variant -> if (variant.buildType.name.equals('release')) { variant.outputs.all { output -> def buildName = "Downloader" def type = variant.buildType.name def releaseApkName = buildName + '_' + type + "_" + versionName + '_' + getTime() + '.apk' outputFileName = releaseApkName } } }}复制代码
两种方式的区别在于,重命名的地方,从 variant.outputs.each 方法改成 variant.outputs.all 方法,以及output.outputFile = new File(parent, releaseApkName) 改成 outputFileName = releaseApkName,就是说,现在不能指定路径,你只能修改文件名,路径只能使用默认的(当然你如果找到了可以修改路径的方法,千万留言),默认路径一般在/app/build/outputs/apk/{buildType}/:
你如果使用Android stuido 的打包选型,并且创建了新的productFlavors,会在App文件夹里自动创建对应productFlavors的文件: