由於國內Android應用市場品種太多,血統不純,每次上線App都打包打到手軟!好在現在有了Android studio和gradle,一條命令打包各種渠道版本App!
多渠道打包實現步驟主要分兩步:
- 在AndroidManifest.xml的application標籤下定義UMENG_CHANNEL佔位符。
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
- 修改app目錄下的build.gradle腳本,在android閉包中添加 productFlavors 屬性,配置替換佔位符的渠道標識。
android {
// 友盟多渠道打包
/* productFlavors {
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
_360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "_360"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
}*/
productFlavors {
xiaomi {}
_360 {}
baidu {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
完成上面之後點擊AS右邊Gradle後雙擊這寫文件就可以單個運行
我們是要一鍵打包所有渠道APK點擊assemble文件,就會一鍵生成所有Apk。
完成上面的操作只是生成所有APK,但都沒有簽名不能上線,下面就需要在下面設置簽名文件。設置完後刪除之前apk再重新點擊assemble。
生成的APK可以在這裏看到
還有一種方法就是配置好後,用最基本的方法簽名打包
修改app目錄下的build.gradle腳本如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "包名"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
// dex突破65535的限制
multiDexEnabled true
// 默認是umeng的渠道
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
useLibrary 'org.apache.http.legacy'
lintOptions {
disable 'MissingTranslation', 'ExtraTranslation'
abortOnError false
}
signingConfigs {
// D://APK簽名密鑰/monkey.Keystore是簽名文件所在全路徑
// debug時打包用的簽名
debug {
// keyAlias 'debug'
// keyPassword 'password'
// storeFile file('D://APK簽名密鑰/monkey.Keystore')
// storePassword 'password'
}
// release時打包用的簽名
release {
keyAlias 'release'
keyPassword 'password'
storeFile file('D://APK簽名密鑰/monkey.Keystore')
storePassword 'password'
}
}
buildTypes {
debug {
// 顯示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}
release {
// 不顯示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
minifyEnabled true
zipAlignEnabled true
// 移除無用的resource文件
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱爲appname_v1.0_2015-09-15_09-57-58_wandoujia.apk
// 下面的appname改爲你自己的app name
def fileName = "appname_v${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
// 友盟多渠道打包
/* productFlavors {
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
_360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "_360"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
}*/
productFlavors {
xiaomi {}
_360 {}
baidu {}
wandoujia {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile files('libs/volley.jar')
compile files('libs/gson-2.2.1.jar')
compile files('libs/commons-lang3-3.3.2.jar')
}
點擊Build 》Generate Signed APK
沒有KeyStore的就新建一個
填寫上面的信息
輸入你剛剛創建的文件和密碼
選擇你要生成想要渠道的Apk