Android几种多渠道打包

https://www.cnblogs.com/sunjiachen/p/11457630.html

我这里采用的是android studio的方法

第一,修改app的build.gradle


apply plugin: 'com.android.application'

android {
compileSdkVersion 27
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.example.james.cookie"
minSdkVersion 22
targetSdkVersion 27
versionCode 3
versionName "2.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// 1 渠道名称
productFlavors {
baidu {}
xiaomi {}
qihu360 {}
huawei {}
}
// 2 自动替换 AndroidManifest.xml 里面的metadata
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL: name]
}
// 3 设置输出APK名称
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "driver_${variant.productFlavors[0].name}_v${defaultConfig.versionName}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}

}



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:27.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile'com.mcxiaoke.volley:library:1.0.+'

}

第二,修改app的AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.james.cookie">

<!-- 允许应用程序联网,以便向我们的服务器端发送数据 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL}" />

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".OkActivity">
</activity>
</application>

</manifest>

第三,代码中获取meta 数据


private String getChannel() {
try {
PackageManager pm = getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
String channel = appInfo.metaData.getString("UMENG_CHANNEL"); // key为<meta-data>标签中的name
if (!TextUtils.isEmpty(channel)) {
return channel;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


posted on 2020-06-15 16:18  katago  阅读(1919)  评论(0编辑  收藏  举报