1,defaultConfig 默认配置
defaultConfig { applicationId "com.xx.dxf.xxx" //包名 minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0.5" }
2,signingConfigs 签名信息配置
signingConfigs {
release {
//发布版本的签名配置
storeFile file(props[
'KEYSTORE_FILE'
])
keyAlias props[
'KEY_ALIAS'
]
storePassword props[
'KEYSTORE_PWD'
]
keyPassword props[
'KEY_PWD'
]
}
debug {
//调试版本的签名配置
storeFile file(props[
'DEBUG_KEYSTORE'
])
keyAlias props[
'DEBUG_ALIAS'
]
storePassword props[
'DEBUG_KEYSTORE_PWD'
]
keyPassword props[
'DEBUG_KEY_PWD'
]
}
}
签名配置文件signing.properties:
1
2
3
4
5
6
7
8
9
|
KEYSTORE_FILE = release.keystore
KEY_ALIAS = wildcreek
KEYSTORE_PWD= xxx
KEY_PWD= xxx
DEBUG_KEYSTORE= debug.keystore
DEBUG_ALIAS= androiddebugkey
DEBUG_KEYSTORE_PWD= android
DEBUG_KEY_PWD= android
|
3, buildTypes
<code> 注意:
1
.minifyEnabled 会对代码进行混淆和压缩,shrinkResources 会对比R文件对无用资源进行删除
2
.minifyEnabled 设置为
true
时shrinkResources 的设置才会生效
</code>
<code>buildTypes {
release {
debuggable
true
minifyEnabled
true
//启用Proguard
shrinkResources
true
//是否清理无用资源,依赖于minifyEnabled
zipAlignEnabled
true
//是否启用zipAlign压缩
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile(
'proguard-android.txt'
),
'proguard-rules.txt'
}
debug {
debuggable
true
minifyEnabled
false
//不启用Proguard
shrinkResources
false
//是否清理无用资源,依赖于minifyEnabled
zipAlignEnabled
false
//是否启用zipAlign压缩
signingConfig signingConfigs.debug
}
}</code>
4,productFlavors
自定义BuildConfig,工程编译后会生成BuildConfig类,该类会包含自定义字段。
假设工程包含beijing和shandong两个productFlavors,且拥有各自不同的服务器等配置信息。
<code>productFlavors{
beijing{
buildConfigField(
"boolean"
,
"IS_LOCAL"
,
"false"
)
buildConfigField(
"String"
,
"SERVER_NAME"
,
"\"BJ\""
)
buildConfigField(
"String"
,
"LOGIN_API"
,
"\"login\""
)
}
shandong{
buildConfigField(
"boolean"
,
"IS_LOCAL"
,
"false"
)
buildConfigField(
"String"
,
"SERVER_NAME"
,
"\"SD\""
)
buildConfigField(
"String"
,
"LOGIN_API"
,
"\"loginsd\""
)
}
}</code>
5,打包apk重命名
<code>def releaseTime() {
return
new
Date().format(
"yyyy-MM-dd"
, TimeZone.getTimeZone(
"UTC"
))
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if
(variant.buildType.name.equals(
'release'
)) {
def fileName =
"Demo_v${defaultConfig.versionName}_${releaseTime()}_release.apk"
if
(variant.flavorName.equals(
"beijing"
)){
fileName =
"Demo_v${defaultConfig.versionName}_${releaseTime()}_beijing_release.apk"
}
else
if
(variant.flavorName.equals(
"shandong"
)){
fileName =
"Demo_v${defaultConfig.versionName}_${releaseTime()}_shandong_release.apk"
}
output.outputFile =
new
File(outputFile.parent, fileName)
}
else
if
(variant.buildType.name.equals(
'debug'
)){
def fileName =
"Demo_v${defaultConfig.versionName}_${releaseTime()}_debug.apk"
if
(variant.flavorName.equals(
"beijing"
)){
fileName =
"Demo_v${defaultConfig.versionName}_${releaseTime()}_beijing_debug.apk"
}
else
if
(variant.flavorName.equals(
"shandong"
)){
fileName =
"Demo_v${defaultConfig.versionName}_${releaseTime()}_shandong_debug.apk"
}
output.outputFile =
new
File(outputFile.parent, fileName)
}
}
}</code>
6,dependencies 依赖配置
不同buildTypes 和productFlavors 依赖不同的jar的配置情况
<code> dependencies {
compile
'com.android.support:appcompat-v7:22.2.1'
compile
'com.android.support:support-v4:22.2.1'
compile
'com.google.code.gson:gson:2.2.1'
compile
'net.robinx:lib.blur:1.0.1'
debugCompile
'com.squareup.leakcanary:leakcanary-android:1.5'
releaseCompile
'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
compile fileTree(dir:
'libs'
, exclude:[
"*mtclib*.jar"
,
"*peerconnection*.jar"
], include:
"*.jar"
)
beijingCompile files(
"libs/mtclib_0908_bj.jar"
)
beijingCompile files(
"libs/libjingle_peerconnection_bj.jar"
)
shandongCompile files(
"libs/mtclib_170110_sd.jar"
)
shandongCompile files(
"libs/libjingle_peerconnection_sd.jar"
)
}</code>