多渠道打包

来源:互联网 发布:云计算的体系结构 编辑:程序博客网 时间:2024/06/04 19:05

我们在发布开发好的应用时,往往都是发布到各个手机市场上的,比如360手机市场、豌豆荚、应用宝等,这样方便用户下载,这些手机市场就是我们这边说的渠道。一般我们把应用发布到各个渠道时需要对应用趋势、应用下载量用户行为、属性以及其他一下方面进行分析,通常使用第三方服务进行统计比如友盟和百度统计。

在项目中接入友盟统计sdk

友盟快速通道:http://www.umeng.com/

官方文档中很详细的记录了接入的步骤,此处并无难点。

修改项目中AndroidManifest.xml文件

<!--友盟相关的meta-data--><!--友盟AppKey-->     <meta-data         android:name="UMENG_APPKEY"         android:value="58ad490876661308d6000852"/>         <!--渠道号-->     <meta-data         android:name="UMENG_CHANNEL"         android:value="${UMENG_CHANNEL_VALUE}"/>

这里的value使用一个占位符${UMENG_CHANNEL_VALUE},方便等下在build.gradle文件中操作,占位符的命名随意,但是一定要和build.gradle文件的中匹配。

修改Module中的build.gradle文件

build.gradle文件:

apply plugin: 'com.android.application'android {    compileSdkVersion 24    buildToolsVersion "24.0.2"    defaultConfig {        applicationId "com.wyy.youmengtest"        minSdkVersion 15        targetSdkVersion 24        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"        multiDexEnabled true//突破应用方法数不能超过65535的限制        //这里的UMENG_CHANNEL_VALUE与AndroidManifest.xml中设置的占位符一致        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]    }    //添加文件签名配置    signingConfigs {        debug {}        //为我们的release包添加签名文件配置        release {            storeFile file("monker.jks")            storePassword "wyy123"            keyAlias "monker"            keyPassword "wyy123"        }    }    buildTypes {        release {            minifyEnabled false//是否启用混淆            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'            signingConfig signingConfigs.release            //修改生成的apk包的名字为渠道名            applicationVariants.all { variant ->                variant.outputs.each { output ->                    def outFile = output.outputFile                    if (outFile != null && outFile.name.endsWith(".apk")) {                        def fileName = "${variant.productFlavors[0].name}.apk"                        output.outputFile = new File(outFile.parent, fileName);                    }                }            }        }    }    //设置多渠道打包的渠道号    productFlavors {        xiaomi {//            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]//            resValue("string", "app_name", "xiaomi")        }        wandoujia {//            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]//            resValue("string", "app_name", "wandoujia")        }    }    //循环设置生成多渠道安装包,上面的渠道设置中可以放空,也可以单个设置,把上面productFlavors中注释的内容解开就行    productFlavors.all {        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]    }}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:24.2.1'    testCompile 'junit:junit:4.12'    compile 'com.umeng.analytics:analytics:latest.integration'}

遇到的问题

错误提示:Could not get unknown property ‘release’ for SigningConfig container.

这是signingConfigs没有放到buildTypes前面,导致release无法找到。

这里写图片描述

这是在使用此类方式进行打包的时候,app_name冲突了

productFlavors {    xiaomi {           manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]           resValue("string", "app_name", "xiaomi")    }    wandoujia {           manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]           resValue("string", "app_name", "wandoujia")    }}}

只要在打包时,把res/values/strings.xml文件中的app_name注释掉就行

原创粉丝点击