Android多渠道打包(友盟)

1.在Manifest.xml文件中添加权限和AppKey

权限





AppKey和渠道变量:


//设置动态渠道变量

2.在build.gradle中设置productFlavors

为不同的渠道市场进行设置:

android {  
    productFlavors {
        xiaomi {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
        }
        baidu {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
        }
        wandoujia {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
        }
    }  
}

 

//通过gradle脚本语法统一设置不同的渠道
android {  
    productFlavors {
        xiaomi {}
        baidu {}
        wandoujia {}
    }  
    productFlavors.all{
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
}

3.对应用进行打包操作

在AndroidStudio菜单栏点击Build菜单–>Generate signed APK

Android多渠道打包(友盟)_第1张图片

Android多渠道打包(友盟)_第2张图片

4.通过studio命令行工具,进行打包

命令:gradlew assembleRelease 开始打包

Android多渠道打包(友盟)_第3张图片

打包成功

Android多渠道打包(友盟)_第4张图片

查看apk

Android多渠道打包(友盟)_第5张图片

至此,多渠道打包就已经完成!!

4.签名

//添加签名文件配置
signingConfigs{
    debug{}
    //为release包添加签名文件配置
    release{
        storeFile file("Multi.jks")
        storePassword "5258168699"
        keyAlias "Multi"
        keyPassword "5258168699"
    }
}

注意:signingConfigs代码块一定要写在buildTypes前面,否则会报错:
Could not find property 'debugConfig' on SigningConfig container.

5.自己的多渠道打包

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "xiaoming.com.multi"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        //默认设置友盟
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
    }
    //添加签名文件配置
    signingConfigs{
        debug{}
        //为release包添加签名文件配置
        release{
            storeFile file("Multi.jks")
            storePassword "5258168699"
            keyAlias "Multi"
            keyPassword "5258168699"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            // 自定义输出不同的渠道配置
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 输出apk名称为wooyun_v1.0_wandoujia.apk
                        def fileName = "${variant.productFlavors[0].name}_app_v${defaultConfig.versionName}.apk"
                        output.outputFile = new File(outputFile.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")
//        }
//        baidu{
////            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
//            resValue("string","app_name","baidu")
//        }

        //为不同功能的apk进行打包测试,可以在同一手机上安装同一应用进行不同功能的测试
        okhttp{
            applicationIdSuffix "okhttp"
            resValue "string","app_name","okHttp"
        }
        jpush{
            applicationIdSuffix "jpush"
            resValue "string","app_name","jpush"
        }
    }
    //通过gradle脚本语法统一设置不同的渠道
    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:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    //添加友盟应用统计支持
    compile 'com.umeng.analytics:analytics:latest.integration'
}

你可能感兴趣的:(Android多渠道打包(友盟))