多渠道打包

1.清单文件中的配置

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

    <!--权限-->
    <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"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:name=".App"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!--AndroidManifest清单配置文件方式初始化AppKey和channel-->
        <meta-data android:value="5af83658b27b0a6a9600039f" android:name="UMENG_APPKEY"/>
        <meta-data android:value="${UMENG_CHANNEL_VALUE}" android:name="UMENG_CHANNEL"/>

    </application>

</manifest>

2.app.build.grable中配置

apply plugin: 'com.android.application'

android {
    signingConfigs {
        releace {
            keyAlias 'key0'
            keyPassword '123456'
            storeFile file('F:/MultiChannelPackageDemo05/app/build/multi.jks')
            storePassword '123456'
        }
    }
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.dell.multichannelpackagedemo05"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        //app build.gradle动态配置Manifest的占位符和默认值
        manifestPlaceholders = [UMENG_CHANNEL_VALUE:"umeng"]
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          //配置签名文件信息,注意:signingConfigs要在buildTypes之前
           signingConfig signingConfigs.releace

            //指定我们release包输出文件名就是我们渠道的名字
            //android studio3.0以后的写法
            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "${variant.productFlavors[0].name}.apk"
                }
            }
        }
    }

    //多渠道重点东西
    flavorDimensions "color"
    //添加渠道信息
    productFlavors {
        xiaomi{
//            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
//            dimension "color"
            //可以给不同渠道的apk赋不同的应用名
            applicationIdSuffix "xiaomi"//给测试打不同功能点可以安装在同一个手机上的测试包
            resValue "string", "app_name", "xiaomi"
        }

        wandoujia{
//            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
//            dimension "color"
            //可以给不同渠道的apk赋不同的应用名
            applicationIdSuffix "wandoujia"//给测试打不同功能点可以安装在同一个手机上的测试包
            resValue "string", "app_name", "wandoujia"
        }
        yingyongbao{
//            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "yingyongbao"]
//            dimension "color"
            //可以给不同渠道的apk赋不同的应用名
            applicationIdSuffix "yingyongbao"//给测试打不同功能点可以安装在同一个手机上的测试包
            resValue "string", "app_name", "yingyongbao"
        }
    }

    //为了简单可以用脚本去替换重复代码
    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //友盟统计自动继承依赖
    implementation 'com.umeng.sdk:common:1.5.0'
    implementation 'com.umeng.sdk:analytics:7.5.0'
}




3.value-styles中配置:

<resources>
    <!--注释掉此段代码:因每个应用各有自己的名字-->
    <!--<string name="app_name">MultiChannelPackageDemo05</string>-->
</resources>

4.App中配置:

package com.example.dell.multichannelpackagedemo05;

import android.app.Application;

import com.umeng.commonsdk.UMConfigure;

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        /*
        注意:如果您已经在AndroidManifest.xml中配置过appkey和channel值,可以调用此版本初始化函数。
        */

        /**
         * 初始化common库
         * 参数1:上下文,不能为空
         * 参数2:设备类型,UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子,默认为手机
         * 参数3:Push推送业务的secret(秘钥)
         */
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值