今天插播一篇文章,如何通过Android Studio自定义我们打包的apk文件名.
找到你App的build.gradle文件,不是根目录的Build.gradle
如下图:
在android 标签中添加如下代码
//修改apk 的 文件名
applicationVariants.all { variant ->
variant.outputs.all { output ->
def date = new Date().format("yyyy_MM_dd_hh_mm", TimeZone.getTimeZone("GMT+08"))
if (variant.buildType.name == "debug") {
output.outputFileName = "${date}_arbitrator_v${android.defaultConfig.versionName}_debug_${android.defaultConfig.versionCode}.apk"
} else if (variant.buildType.name == "release") {
output.outputFileName = "${date}_arbitrator_v${android.defaultConfig.versionName}_release_${android.defaultConfig.versionCode}.apk"
}
}
}
怕有些小伙伴找不到应该在哪里添加,我现在把完整的代码贴上
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.zz.democostomview"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//修改apk 的 文件名
applicationVariants.all { variant ->
variant.outputs.all { output ->
def date = new Date().format("yyyy_MM_dd_hh_mm", TimeZone.getTimeZone("GMT+08"))
if (variant.buildType.name == "debug") {
output.outputFileName = "${date}_arbitrator_v${android.defaultConfig.versionName}_debug_${android.defaultConfig.versionCode}.apk"
} else if (variant.buildType.name == "release") {
output.outputFileName = "${date}_arbitrator_v${android.defaultConfig.versionName}_release_${android.defaultConfig.versionCode}.apk"
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}