Gradle多渠道多環境打包自動重新命名
阿新 • • 發佈:2019-02-09
概述
在公司開發時,一般會模擬出幾套環境,ps,測試環境、準生產環境、釋出環境等,同時 Android 市場繁多,為了方便後期資料分析,在釋出的時候還要新增一個渠道統計,一般會用到友盟統計,這就給我們的打包帶來了麻煩。
gradle
好在android studio的gradle指令碼十分強大,給我們的構建帶來了方便。
首先,我們定義一個配置檔案,方便後面管理
gradle.properties
,因為我的專案是區分中英文的,所以定義了兩個packageName
,打包的時候也算是兩個產品
gradle.properties:
// build_tools版本號
ANDROID_BUILD_TOOLS_VERSION=22.0 .1
// build_dk版本號
ANDROID_BUILD_SDK_VERSION=22
// version_name
VERSION_NAME=2.1
// version_code
VERSION_CODE=2100
// 包名
// PACKAGE= xxxx
// english version
PACKAGE= xxxx.en
//minSdkVersion
ANDROID_BUILD_MIN_SDK_VERSION=15
//targetSdkVersion
ANDROID_BUILD_TARGET_SDK_VERSION=22
// grade_version
GRADLE = com.android.tools.build:gradle:1.5 .0
build.gradle(app 工程目錄下):
//宣告是Android程式,和library區別
apply plugin: 'com.android.application'
// 因為專案中使用了retrolambda
apply plugin: 'me.tatarka.retrolambda'
// 打包時間
def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
android {
//編譯SDK的版本
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
// build tools的版本
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
//應用的包名,定義在gradle.properties
applicationId project.PACKAGE
minSdkVersion Integer.parseInt(ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(ANDROID_BUILD_TARGET_SDK_VERSION)
versionCode Integer.parseInt(project.VERSION_CODE)
versionName project.VERSION_NAME
// dex突破65535的限制,為大程式而設
multiDexEnabled true
// AndroidManifest.xml 裡面CHANNEL的value為 ${CHANNEL_VALUE}
manifestPlaceholders = [CHANNEL_VALUE: "name"]
}
//執行lint檢查,有任何的錯誤或者警告提示,都會終止構建,我們可以將其關掉。
lintOptions {
checkReleaseBuilds false
abortOnError false
// 防止在釋出的時候出現因MissingTranslation導致Build Failed!
disable 'MissingTranslation'
}
dexOptions {
incremental true
javaMaxHeapSize "8g"
jumboMode = true
preDexLibraries = false
threadCount ="8"
}
//簽名
signingConfigs {
debug {
}
relealse {
storeFile file('xxx.keystore')
storePassword '***'
keyAlias 'xxx'
keyPassword '***'
}
}
// 打包
buildTypes {
//debug 版本
// debug {
// buildConfigField "int", "ENV", "0"
// minifyEnabled true
// zipAlignEnabled true
// shrinkResources true
// proguardFile 'proguard-rules.pro'
// signingConfig signingConfigs.relealse
// }
// pre {
// buildConfigField "int", "ENV", "1"
// minifyEnabled false
// zipAlignEnabled false
// shrinkResources false
// signingConfig signingConfigs.relealse
// }
// online {
// buildConfigField "int", "ENV", "2"
// minifyEnabled false
// zipAlignEnabled false
// shrinkResources false
// signingConfig signingConfigs.relealse
// }
release {
// 定義ENV變數,可以通過BuildConfig.ENV 來獲取,進行一些介面環境變化的操作
buildConfigField "int", "ENV", "2"
//混淆
minifyEnabled true
//Zipalign優化
zipAlignEnabled true
// 移除無用的resource檔案
shrinkResources true
//混淆配置檔案
proguardFile 'proguard-rules.pro'
//簽名
signingConfig signingConfigs.relealse
}
}
//渠道Flavors
productFlavors {
Market_Google_Play {}
// Market_xiaomi {}
// Market_wandoujia {}
// Market_Default {}
// Market_Amazon {}
// Market_yingyongbao {}
// Market_360 {}
// Market_baidu {}
}
// Java版本
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
// 替換manifest中定義的佔位符
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
}
// apk輸出重新命名
applicationVariants.all { variant ->
variant.outputs.each { output ->
// oldName
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')){
// 獲取中英文版本
def isEn = variant.applicationId.endsWith('.en')
def newName = ''
def version= "-v${defaultConfig.versionName}"
def time = "-${releaseTime()}.apk"
def enName = 'appEN'
def cnName = 'appCN'
// release版本
// if(variant.buildType.name.equals('release')) {
//
// // 獲取渠道號
// def productFlavor = variant.productFlavors[0].name
//
// if (isEn) {
// newName = enName + version + '-' + productFlavor + time
//
// } else {
// newName = cnName + version + '-' + productFlavor + time
// }
// }else if (variant.buildType.name.equals('pre')){
// if (isEn) {
// newName = enName + version + '-pre-online' + time
//
// } else {
// newName = cnName + version + '-pre-online' + time
// }
// }else if (variant.buildType.name.equals('online')){
// if (isEn) {
// newName = enName + version + '-online' + time
//
// } else {
// newName = cnName + version + '-online' + time
// }
// }else{
if (isEn) {
newName = enName + version + '-debug' + time
} else {
newName = cnName + version + '-debug' + time
}
// }
output.outputFile = new File(outputFile.parent, newName)
}
}
}
}
retrolambda {
javaVersion JavaVersion.VERSION_1_7
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.1'
compile 'com.annimon:stream:1.0.1'
compile project(':mylibrary')
}
最後會在app/build/outputs/apk下生成相應的檔案
debug:name-version-debug-time.apk
release: name-version-productFlavors-time.apk
AndroidManifest.xml
<application>
<meta-data
android:name="UMENG_APPKEY"
android:value="54d32a3afd98c511cf000629" />
<meta-data
android:name="UMENG_CHANNEL"
android:value="${CHANNEL_VALUE}" />
</application>