在项目开发的过程中,总能碰到这样的需求,打包的时候发布两个相同版本的包,但是服务器地址不同,这时候就不想打两个包的时候只想运行一句打包命令打出两个服务器地址的包,这个时候就想到可以根据gradle配置打出差异化版本的包。
需求如上,gradle代码配置如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.haocang.mangomax"
minSdkVersion 21
targetSdkVersion 26
versionCode 10042
versionName "2.0.0.0042E"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
flavorDimensions "default"
//Enabling multidex support.
multiDexEnabled true
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath = true
arguments = [moduleName: project.getName()]
}
}
configurations.all {
exclude group: 'com.android.support', module: 'support-v13'
}
multiDexEnabled true
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
splits {
abi {
enable true
reset()
include 'armeabi-v7a'
universalApk false
}
}
dexOptions {
preDexLibraries false
javaMaxHeapSize "3072m"
}
signingConfigs {
release {
keyAlias 'commonsign'
keyPassword 'haocangapp'
storeFile file('./commonSign.jks')
// storeFile file('D:/AndroidStudio/Mango/commonSign.jks')
storePassword 'haocangapp'
}
loadSigningConfigs() //加载签名信息
}
buildTypes {
debug {
signingConfig signingConfigs.release
buildConfigField("String", "Address", "\"http://122.114.164.162:16801/\"")
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
productFlavors {
local {
buildConfigField("String", "Address", "\"http://192.168.8.246/\"")
}
online {
buildConfigField("String", "Address", "\"http://122.114.164.162:16801/\"")
}
}
applicationVariants.all { variant ->
variant.outputs.all {
if (!variant.buildType.isDebuggable()) {
variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + "/apk")
}
outputFileName = "taihe_APP_${defaultConfig.versionName}_${releaseTime()}_${variant.buildType.name}_${variant.productFlavors[0].name}.apk"
}
}
}
def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("GMT+08:00"))
}
def loadSigningConfigs() {
def Properties props = new Properties()
def propFile = file('../signing.properties') //加载properties文件
if (propFile.canRead()) {
props.load(new FileInputStream(propFile))
if (props != null && props.containsKey('RELEASE_STORE_FILE') && props.containsKey('RELEASE_STORE_PASSWORD') &&
props.containsKey('RELEASE_KEY_ALIAS') && props.containsKey('RELEASE_KEY_PASSWORD')) {
android.signingConfigs.release.storeFile = file(props['RELEASE_STORE_FILE'])
android.signingConfigs.release.storePassword = props['RELEASE_STORE_PASSWORD']
android.signingConfigs.release.keyAlias = props['RELEASE_KEY_ALIAS']
android.signingConfigs.release.keyPassword = props['RELEASE_KEY_PASSWORD']
} else {
android.buildTypes.release.signingConfig = null
}
} else {
android.buildTypes.release.signingConfig = null
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// implementation project(':equiment')
// implementation project(path: ':mango-lib')
// implementation project(':messge')
// implementation project(':monitor')
// implementation project(':patrol')
// implementation project(':repair')
// implementation project(':fault')
// implementation project(':curve')
// implementation project(':maintain')
implementation('com.alibaba:arouter-api:1.3.1') {
exclude group: 'com.android.support', module: 'support-v4'
}
annotationProcessor 'com.alibaba:arouter-compiler:1.1.4'
api project(':water-link')
api project(':hc-lib')
}
关键代码:
flavorDimensions "default"
productFlavors { local { buildConfigField("String", "Address", "\"http://192.168.8.246/\"") } online { buildConfigField("String", "Address", "\"http://122.114.164.162:16801/\"") } } applicationVariants.all { variant -> variant.outputs.all { if (!variant.buildType.isDebuggable()) { variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + "/apk") } outputFileName = "taihe_APP_${defaultConfig.versionName}_${releaseTime()}_${variant.buildType.name}_${variant.productFlavors[0].name}.apk" } }