需求
現在項目已經實現jenkins自動打包,現在需要在jenkns中的shell修改一個參數即可打包生成各種版本的app,例如 生成:
- 測試環境有調試信息的app
- 預上線環境有調試信息
- 預上線環境無調試信息
- 正式環境有調試信息,
- 正式環境(發包版本無調試信息)
順便說下 目前生成的文件是通過 gradle 命令打包生成的.然後通過shell命令從工作空間將apk拷貝出來.供測試人員下載.
解決思路
因爲gradle 是groovy 語法 ,那麼我定義各種域名後在 buildTypes 裏面添加各種類型的版本.讀取各種版本的域名.
還有一些其他技巧 比如打包生成的apk 統一放在 output/main 目錄下 除了debug 版本
調試信息的含義
調試信息是可以在app裏面看到一些網絡請求參數,返回值,app的crash.以懸浮窗實現
安利下我之前寫的神器,開發工具幫助庫
http://blog.csdn.net/liudao7994/article/details/55510782
配置文件
apply plugin: 'com.android.application'
android {
//DOMAIN
def API_RELEASE_DOMAIN = "\"正式環境\""
//正式環境
def API_RELEASE = "\"https://正式環境\""
//DOMAIN
def API_TEST_DOMAIN = "\"測試環境\""
//測試環境
def API_TEST = "\"http://測試環境\""
//DOMAIN
//預發環境
def API_PRE_RELEASE = "\"https://預發環境\""
def API_PRE_RELEASE_DOMAIN = "\"預發環境\""
//開發環境DOMAIN
def API_DEV_DOMAIN = "\"開發環境\""
//開發環境
def API_DEV_HOST = "\"http://開發環境\""
compileSdkVersion 25
buildToolsVersion '26.0.2'
//23 編譯版本需要添加這句話,低於23 不需要
useLibrary 'org.apache.http.legacy'
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
defaultConfig {
multiDexEnabled true
applicationId "com.a1chemic.promotion.android"
minSdkVersion 12
targetSdkVersion 23
versionCode 201
versionName "2.0.1"
buildConfigField("String", "API_HOST", "${API_DEV_HOST}")
buildConfigField("String", "API_DOMAN", "${API_DEV_DOMAIN}")
ndk {
abiFilters "armeabi", "x86", "mips"
}
}
dexOptions {
javaMaxHeapSize "4g"
}
aaptOptions {
cruncherEnabled = false
useNewCruncher = false
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
signingConfigs {
release {
v1SigningEnabled true
v2SigningEnabled true
}
}
loadSigningConfigs()
buildTypes {
//開發 自己用的
debug {
buildConfigField "String", "API_HOST", "${API_DEV_HOST}"//API Host
buildConfigField "String", "API_DOMAN", "${API_DEV_DOMAIN}"//測試地址
buildConfigField "boolean", "SING_DEBUG", "true"
buildConfigField "boolean", "DEBUGINFO", "true"
resValue "string", "AppName", "應用開發板"
}
//測試 顯示調試
debugTest {
signingConfig signingConfigs.release
buildConfigField "String", "API_HOST", "${API_TEST}"//API Host
buildConfigField "String", "API_DOMAN", "${API_TEST_DOMAIN}"//測試地址
buildConfigField "boolean", "SING_DEBUG", "true"
buildConfigField "boolean", "DEBUGINFO", "true"
matchingFallbacks = ['debug']
resValue "string", "AppName", "應用測試版"
}
//預上線 顯示調試
preRelease {
signingConfig signingConfigs.release
buildConfigField "String", "API_HOST", "${API_PRE_RELEASE}"//預上線 API Host
buildConfigField "String", "API_DOMAN", "${API_PRE_RELEASE_DOMAIN}"//預上線
buildConfigField "boolean", "SING_DEBUG", "false"
buildConfigField "boolean", "DEBUGINFO", "true"
matchingFallbacks = ['debug']
resValue "string", "AppName", "應用預覽版"
}
//正式 有調試信息
releasePE {
minifyEnabled false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "API_HOST", "${API_RELEASE}"//API Host
buildConfigField "String", "API_DOMAN", "${API_RELEASE_DOMAIN}"//正式地址
buildConfigField "boolean", "SING_DEBUG", "false"
buildConfigField "boolean", "DEBUGINFO", "true"
matchingFallbacks = ['release']
resValue "string", "AppName", "應用名稱"
}
//預上線Demo 無調試信息
preReleaseDemo {
signingConfig signingConfigs.release
buildConfigField "String", "API_HOST", "${API_PRE_RELEASE}"//預上線 API Host
buildConfigField "String", "API_DOMAN", "${API_PRE_RELEASE_DOMAIN}"//預上線
buildConfigField "boolean", "SING_DEBUG", "false"
buildConfigField "boolean", "DEBUGINFO", "false"
matchingFallbacks = ['release']
resValue "string", "AppName", "應用預覽版"
}
//正式 無調試信息
release {
debuggable false
minifyEnabled false //啓用Proguard --正常應該是true
shrinkResources false //是否清理無用資源,依賴於minifyEnabled
zipAlignEnabled true //是否啓用zipAlign壓縮
minifyEnabled false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "API_HOST", "${API_RELEASE}"//
buildConfigField "String", "API_DOMAN", "${API_RELEASE_DOMAIN}"//
buildConfigField "boolean", "SING_DEBUG", "false"
buildConfigField "boolean", "DEBUGINFO", "false"
resValue "string", "AppName", "正式名稱"
}
}
sourceSets {
main {
jniLibs.srcDir 'libs'
//說明so的路徑爲該libs路徑,關聯所有地圖SDK的so文件
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/resources']
res.srcDirs = ['src/main/res']
assets.srcDirs = ['src/main/assets']
}
vivo.res.srcDirs = ['src/main/res-vivo']
}
flavorDimensions "default"
productFlavors {
main { dimension "default" }
// vivo { dimension "default" } //vivo
// baidu { dimension "default" } //百度
// tencent { dimension "default" } //應用寶
// xiaomi { dimension "default" } //小米
// qh360 { dimension "default" } //360
// huawei { dimension "default" } //華爲
// wandoujia { dimension "default" } //豌豆莢
// vivo { dimension "default" } //vivo
// oppo { dimension "default" } //oppo
// meizu { dimension "default" } //魅族
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
applicationVariants.all { variant ->
if (variant.buildType.name != "debug") {//防止AS無法安裝debug包(apk)
variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + "/app/build/outputs/apk/main")
}
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱爲com_biniusports_app_bnsports_v1.0.0_20171205_baidu.apk
def fileName = "應用_${variant.buildType.name}_V${defaultConfig.versionName}_${getNowTime()}_${variant.productFlavors[0].name}.apk"
outputFileName = fileName
}
}
}
}
//獲取時間戳
def getNowTime() {
def date = new Date()
def now = date.format('yyyyMMdd')
return now
}
/**
* 加載簽名配置文件
*/
def loadSigningConfigs() {
Properties props = new Properties()
def propFile = file('../signing.properties')
if (propFile.canRead()) {
props.load(new FileInputStream(propFile))
//發佈版本的簽名配置
android.signingConfigs.release.storeFile = file(props['KEYSTORE_FILE'])
android.signingConfigs.release.storePassword = props['KEYSTORE_PWD']
android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
android.signingConfigs.release.keyPassword = props['KEY_PWD']
/* //調試版本的簽名配置
android.signingConfigs.debug.storeFile = file(props['DEBUG_KEYSTORE'])
android.signingConfigs.debug.storePassword = props['DEBUG_KEYSTORE_PWD']
android.signingConfigs.debug.keyAlias = props['DEBUG_ALIAS']
android.signingConfigs.debug.keyPassword = props['DEBUG_KEY_PWD']*/
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
compile files('libs/android-support-v7-recyclerview.jar')
}
jenkins shell命令
#測試版-測試環境 - 分支 dev:Test
#預覽版-預生產環境-分支 dev:Pre
#生產版-生產環境 - 分支 dev:PE
#演示版-預生產環境-分支 master:Demo
#商店版-生產環境 - 分支 master:AppStore
DEFINE=AppStore
if [ "${DEFINE}" == "Test" ]; then
AssembleBuild="assembledebugTest"
elif [ "${DEFINE}" == "Pre" ]; then
AssembleBuild="assemblepreRelease"
elif [ "${DEFINE}" == "PE" ]; then
AssembleBuild="assemblereleasePE"
elif [ "${DEFINE}" == "Demo" ]; then
AssembleBuild="assemblepreReleaseDemo"
elif [ "${DEFINE}" == "AppStore" ]; then
AssembleBuild="assemblerelease"
fi
gradle clean ${AssembleBuild}
如何使用域名
BuildConfig.API_HOST 就行了
圖標的修改
//預上線Demo 無調試信息
preReleaseDemo {
signingConfig signingConfigs.release
buildConfigField "String", "API_HOST", "${API_PRE_RELEASE}"//預上線 API Host
buildConfigField "String", "API_DOMAN", "${API_PRE_RELEASE_DOMAIN}"//預上線
buildConfigField "boolean", "SING_DEBUG", "false"
buildConfigField "boolean", "DEBUGINFO", "false"
matchingFallbacks = ['release']
resValue "string", "AppName", "應用預覽版"
manifestPlaceholders =[app_icon:"@drawable/app_icon_test"]
}
manifestPlaceholders =[app_icon:"@drawable/app_icon_test"]
//app_icon_test 這個就是需要的圖標
在AndroidManifest.xml中替換\
android:icon=”${app_icon}”
就做這兩步就OK了
TODU
後面有時間還要做的是使用命令行進行360加固,這樣打包出來之後就可以直接發佈市場了.