赞
踩
使用Androidstudio开发app,一般打包会默认生成app-debug.apk、app-release.apk、app-release-unsigned.apk等包,我们如果有定制化的需求,比如在包名上体现出版本号等,这时我们就需要适当了解gradle脚本。
//根据项目工程配置出不同的包 flavorDimensions "app" productFlavors { comone { applicationId "com.example.one" dimension "app" versionCode 1 versionName "1.0" signingConfig signingConfigs.comone } comontwo { applicationId "com.example.two" dimension "app" versionCode 1 versionName "2.0" signingConfig signingConfigs.comtwo } }
signingConfigs {
comone {
keyAlias 'hsemploy'
keyPassword '*****'
storeFile file('/Users/zhanglei/Desktop/employ')
storePassword '*****'
}
comtwo {
keyAlias 'hsemploy'
keyPassword '*****'
storeFile file('/Users/zhanglei/Desktop/employ')
storePassword '*****'
}
}
//定义一个装apk文件路径的数组 def fileArray = [] android.applicationVariants.all { variant -> variant.outputs.all { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.contains('release')) { //获取每个打包产物 def variantProd = variant.productFlavors[0] def fileName = "example_${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); } } }
build {
doLast() {
println "任务1编译打包完成后需要复制apk的数量:" + fileArray.size()
forEachFile(fileArray)
}
}
def forEachFile(fileArray) { fileArray.forEach { file -> //遍历进行文件操作 println "任务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 "任务4复制apk到指定位置:" + intoFile rename("${android.defaultConfig.versionName}_${android.defaultConfig.versionCode}_", "") println "任务5修改apk的命名" } }
apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "com.example.myapplication" minSdkVersion 19 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } signingConfigs { comone { keyAlias 'hsemploy' keyPassword 'hsemploy' storeFile file('/Users/zhanglei/Desktop/employ') storePassword 'hsemploy' } comtwo { keyAlias 'hsemploy' keyPassword 'hsemploy' storeFile file('/Users/zhanglei/Desktop/employ') storePassword 'hsemploy' } } //根据项目工程配置出不同的包 flavorDimensions "app" productFlavors { comone { applicationId "com.example.one" dimension "app" versionCode 1 versionName "1.0" signingConfig signingConfigs.comone } comontwo { applicationId "com.example.two" dimension "app" versionCode 1 versionName "2.0" signingConfig signingConfigs.comtwo } } //定义一个装apk文件路径的数组 def fileArray = [] android.applicationVariants.all { variant -> variant.outputs.all { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.contains('release')) { //获取每个打包产物 def variantProd = variant.productFlavors[0] def fileName = "example_${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); } } } build { doLast() { println "任务1编译打包完成后需要复制apk的数量:" + fileArray.size() forEachFile(fileArray) } } } def forEachFile(fileArray) { fileArray.forEach { file -> //遍历进行文件操作 println "任务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 "任务4复制apk到指定位置:" + intoFile rename("${android.defaultConfig.versionName}_${android.defaultConfig.versionCode}_", "") println "任务5修改apk的命名" } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。