CHANNEL_ID
示例:
AndroidManifest.xml
<meta-data
android:name="UMENG_CHANNEL"
android:value="${CHANNEL_VALUE}" />
<meta-data
android:name="CHANNEL_ID"
android:value="${CHANNEL_ID}" />
build.gradle
productFlavors { //多渠道打包 每个渠道指定不同的id
wandoujia {
manifestPlaceholders = [CHANNEL_VALUE: 'wandoujia', CHANNEL_ID:200]
}
qihu360 {
manifestPlaceholders = [CHANNEL_VALUE: '360', CHANNEL_ID:201]
}
baidu {
manifestPlaceholders = [CHANNEL_VALUE: 'baidu', CHANNEL_ID:202]
}
}
gradle to jar
gradle clean build --rerun-tasks
open file app-> build.gradle
Termianl run:
gradle makeJar
//打一个jar包
task clearJar(type: Delete) {
delete 'libs/sdk.jar'
}
task makeJar(type:org.gradle.api.tasks.bundling.Jar) {
//指定生成的jar名
baseName 'sdk'
//从哪里打包class文件
from('build/intermediates/classes/release/demo/tanxuze/com/myapplication/')
//打包到jar后的目录结构
into('demo/tanxuze/com/myapplication/')
//去掉不需要打包的目录和文件
exclude('test/', 'BuildConfig.class', 'R.class')
//去掉R$开头的文件
exclude{it.name.startsWith('R$');}
}
makeJar.dependsOn(clearJar, build)
gradle to apk
open file app->build.gradle
Terminal run:
gradle build
gradle aR / gradle release
android {
signingConfigs {
releaseSigningConfig {
keyAlias 'hawazhonghang'
keyPassword 'zhonghanghawa'
storeFile file('keys/hawarelease.keystore')
storePassword 'zhonghanghawa'
archivesBaseName = "Hawa"
}
}
buildTypes {
debug {
//语言版本 1---my,2---en,3---zh
//int Long = BuildConfig.Lang; code
buildConfigField("int", "Lang", "2")
//显示log
buildConfigField "boolean", "mDebug", "true"
//debug环境
buildConfigField "boolean", "mDebugHost", "true"
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.releaseSigningConfig
}
release {
//语言版本 1---my,2---en,3---zh
buildConfigField("int", "Lang", "1")
//不显示log
buildConfigField "boolean", "mDebug", "false"
//release环境
buildConfigField "boolean", "mDebugHost", "false"
//混淆
minifyEnabled true
//zipalign 优化
zipAlignEnabled true
//移出无用的resources文件
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.releaseSigningConfig
}
}
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
}
}
android {
signingConfigs {
release {
storeFile file("key/msypal.key")
storePassword "123456"
keyAlias 'msypalsdk'
keyPassword '123456'
}
}
buildTypes {
release {
buildConfigField("boolean", "LOG_DEBUG", "false")
signingConfig signingConfigs.release
minifyEnabled false
}
debug {
buildConfigField("boolean", "LOG_DEBUG", "true")
}
}
// productFlavors {
// web {
// manifestPlaceholders = [CHANNEL_VALUE: "web "]
// }
// baidu {
// manifestPlaceholders = [CHANNEL_VALUE: "baidu "]
// }
// c360 {
// manifestPlaceholders = [CHANNEL_VALUE: "c360 "]
// }
// qq {
// manifestPlaceholders = [CHANNEL_VALUE: "qq "]
// }
// wandoujia {
// manifestPlaceholders = [CHANNEL_VALUE: "wandoujia"]
// }
// }
productFlavors {
CHANNEL_1 {
// gradle 3.0.0 以上需要有这个
// dimension "app"
applicationId "${defaultConfig.applicationId}.channel_1"
versionCode 1
versionName "${defaultConfig.versionName}_channel_1"
resValue "string", "app_id", "00002"//现有的xml文件中不能出现该字段
buildConfigField "String", "channel", '"1"'
modifyAssetsConfig("CHANNEL_1")
}
CHANNEL_2 {
applicationId "${defaultConfig.applicationId}.channel_2"
versionCode 1
versionName "${defaultConfig.versionName}_channel_2"
resValue "string", "app_id", "00003"//现有的xml文件中不能出现该字段
buildConfigField "String", "channel", '"2"'
modifyAssetsConfig("CHANNEL_2")
}
CHANNEL_3 {
}
CHANNEL_4 {
}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
}
}
//自定义APK名称,多渠道打包
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
variant.productFlavors.each { flavor ->
def separator = "_"
def buildType = variant.variantData.variantConfiguration.buildType.name
def versionName = variant.versionName
def date = new Date()
def formattedDate = date.format('yyyy-MM-dd')
def apkName = versionName + separator + buildType + ".apk"
if (buildType == "release") {
apkName = versionName + separator + buildType + separator + formattedDate + ".apk"
}
output.outputFileName = apkName
// output.outputFileName = new File(outputFile.parent, apkName)
}
}
}
lintOptions {
abortOnError false
}
//android studio默认so文件加载目录为:src/main/jniLibs
//如在module的build.gradle按照如下方式,自定义了so文件加载目录请确保对应目录下只有armeabi目录
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
}
//tasks.whenTaskAdded { theTask ->
// println("theTask.name:" + theTask.name + " " + theTask.group)
// if (theTask.name.equalsIgnoreCase("debug")) {
// theTask.doFirst { modifyAssetsConfig(1) }
// }
// if (theTask.name.equalsIgnoreCase("release")) {
// theTask.doFirst { modifyAssetsConfig(0) }
// }
// if (theTask.name.equalsIgnoreCase("Yufa")) {
// theTask.doFirst { modifyAssetsConfig(2) }
// }
//}
def modifyAssetsConfig(String merchant_id) {
def assetDir = new File(project.projectDir.absolutePath + "/src/main/assets")
def configText = new File(assetDir.absolutePath + "/ct108sdkconfig.json")
String jsonStr = "{\n" +
"\t\"power_id\": \"功能编号\",\n" +
"\t\"order_name\": \"游戏金币\",\n" +
"\t\"order_desc\": \"1000金币\",\n" +
"\t\"app_id\": \"应用编号\",\n" +
"\t\"order_money\": \"计费金额\",\n" +
"\t\"merchant_id\": \"${merchant_id}\"\n" +
"}"
configText.setText(jsonStr, "utf-8")
}
Activity
try {
ApplicationInfo ai = getPackageManager().getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String myApiKey = bundle.getString("CHANNEL_NUMER");
Log.e("info", myApiKey);
} catch (Exception e) {
e.printStackTrace();
}
Log.e("info", BuildConfig.channel);
Log.e("info", BuildConfig.LOG_DEBUG + " --- ");
AndroidManifest.xml
<meta-data
android:name="CHANNEL_NUMER"
android:value="${CHANNEL_VALUE}" />