首發公衆號:Android程序員日記
作者:賢榆的榆
如果你覺得有幫助歡迎關注、讚賞、轉發
閱讀時間:4750字 9分鐘
對於Android 實戰項目這個系列,我已經寫了項目入門搭建、git項目管理、git分支管理及As實用插件,文末有對應的鏈接。這前面都是一些不可或缺的準備工作。那麼今天我給大家分享通過gradle來配置項目的release和debug的環境分離。
從Android Studio 1.5開始我就開始使用Android Studio 做Android 開發了。當時覺得很省事兒的就是,applicationId 、minSdkVersion 、targetSdkVersion 、versionCode 、versionName
這些東西以及簽名的key和密碼也都可以直接在build.gradle中進行配置。從Eclipse 過渡到Android Studio 我個人覺得還是受到了AS的不少好處。而將release和debug環境分離也算其中一個吧。
好了,話不多少,先看一下效果圖吧!
正如上圖大家看到,我們最終需要的效果是可以在同一臺手機上同時運行我們的正式app和測試app,它們有不同的圖標、不同的報名,還有就是測試換請求的服務器地址與正式環境也是不一樣的!
配置環境分離
1、配置config.gradle
其實這不是配置環境分離的必要條件,但是創建一個config.gradle文件,可以方便我們統一管理一些module下gradle文件用的變量,便於我們開發和維護。
- 切換爲project目錄結構。如圖
- 根目錄下創建一個config.gradle的文件
-
並在該文件中配置提到上文的
applicationId
等信息以及我們會用的依賴信息。具體配置如下:ext { supportLibrary = "27.1.0" android = [ compileSdkVersion: 27, minSdkVersion : 16, targetSdkVersion : 26, buildToolsVersion: "27.0.3", versionCode : 1, versionName : "2.3.0.0" ] //配置數據服務器的host地址 url = [ "debug" : "https://test.com", "release": "https://release.com" ] //配置圖片服務器的host地址 imgUrl = [ "debug" : "http://test.image.com/picture", "release": "https://release.image.com/picture" ] //統一配置依賴庫 dependencies = [ "multidex" : "com.android.support:multidex:1.0.1", "cardview" : "com.android.support:cardview-v7:${supportLibrary}", "constraint" : "com.android.support.constraint:constraint-layout:1.0.2", "recyclerview" : "com.android.support:recyclerview-v7:${supportLibrary}", "suppoutDesign": "com.android.support:design:${supportLibrary}" ] }
2、主項目的build.gradle中引入config.gradle
//這裏將config.gradle文件引用進來,app的gradle才能使用到裏面的配置項。
apply from: "config.gradle"
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
3、在module下的build.gradle中配置環境分離
apply plugin: 'com.android.application'
def cfg = rootProject.ext.android
def libraries =rootProject.ext.dependencies
android {
compileSdkVersion 27
defaultConfig {
applicationId cfg.appId
minSdkVersion cfg.minSdkVersion
targetSdkVersion cfg.targetSdkVersion
versionCode cfg.versionCode
versionName cfg.versionName
}
//配置簽名
signingConfigs {
debug {
storeFile file("../app/androidBook")
storePassword KEYSTORE_PASSWORD
keyAlias DEBUG_KEY_ALIAS
keyPassword DEBUG_KEY_PASSWORD
}
release {
storeFile file("../app/androidBook")
storePassword KEYSTORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}
buildTypes {
debug {
//是否壓縮資源
shrinkResources false
//是否混淆
minifyEnabled false
//混淆配置文件
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//簽名配置
signingConfig signingConfigs.debug
//在release包名後面添加了一個後綴形成了debug的包名
applicationIdSuffix '.inhouse'
//在AndroidManifest.xml中配置的app名稱,在這裏實現了動態配置
resValue "string", "app_name", "@string/app_name_debug"
//該數組的值都可以在AndroidManifest.xml文件中配置。
manifestPlaceholders = [
//這裏也是在AndroidManifest.xml中配置的app圖標,同樣在這裏寫了了不同的資源文件,從而實現了動態配置
app_icon: "@drawable/app_logo_debug",
]
//通過配置buildConfig來動態配置請求的host服務器和圖片服務器
buildConfigField "String", "HostUrl", "\"${url["release"]}\""
buildConfigField "String", "ImageUrl", "\"${imgUrl["release"]}\""
}
release {
shrinkResources true
minifyEnabled true
resValue "string", "app_name", "@string/app_name_release"
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
manifestPlaceholders = [
app_icon: "@drawable/app_logo",
]
buildConfigField "String", "HostUrl", "\"${url["uat"]}\""
buildConfigField "String", "ImageUrl", "\"${imgUrl["debug"]}\""
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//引用了config.gradle中配置的庫文件,從而實現在config.gradle實現版本的統一管理管理
implementation libraries.supportV7
implementation libraries.constraint
implementation libraries.supportDesign
implementation libraries.recyclerview
implementation libraries.cardview
}
注:
1、提醒看上文中的註解
2、上文中的簽名文件大家可以自行生成,build-->Generate Signed Apk
即可
3、signingConfigs中的簽名配置是放到了gradle.properties文件中,這樣比較直接寫在build.gradle中安全(配置如下)
KEYSTORE_PASSWORD=123456
DEBUG_KEY_PASSWORD=debug123456
RELEASE_KEY_PASSWORD=123456
DEBUG_KEY_ALIAS=debug
RELEASE_KEY_ALIAS=release
4、在上文中動態配置了app_name
和app_icon
最後需要將這兩個值配置在AndroidManifest.xml文件中(如下)
<application
android:allowBackup="true"
android:icon="${app_icon}"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
……
</application>
5、同上一步配置app_icon,AndroidManifest中其他的需要動態配置變量也可以通過對manifestPlaceholders
數組配置相應的鍵值對來實現,比如通過下面的代碼可以動態配置正式和測試環境所需要的appId、appKey等值
manifestPlaceholders = [
JPUSH_PKGNAME: "com.xianyu.app",
JPUSH_APPKEY : "1234567890asdfghhjk", //JPush上註冊的包名對應的appkey(托盤).正式Key
JPUSH_CHANNEL: "developer-default", //暫時填寫默認值即可.
]
4、運行我們的應用
好了,基本上配置工作就暫且做完。至於網絡環境如如何動態切換,我們這先不講,後面封裝網絡的時候會踢到。那麼,下面我們通過AS左下角的Build Variants 中app的release和deubug 打兩個包到手機上看一下效果,如下圖
這裏是我運行兩兩個包後,查看後臺的截圖,我們可以看到兩個app同時運行在手機上並且有着不同的名字。
本文源碼在這:
https://github.com/luorenyu/n...
最後,如果想要了解更多關於gradle的實用和實戰可在我的公衆號回覆「gfa」(英文書名的首字母)獲取下面這本介紹gradle的電子書
書名:Gradle for Android 中文版
作者:凱文·貝利格里姆斯著(美)
譯者:餘小樂譯
北京:電子工業出版社
豆瓣評分:8.8
推薦閱讀
系列文章
- 「Do.006」Android實戰項目(1)——我想說“開始吧”
- 「Do.007」Android實戰項目(2)——使用Github進行版本管理
- 「Do.008」Android實戰項目(3)——Git 分支管理模型
- 「Do.009」Android實戰項目(4)——AndroidStudio插件推薦(磨刀)
其他