android:獲取渠道名+Android Studio多渠道打包+python 美團打包
阿新 • • 發佈:2019-01-22
需要使用友盟多渠道統計
/**
* 獲取application中指定的meta-data。本例中,呼叫方法時key就是UMENG_CHANNEL
* @return 如果沒有獲取成功(沒有對應值,或者異常),則返回值為空
*/
public static String getAppMetaData(Context ctx, String key) {
if (ctx == null || TextUtils.isEmpty(key)) {
return null;
}
String resultData = null ;
try {
PackageManager packageManager = ctx.getPackageManager();
if (packageManager != null) {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null ) {
if (applicationInfo.metaData != null) {
resultData = applicationInfo.metaData.getString(key);
}
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return resultData;
}
使用getAppMetaData(context,"getAppMetaData")
使用Android Studio多渠道打包步驟
1、在註冊檔案中新增
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}"/>
2、在app的gradle檔案中新增:
//簽名
signingConfigs {
myConfig {
storeFile file("G:\\key\\你的簽名檔案")
storePassword "簽名檔案密碼"
keyAlias "kayALias的值"
keyPassword "密碼"
}
}
buildTypes {
release {
minifyEnabled false//混淆
zipAlignEnabled true//壓縮優化
shrinkResources false//移除無用resource資源
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//指定proguard檔案
signingConfig signingConfigs.myConfig//簽名
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName1 = "APPNAME_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName1)
}
}
}
}
debug {
minifyEnabled false
zipAlignEnabled true
shrinkResources false
signingConfig signingConfigs.myConfig
}
}
productFlavors {
shenma21{}
shenma22{}
shenma23{}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
}