AndroidStudio 3.0后,自定义apk包名出错: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
解决方案直接贴代码吧:
在app.gradle的android节点中添加
android {
....................
....................
buildTypes {
release {
// 使用混淆:true
minifyEnabled false
// Zipalign优化
//zipAlignEnabled true
// 移除无用的resource文件
//shrinkResources true
// 前一部分代表系统默认的android程序的混淆文件,该文件已经包含了基本的混淆声明,后一个文件是自己的定义混淆文件
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
// 自定义包名
applicationVariants.all { variant ->
variant.outputs.all { output ->
// test_版本号_yyyymmdd_release.apk
def fileName = "test_release_${variant.versionName}_${releaseTime()}.apk"
def outFile = output.outputFile
if (outFile != null && outFile.name.endsWith('.apk')) {
outputFileName = fileName
}
}
}
}
}
....................
....................
}
def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
}