第一种方式,自带工具打包
1、在AndroidManifest.xml中application下配置,name和value可以指定其他字符串。
2、在主module中build.gradle添加渠道信息
android{
productFlavors {
wandoujia {
manifestPlaceholders = [CHANNEL_ID_VALUE: "wandoujia"]
}
baidu {
manifestPlaceholders = [CHANNEL_ID_VALUE: "baidu"]
}
c360 {
manifestPlaceholders = [CHANNEL_ID_VALUE: "c360"]
}
qishui {
manifestPlaceholders = [CHANNEL_ID_VALUE: "七水"]
}
zhou {
manifestPlaceholders = [CHANNEL_ID_VALUE: "周"]
}
}
}
wandoujia、qishui为渠道ID;wandoujia、七水为具体渠道值。
另外可以配置为另一种方式
productFlavors {
wandoujia {}
baidu {}
c360 {}
uc {}
qishui{}
zhou{}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [CHANNEL_ID_VALUE: name]
}
}
注意点
CHANNEL_ID渠道号不能全部都是数字,不是一个合法字符串,360或者360x,CHANNEL_ID_VALUE也不能。
在AS3.0上会出现
Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html
需要配置
flavorDimensions "1" //与versioncode值对应
build.gradle完整配置
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.qishui.channeldemo"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
flavorDimensions "1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// productFlavors {
// wandoujia {}
// baidu {}
// x360x {}
// uc {}
// qishui{}
// zhou{}
// productFlavors.all { flavor ->
// flavor.manifestPlaceholders = [CHANNEL_ID_VALUE: name]
// }
// }
productFlavors {
wandoujia {
manifestPlaceholders = [CHANNEL_ID_VALUE: "wandoujia"]
}
baidu {
manifestPlaceholders = [CHANNEL_ID_VALUE: "baidu"]
}
c360 {
manifestPlaceholders = [CHANNEL_ID_VALUE: "c360"]
}
qishui {
manifestPlaceholders = [CHANNEL_ID_VALUE: "七水"]
}
zhou {
manifestPlaceholders = [CHANNEL_ID_VALUE: "周"]
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
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'
}
查看当前渠道
获取渠道信息
public class Utils {
/**
* 获取渠道信息
* @param context
* @param key
* @return
*/
public static String getAppChannelData(Context context, String key) {
if (context == null || TextUtils.isEmpty(key)) {
return null;
}
String channelNumber = null;
try {
PackageManager packageManager = context.getPackageManager();
if (packageManager != null) {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null) {
if (applicationInfo.metaData != null) {
channelNumber = applicationInfo.metaData.getString(key);
}
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Log.e("Tag",channelNumber);
return channelNumber;
}
}
“CHANNEL_ID”对应application下多渠道ID标签
Utils.getAppChannelData(this,"CHANNEL_ID");
打包
和生成正是打包一样,不过选择你需要生成的app;
生成目录比较尴尬,可以选择D盘啥的,在项目外文件地址。
2、第二种,使用多渠道插件打包
插件使用地址
具体配置信息github提供了。
注意配置签名信息,完整
apply plugin: 'com.android.application'
apply plugin: 'channel'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.qishui.channeldemo"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
keyAlias 'qishui'
keyPassword 'zwxqishuichixi'
storeFile file('C:\\Users\\zhou\\Desktop\\app\\qishui.jks')
storePassword 'zwxqishuichixi'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
channel{
//多渠道包的输出目录,默认为new File(project.buildDir,"channel")
baseOutputDir = new File(project.buildDir,"channel")
//多渠道包的命名规则,默认为:${appName}-${versionName}-${versionCode}-${flavorName}-${buildType}
apkNameFormat ='${appName}-${versionName}-${versionCode}-${flavorName}-${buildType}'
//快速模式:生成渠道包时不进行校验(速度可以提升10倍以上,默认为false)
isFastMode = false
//buildTime的时间格式,默认格式:yyyyMMdd-HHmmss
buildTimeDateFormat = 'yyyyMMdd-HH:mm:ss'
//低内存模式(仅针对V2签名,默认为false):只把签名块、中央目录和EOCD读取到内存,不把最大头的内容块读取到内存,在手机上合成APK时,可以使用该模式
lowMemory = false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
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'
api 'com.leon.channel:helper:2.0.1'
}
获取渠道信息
String channel = ChannelReaderUtil.getChannel(getApplicationContext());
同样可以将apk生成到其他文件盘
baseOutputDir= new File("C:\\Users\\zhou\\Desktop\\app\\channel")