Android学习笔记--多渠道打包

多渠道打包

什么是多渠道打包?

渠道是国内Android平台特有的名词,由于Google Play在国内无法使用,所以出现了众多的应用市场,360市场,应用宝等,一个渠道代表一个应用市场。

为什么要多渠道打包?

如果你的应用只针对一个平台,那么无需多渠道(几乎不可能)。只要应用提供了多种下载途径,那么会用到多渠道,例如官网下载,豌豆荚下载等。

多渠道的意义

  1. 通过多渠道,可以更加详细的统计app被下载的途径。
  2. 通过多渠道,可以查看app在每个渠道上的表现。
  3. 通过多渠道,更有利于app的推广

多渠道打包原理

为每个平台或市场的apk指定一个唯一的标识符,通过标识符区分不同的平台。通常在Mainifest.xml中指定。

集成友盟

添加对友盟库的依赖

`compile 'com.umeng.analytics:analytics:latest.integration'`

在Manifest.xml中去声明appkey,渠道及权限

 <!--友盟 App Key-->
    <meta-data
        android:name="UMENG_APPKEY"
        android:value="588c5ab6ae1bf87552001aee"/>
    <!--友盟渠道号 通过占位符动态修改渠道号-->
    <meta-data
        android:name="UMENG_CHANNEL"
        android:value="${UMENG_CCHANNEL_VALUE}"/>
 <!--友盟权限-->
    <uses-permission 
        android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission 
        android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission 
        android:name="android.permission.INTERNET"/>
    <uses-permission 
        android:name="android.permission.READ_PHONE_STATE"/>

在模块的build.gradle配置打包信息

在android{….defaultConfig{}}配置文件中添加
multiDexEnabled true//突破应用方法数65535的限制
manifestPlaceholders = [UMENG_CCHANNEL_VALUE :"umeng"] //默认的渠道号
最后是这样的

android {
    compileSdkVersion 25
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "rc.loveq.meizhi"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //突破应用方法数65535的限制
        multiDexEnabled  true
        //默认的渠道号
        manifestPlaceholders = [UMENG_CCHANNEL_VALUE :"umeng"]
    }

添加签名文件配置,签名文件可以在AS中 Build->Generate Signed APK中生成

 //添加签名文件配置,必须写在buildTypes之前
    signingConfigs{
        debug{}
        //为release添加签名文件配置
        release{
            storeFile file("umeng.jks")
            storePassword "umeng123"
            keyAlias "umeng"
            keyPassword "umeng123"
        }
    }

配置buildTypes

 buildTypes {
        release {
            minifyEnabled false //是否启动混淆
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //指定apk使用的签名文件
            signingConfig signingConfigs.release
            //指定release包apk输出名
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        def fileName = "${variant.productFlavors[0].name}"+".apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }

配置多渠道脚本

  productFlavors{
        xiaomi{
            manifestPlaceholders =[UMENG_CCHANNEL_VALUE :"xiaomi"]
        }
        wandoujia{
            manifestPlaceholders =[UMENG_CCHANNEL_VALUE :"wandoujia"]
        }
    }

通过上面几步,现在build.gradle是这样的

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "rc.loveq.meizhi"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //突破应用方法数65535的限制
        multiDexEnabled  true
        //默认的渠道号
        manifestPlaceholders = [UMENG_CCHANNEL_VALUE :"umeng"]
    }
    //添加签名文件配置,必须写在buildTypes之前
    signingConfigs{
        debug{}
        //为release添加签名文件配置
        release{
            storeFile file("umeng.jks")
            storePassword "umeng123"
            keyAlias "umeng"
            keyPassword "umeng123"
        }
    }
     buildTypes {
        release {
            minifyEnabled false //是否启动混淆
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //指定apk使用的签名文件
            signingConfig signingConfigs.release
            //指定release包apk输出名
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        def fileName = "${variant.productFlavors[0].name}"+".apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }

//方法一:
    productFlavors{
        xiaomi{
            manifestPlaceholders =[UMENG_CCHANNEL_VALUE :"xiaomi"]
        }
        wandoujia{
            manifestPlaceholders =[UMENG_CCHANNEL_VALUE :"wandoujia"]
        }
    }

}

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:25.1.0'
    testCompile 'junit:junit:4.12'
    compile 'com.umeng.analytics:analytics:latest.integration'
}

在AS终端输入gradlew assembleRelease(需要配置环境变量)
最后生成的apk

多渠道脚本其他配置方法

      //方法二
    productFlavors{
        xiaomi{}
        wandoujia{}
    }
       productFlavors.all{
        flavor->flavor.manifestPlaceholders=[UMENG_CCHANNEL_VALUE :name]

    }

多渠道打包一些实际应用场景

有些时候,我们要测试不同的的功能,但是又不想卸载重装应用,我们可以这样用:

 productFlavors{
        okhttp{
            //包名为在applicationId拼接applicationIdSuffix
            //但是在实际应用运行中,通过代码获取包名时还是原来的包名,即applicationId
            applicationIdSuffix "okhttp"
            //通过脚本动态修改应用的app_name
            //必须把工程中 <!--<string name="app_name">MeiZhi</string>-->注释掉
            //否则打包不通过
            resValue "string","app_name","okhttp"
        }
        jpush{
            applicationIdSuffix "jpush"
            resValue "string","app_name","jpush"
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值