使用 Android Studio 進(jìn)行多渠道打包超級簡單,廢話不多說,兩步:
兩種打包方式:
方式一:
1、在清單文件 AndroidManifest.xml 中添加如下帶
在 AndroidManifest.xml 文件 中 application 內(nèi) 添加:
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
這個是在使用友盟統(tǒng)計時的一個代碼示例,meta-data 中的 name 是可以更改的,根據(jù)實際需求如自己修。
2、在 build.gradle 進(jìn)行配置
在 build.gradle 文件中 android 下添加:
android {
........
productFlavors {
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
c360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "c360"]
}
}
}
只要進(jìn)行這兩步配置,就可以實現(xiàn)多渠道打包,非常簡單。 上面只配置了三個,如果想配置更多仿照這添加相應(yīng)渠道的就可以了
方式二:
1、在清單文件 AndroidManifest.xml 中添加如下帶
在 AndroidManifest.xml 文件 中 application 內(nèi) 添加:
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
這個是在使用友盟統(tǒng)計時的一個代碼示例,meta-data 中的 name 是可以更改的,根據(jù)實際需求如自己修。
2、在 build.gradle 進(jìn)行配置
在 build.gradle 文件中 android 下添加:
android {
........
productFlavors {
wandoujia {}
baidu {}
c360 {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [NAME_VALUE : name]
}
}
這樣配置也可以實現(xiàn)多渠道打包。
打包時設(shè)置包名
在 build.gradle 文件中 android 的 buildTypes 的 release 下添加:
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱為boohee_v1.0_2015-01-15_wandoujia.apk
def fileName = "myApk_${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
代碼不多,很簡單,不解釋,照著用就OK了。
完整代碼:
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "com.cfox.gradlemodel"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱為boohee_v1.0_2015-01-15_wandoujia.apk
def fileName = "myApk_${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
productFlavors {
wandoujia {}
baidu {}
xiaomi {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [NAME_VALUE : name]
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
}
最后
在打包時候選擇相應(yīng)的渠道就可以了。
下面對配置文件簡單說明一下:
我們看一下 meta-data 中 android:value="${UMENG_CHANNEL_VALUE}" 其中 ${UMENG_CHANNEL_VALUE} 是通配符,和 build.gradle 文件中 manifestPlaceholders = [UMENG_CHANNEL_VALUE: "c360"] 中的 UMENG_CHANNEL_VALUE 相對應(yīng)。
如果以上內(nèi)容收的不對請及時指出來,共同學(xué)習(xí),共同進(jìn)步。