热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

build.gradle该配置文件里路径均是相对路径applyplugin:com.android.applicationandroid{defsuffixsuffix后缀新

//build.gradle该配置文件里路径均是相对路径
apply plugin: 'com.android.application'android {def suffix = "suffix"//后缀//新增一个配置须要加入一个新的变量,命名规则operator_xxxxxxdef operator_Z = "Z"Map operatorMap = new HashMap();Map versionMap = new HashMap();Map versionCodeMap = new HashMap();Map versionNameMap = new HashMap();//设置是否使用独立的versionCode和versionNamesetVersionCN(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 = falselintOptions {//运行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 11targetSdkVersion 15versionCode 11versionName "1.0.0.11"}buildTypes {release {//是否优化zipAlignEnabled true//是否混淆minifyEnabled trueproguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-project.txt'//这里的proguard-project.txt要改动成混淆配置文件名称}}productFlavors {def flavorSign = signingConfigs.commonConfigprojectName001 {sourceSets."$name".java.srcDirs += "build-types/projectName001"signingConfig flavorSignoperatorMap.put("$name", operator_Z)if (versionMap.get(name)) {//这里传入name參数时直接用nameversionCode versionCodeMap.get(name)versionName versionNameMap.get(name)} else {versionCode defaultConfig.versionCodeversionName defaultConfig.versionName}}projectName002 {sourceSets."$name".java.srcDirs += "build-types/projectName002"signingConfig flavorSignoperatorMap.put("$name", operator_Z)if (versionMap.get(name)) {versionCode versionCodeMap.get(name)versionName versionNameMap.get(name)} else {versionCode defaultConfig.versionCodeversionName 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.jarcompile '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 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.outputFiledef buildTypeOfApp = variant.buildType.namedef operator = operatorMap.get("${variant.productFlavors[0].name}")//这里传入參数时须要使用“ "${variant.productFlavors[0].name}" ”
// def outputName = name + operator + variant.productFlavors[0].name + "_" + suffix + "_" + versiondef 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不同配置多项目打包(升级版)




推荐阅读
author-avatar
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有