Android使用gradle不同配置多项目打包

时间:2023-02-07 14:53:37
//build.gradle该配置文件里路径均是相对路径
apply plugin: 'com.android.application' android {
def suffix = "suffix"//后缀
//新增一个配置须要加入一个新的变量,命名规则operator_xxxxxx
def operator_Z = "Z"
Map<String, String> operatorMap = new HashMap<String, String>();
Map<String, Boolean> versionMap = new HashMap<String, Boolean>();
Map<String, Integer> versionCodeMap = new HashMap<String, Integer>();
Map<String, String> versionNameMap = new HashMap<String, String>();
//设置是否使用独立的versionCode和versionName
setVersionCN(versionMap, versionCodeMap, versionNameMap, "projectName001", true, 10, "1.0.0.10test")
setVersionCN(versionMap, versionCodeMap, versionNameMap, "projectName002", true, 10, "1.0.0.10test")
//编译sdk版本号
compileSdkVersion 21
//编译工具版本号
buildToolsVersion "23.0.0 rc2"
//是否强制唯一包名
enforceUniquePackageName = false lintOptions {
//运行lint检查,不会终止编译
abortOnError false
} dexOptions {
preDexLibraries = false
} signingConfigs {
commonConfig {
storeFile file("wasu.keystore")
storePassword "wasu1234"
keyAlias "android.keystore"
keyPassword "wasu1234"
}
} defaultConfig {
applicationId "cn.com.wasu.backplay"
minSdkVersion 11
targetSdkVersion 15
versionCode 11
versionName "1.0.0.11"
}
buildTypes {
release {
//是否优化
zipAlignEnabled true
//是否混淆
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-project.txt'//这里的proguard-project.txt要改动成混淆配置文件名称
}
}
productFlavors {
def flavorSign = signingConfigs.commonConfig
projectName001 {
sourceSets."$name".java.srcDirs += "build-types/projectName001"
signingConfig flavorSign
operatorMap.put("$name", operator_Z)
if (versionMap.get(name)) {//这里传入name參数时直接用name
versionCode versionCodeMap.get(name)
versionName versionNameMap.get(name)
} else {
versionCode defaultConfig.versionCode
versionName defaultConfig.versionName
}
}
projectName002 {
sourceSets."$name".java.srcDirs += "build-types/projectName002"
signingConfig flavorSign
operatorMap.put("$name", operator_Z)
if (versionMap.get(name)) {
versionCode versionCodeMap.get(name)
versionName versionNameMap.get(name)
} else {
versionCode defaultConfig.versionCode
versionName defaultConfig.versionName
}
}
} sourceSets {
main {
assets.srcDirs = ['src/main/assets']
res.srcDirs = ['src/main/res']
aidl.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/java']
renderscript.srcDirs = ['src/main/java']
jniLibs.srcDirs = ['libs']//同一时候包括*.so,*.jar文件
manifest.srcFile 'src/main/AndroidManifest.xml' java {
srcDirs = ['src/main/java']
exclude 'cn/com/a/b/c/Common.java'//排除项目中的该类
}
}
} packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/ASL2.0'
} println "共" + operatorMap.size() + "个项目须要编译" setup(operatorMap, suffix) clean {
delete += "output"
}
} dependencies {
//在混淆文件里须要凝视相关jar包。例如以下
//#-libraryjars libs/dom4j-1.6.1.jar
//#-libraryjars libs/jackson-core-asl-1.8.0.jar
//#-libraryjars libs/jackson-mapper-asl-1.8.0.jar
//#-libraryjars libs/junit4.4.jar
//#-libraryjars libs/spring-android-rest-template-1.0.0.M2.jar
//#-libraryjars libs/Log.jar
//#-libraryjars libs/gson-2.2.4.jar
compile 'com.android.support:support-v4:21.0.3'
compile 'com.google.code.gson:gson:2.2.4'
compile files('libs/Log.jar')
compile files('libs/dom4j-1.6.1.jar')
compile files('libs/jackson-core-asl-1.8.0.jar')
compile files('libs/jackson-mapper-asl-1.8.0.jar')
compile files('libs/junit4.4.jar')
compile files('libs/spring-android-rest-template-1.0.0.M2.jar')
} build.doLast {
def today = new Date().format('yyyyMMdd');
def dir_debug = 'output/' + today + '/debug'
def dir_release = 'output/' + today + '/release' copy {//复制release版本号apk到单独目录
from('build/outputs/apk')
into(dir_debug)
include('*_debug.apk')
exclude("*-unaligned.apk");
exclude("*-unsigned.apk");
} copy {//复制debug版本号apk到单独目录
from('build/outputs/apk')
into(dir_release)
include('*.apk')
exclude('*_debug.apk')
exclude("*-unaligned.apk");
exclude("*-unsigned.apk");
}
} def setup(Map<String, String> operatorMap, suffix) {
def name = "xxxx_";
def today = new Date().format('yyyyMMddHHmm');
def today1 = new Date().format('yyyyMMdd'); android.applicationVariants.all { variant ->
variant.outputs.each { output ->
// def version = android.defaultConfig.versionName;
def version = variant.productFlavors[0].versionName;
def file = output.outputFile
def buildTypeOfApp = variant.buildType.name
def operator = operatorMap.get("${variant.productFlavors[0].name}")//这里传入參数时须要使用“ "${variant.productFlavors[0].name}" ”
// def outputName = name + operator + variant.productFlavors[0].name + "_" + suffix + "_" + version
def test = "%s%s%s_%s_%s" def outputName = String.format(test, name, operator, variant.productFlavors[0].name, suffix, version) if (!variant.buildType.name.equalsIgnoreCase("debug")) {
output.outputFile = new File(file.parent, outputName + "_" + today + ".apk")
} else {
output.outputFile = new File(file.parent, outputName + "_" + today1 + "_debug.apk");
}
println output.outputFile;
}
}
} /**
* 设置是否使用自己定义versionCode和versionName
* @param versionMap 是否自己定义map
* @param versionCodeMap 自己定义versionCode map
* @param versionNameMap 自己定义versionName map
* @param name 项目名
* @param isSingle 是否使用自己定义
* @param code 自己定义versionCode
* @param vname 自己定义versionName
* @return
*/
def setVersionCN(versionMap, versionCodeMap, versionNameMap, String name, boolean isSingle, int code, String vname) {
versionMap.put(name, isSingle);
versionCodeMap.put(name, code);
versionNameMap.put(name, vname);
}

升级版Android使用gradle不同配置多项目打包(升级版)