新年伊始
過了雨水節(jié)氣,天氣漸暖,想到了一首非常喜歡的古詩:
王維 <<山居秋暝>>
空山新雨后,天氣晚來秋.
明月松間照,清泉石上流.
竹喧歸浣女,蓮動下漁舟.
隨意春芳歇,王孫自可留.
正題
之前寫過一篇命令行打出多個應(yīng)用包
這一次在之前的基礎(chǔ)上,進(jìn)一步加強(qiáng),實(shí)現(xiàn)修改打包的名稱,自定義輸出apk地址
項(xiàng)目結(jié)構(gòu)
image.png
在工程src目錄下新建:comone comtwo兩個文件夾,主要目的是為了實(shí)現(xiàn)打包出多個apk,當(dāng)然我并沒有在這兩個文件夾里面添加任何東西,如果需要可以參考:命令行打出多個應(yīng)用包
項(xiàng)目下的build.gradle 文件配置
- 在android{ } 里面配置多個工程包
//根據(jù)項(xiàng)目工程配置出不同的包
flavorDimensions "app"
productFlavors {
comone {
applicationId "com.huayi.one"
dimension "app"
versionCode 1
versionName "1.0"
signingConfig signingConfigs.comone
}
comontwo {
applicationId "com.huayi.two"
dimension "app"
versionCode 1
versionName "2.0"
signingConfig signingConfigs.comtwo
}
}
- 在android{ } 里面配置簽名
signingConfigs {
comone {
keyAlias 'huayi_one'
keyPassword 'qq2016'
storeFile file('D:\\ASDemo\\keyStore\\comone.jks')
storePassword 'qq2016'
}
comtwo {
keyAlias 'huayi_two'
keyPassword 'qq2016'
storeFile file('D:\\ASDemo\\keyStore\\comtwo.jks')
storePassword 'qq2016'
}
}
- 在android{ } 里面控住輸出路徑
//定義一個裝apk文件路徑的數(shù)組
def fileArray = []
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.contains('release')) {
//獲取每個打包產(chǎn)物
def variantProd = variant.productFlavors[0]
def fileName = "huayi_${variantProd.versionName}_${variantProd.versionCode}" + "_${variantProd.name}.apk"
println "自定義輸出apk的名字:" + fileName;
outputFileName = fileName;
println "輸出apk地址:" + outputFile.parentFile.absolutePath + java.io.File.separator + fileName
fileArray.add(outputFile.parentFile.absolutePath + java.io.File.separator + fileName);
}
}
}
- 在android{ } 里面寫一個apk的復(fù)制重命名的任務(wù)(task)
build {
doLast() {
println "任務(wù)1編譯打包完成后需要復(fù)制apk的數(shù)量:" + fileArray.size()
forEachFile(fileArray)
}
}
- 最后在android{ }之外 當(dāng)然是定義一個執(zhí)行復(fù)制命名的方法了
def forEachFile(fileArray) {
fileArray.forEach { file ->
//遍歷進(jìn)行文件操作
println "任務(wù)3遍歷apk文件"
rename_and_moveout_apk(file)
}
}
def rename_and_moveout_apk(orignalFile) {
def intoFile = rootDir.parentFile.getAbsolutePath() + File.separator + "apk"
copy {
from orignalFile
into intoFile
println "任務(wù)4復(fù)制apk到指定位置:" + intoFile
rename("${android.defaultConfig.versionName}_${android.defaultConfig.versionCode}_", "")
println "任務(wù)5修改apk的命名"
}
}
看看Terminal控制臺結(jié)果
配置上面的一切之后,再AndroidStudio3.0 的Terminal中輸入 : gradle build
-
看看結(jié)果:
image.png
image.png - 上面的結(jié)果清晰明了,先編譯打包,最后執(zhí)行復(fù)制命名任務(wù),一氣呵成.
貼出全部build.gradle,不留一點(diǎn)的
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.huayi.app"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//定義一個裝apk文件路徑的數(shù)組
def fileArray = []
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.contains('release')) {
//獲取每個打包產(chǎn)物
def variantProd = variant.productFlavors[0]
def fileName = "huayi_${variantProd.versionName}_${variantProd.versionCode}" + "_${variantProd.name}.apk"
println "自定義輸出apk的名字:" + fileName;
outputFileName = fileName;
println "輸出apk地址:" + outputFile.parentFile.absolutePath + java.io.File.separator + fileName
fileArray.add(outputFile.parentFile.absolutePath + java.io.File.separator + fileName);
}
}
}
lintOptions {
abortOnError false
}
signingConfigs {
comone {
keyAlias 'huayi_one'
keyPassword 'qq2016'
storeFile file('D:\\ASDemo\\keyStore\\comone.jks')
storePassword 'qq2016'
}
comtwo {
keyAlias 'huayi_two'
keyPassword 'qq2016'
storeFile file('D:\\ASDemo\\keyStore\\comtwo.jks')
storePassword 'qq2016'
}
}
//根據(jù)項(xiàng)目工程配置出不同的包
flavorDimensions "app"
productFlavors {
comone {
applicationId "com.huayi.one"
dimension "app"
versionCode 1
versionName "1.0"
signingConfig signingConfigs.comone
}
comontwo {
applicationId "com.huayi.two"
dimension "app"
versionCode 1
versionName "2.0"
signingConfig signingConfigs.comtwo
}
}
build {
doLast() {
println "任務(wù)1編譯打包完成后需要復(fù)制apk的數(shù)量:" + fileArray.size()
forEachFile(fileArray)
}
}
}
def forEachFile(fileArray) {
fileArray.forEach { file ->
//遍歷進(jìn)行文件操作
println "任務(wù)3遍歷apk文件"
rename_and_moveout_apk(file)
}
}
def rename_and_moveout_apk(orignalFile) {
def intoFile = rootDir.parentFile.getAbsolutePath() + File.separator + "apk"
copy {
from orignalFile
into intoFile
println "任務(wù)4復(fù)制apk到指定位置:" + intoFile
rename("${android.defaultConfig.versionName}_${android.defaultConfig.versionCode}_", "")
println "任務(wù)5修改apk的命名"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
最后
- 如果有具體問題可以私聊