一、在 AndroidManifest.xml 裏設置動態渠道變量
1
2
3
|
<!--
自動升級類型 self、_360、baidu -->
<meta-data
android:name="sdk_key_version_update_type"android:value="${CHANNEL_VALUE}">
</meta-data>
|
二、在 build.gradle 設置 productFlavors
方法 1:直接在 app:gradle 下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
android
{
defaultConfig
{
...
flavorDimensions"versionCode"
}
productFlavors
{
google
{
manifestPlaceholders
= [CHANNEL_VALUE:
"google"]
}
qh360
{
manifestPlaceholders
= [CHANNEL_VALUE:
"360"]
}
baidu
{
manifestPlaceholders
= [CHANNEL_VALUE:
"baidu"]
}
self
{
manifestPlaceholders
= [CHANNEL_VALUE:
"self"]
}
}
}
或者批量修改
defaultConfig
{
...
flavorDimensions"versionCode"
}
android
{
productFlavors
{
kuan
{}
xiaomi
{}
qh360
{}
baidu
{}
wandoujia
{}
}
productFlavors.all
{
flavor
-> flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
}
}
|
方法 2:通過快捷鍵 command + ; 打開配置界面。( Windows 對應的快捷鍵 Ctrl + Alt + Shift + S )並添加對應的配置渠道。如下圖:
注意:這裏僅修改 Name 即可,其他信息無特別需求不要修改
三、執行打包操作
四、執行打包命令 ./gradlew assembleRelease
五、配置簽名文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//簽名
signingConfigs
{
release{
keyAlias'1'
keyPassword
APK_SIGN_KEY_PASSWORD
storeFile
file('../snaillove.keystore')
storePassword
APK_SIGN_STORE_PASSWORD
}
debug{
keyAlias'1'
keyPassword
APK_SIGN_KEY_PASSWORD
storeFile
file('../snaillove.keystore')
storePassword
APK_SIGN_STORE_PASSWORD
}
}
buildTypes
{
release
{
//啓用混淆代碼的功能
minifyEnabledfalse
//指定混淆規則,需要壓縮優化的混淆要把proguard-android.txt換成proguard-android.txt
proguardFiles
getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
signingConfig
signingConfigs.release
}
}
|
注意:一定要把 signingConfigs 文件配置到 buildTypes 前面 否則會編譯時拋出異常 Could not find property ‘debugConfig’ on SigningConfig container.
配置自定義打包路徑與名稱
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
android
{
//
自定義APK輸出配置
applicationVariants.all
{ variant ->
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 =
"com_biniusports_app_bnsports_v${defaultConfig.versionName}_${getNowTime()}_${variant.productFlavors[0].name}.apk"
outputFileName
= fileName
}
}
}
}
//獲取時間戳
def
getNowTime() {
def
date =
new
Date()
def
now = date.format('yyyyMMdd')
returnnow
}
|
注意:Android Studio 版本大於 3.0 時不要使用v ariant.outputs.each 進行查找內容並且不要使用 new File(outputFile.parent, fileName),否則會拋出異常 Android Gradle 3.0.0-alpha2 plugin, Cannot set the value of read-only property ‘outputFile’。