其他分享
首页 > 其他分享> > Android开发分渠道打包之友盟篇

Android开发分渠道打包之友盟篇

作者:互联网

第一步:在清单文件AndroidManifest.xml中application模块中加入:

<meta-data
    android:name="UMENG_CHANNEL"
    android:value="${UMENG_CHANNEL_VALUE}"/>


第二步:在app的build.gradle中的android部分加入:

 flavorDimensions ""
    productFlavors{
        wandoujia{}
        xiaomi{}
        huaiwei{}
        oppo{}
        vivo{}
        meizu{}
        yingyongbao{}
    }
    productFlavors.all{
        flavor ->
            flavor.manifestPlaceholders =[UMENG_CHANNEL_VALUE : name]
    }
    applicationVariants.all{ variant ->
        variant.outputs.all{ output->
                    def outputFile = output.outputFile
                    if(outputFile!=null && outputFile.name.endsWith(".apk")){
                        def fileName = outputFile.name.replace(".apk","-${defaultConfig.versionName}.apk")
                        outputFileName = fileName
                    }

            }
    }

第三步:在示例ApplicationTest(在任何地方都可以)中获取meta-data

private String getChannel(){
        PackageManager pm = getPackageManager();
        ApplicationInfo applicationInfo = null;
        try {
            applicationInfo = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            return applicationInfo.metaData.getString("UMENG_CHANNEL");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }


第四步:build ->Generate Signed Bundle or apk -> apk,打包apk

第五步:验证apk的渠道信息,

标签:outputFile,name,之友,apk,盟篇,Android,PackageManager,flavor,applicationInfo
来源: https://blog.csdn.net/weixin_38576356/article/details/105769515