相关注释请看Android使用gradle不同配置多项目打包
此升级版特点,使用version.properties文件来进行配置,使用Android Studio时,每次修改build.gradle文件,都需要sync now,这个时候当存在多个项目的时候,只需修改version.properties
//build.gradle
apply plugin: 'com.android.application'
android {
Map<String, String> operatorMap = new HashMap<String, String>();
Map<String, Boolean> versionMap = new HashMap<String, Boolean>();
Map<String, Boolean> apkMap = new HashMap<String, Boolean>();
Map<String, Integer> versionCodeMap = new HashMap<String, Integer>();
Map<String, String> versionNameMap = new HashMap<String, String>();
def versionPropsFile = file('version.properties')
def Properties versionProps
String[] operators
if (versionPropsFile.canRead()) {
versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
String operatorstr = versionProps['operator']
operators = operatorstr.split(',')
String projectstr = versionProps['project']
String[] projects = projectstr.split(',')
for (String s : projects) {
setVersionCN(versionMap, versionCodeMap, versionNameMap, apkMap, s, versionProps['single.'+s].toBoolean(), versionProps['apk.'+s].toBoolean(), versionProps['version.code.'+s].toInteger(), versionProps['version.name.'+s])
}
}
compileSdkVersion 21
buildToolsVersion "23.0.0"
enforceUniquePackageName = false
lintOptions {
abortOnError false
}
dexOptions {
preDexLibraries = false
}
signingConfigs {
commonConfig {
storeFile file("android.keystore")
storePassword "xxxxxx"
keyAlias "android.keystore"
keyPassword "xxxxxx"
}
}
defaultConfig {
applicationId "xx.xx.xx.xx"
minSdkVersion 11
targetSdkVersion 15
versionCode versionProps['version.code'].toInteger()
versionName versionProps['version.name']
}
buildTypes {
release {
zipAlignEnabled true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
productFlavors {
//-------------------新增Flavor示例---------------------Start
// if (apkMap.get("OriginalTest")) {
// OriginalTest {}
// } else {
// printMSG("$name")
// }
//另外在version.properties文件中添加如下模板
// #OriginalTest
// #是否生成apk
// apk.OriginalTest=false
// #是否自定义版本信息
// single.OriginalTest=false
// #自定义版本号
// version.code.OriginalTest=0
// #自定义版本名
// version.name.OriginalTest=1.0.0.1
// #运营商
// operator.OriginalTest=0
//-------------------新增Flavor示例---------------------End
if (apkMap.get("OriginalTest")) {
OriginalTest {}
} else {
printMSG("$name")
}
}
productFlavors.all { flavor ->
sourceSets."$name".java.srcDirs += "build-types/"+name
signingConfig signingConfigs.commonConfig
operatorMap.put("$name", operators[versionProps['operator.'+name].toInteger()])
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']
manifest.srcFile 'src/main/AndroidManifest.xml'
java {
srcDirs = ['src/main/java']
exclude 'cn/com/wasu/main/tools/Constant.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(versionProps['prefix'],operatorMap, versionProps['suffix'])
clean {
delete += "output"
}
}
dependencies {
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 {
from('build/outputs/apk')
into(dir_debug)
include('*_debug.apk')
exclude("*-unaligned.apk");
exclude("*-unsigned.apk");
}
copy {
from('build/outputs/apk')
into(dir_release)
include('*.apk')
exclude('*_debug.apk')
exclude("*-unaligned.apk");
exclude("*-unsigned.apk");
}
}
/**
* 设置apk名称
* @param operatorMap 运营商map
* @param suffix 后缀
* @return
* Cannot set the value of read-only property 'outputFile'请看这里https://blog.csdn.net/u014300915/article/details/78355420
*/
def setup(prefix,Map<String, String> operatorMap, suffix) {
def today = new Date().format('yyyyMMddHHmm');
def today1 = new Date().format('yyyyMMdd');
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def version = variant.productFlavors[0].versionName;
def file = output.outputFile
def buildTypeOfApp = variant.buildType.name
def operator = operatorMap.get("${variant.productFlavors[0].name}")
def test = "%s%s%s_%s_%s"
def outputName = String.format(test, prefix, 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 + "_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, apkMap, String name, boolean isSingle, boolean isGenerateAPK, int code, String vname) {
versionMap.put(name, isSingle);
apkMap.put(name, isGenerateAPK);
versionCodeMap.put(name, code);
versionNameMap.put(name, vname);
}
def printMSG(String msg) {
println "不生成" + msg + "项目APK"
}
#version.properties
#Mon Aug 31 11:55:46 CST 2015
#默认版本名
version.name=1.0.0.1
#默认版本号
version.code=1
#运营商(多个以‘,’分隔)
operator=XX
#前缀
prefix=XXX_
#后缀
suffix=XXX
#项目名(多个以‘,’分隔)
project=OriginalTest
#OriginalTest
#是否生成apk
apk.OriginalTest=false
#是否自定义版本信息
single.OriginalTest=false
#自定义版本号
version.code.OriginalTest=0
#自定义版本名
version.name.OriginalTest=1.0.0.1
#运营商索引
operator.OriginalTest=0