Android Studio 简单配置多渠道包案例

前言

该配置解决了多渠道包和多渠道号的打包问题,并且使用简单的配置文件

过程

新建一个AS工程,工程截图如下


android1
android1

我们新建一个channel.txt,并且增加渠道名字和渠道号

baidu:1111
360:2222
91:3333
:4444
:5555
wandoujia:6666

为了使得我们的配置能够生效,需要修改manifest.xml,增加如下两行:

       <!-- app pid -->
        <meta-data android:name="CHANNEL_ID" android:value="${APP_PID_PLACEHOLDER}" />
        <!-- app key -->
        <meta-data android:name="CHANNEL_NAME" android:value="${APP_KEY_PLACEHOLDER}" />

同样的build.gradle也需要进行相应的修改:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}


apply plugin: 'com.android.application'

tasks.withType(JavaCompile) { options.encoding = "UTF-8" }


android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.monkey.multichannelapk"
        minSdkVersion 10
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    sourceSets {


        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
    productFlavors {
        def path="./channel.txt"
        file(path).eachLine { line->
            def words = line.split(':')
            def key = words[0]
            def channel = words[1]
            if (key == '') {
                key = channel
            }
            def name = 's'+channel
            "$name" {
                manifestPlaceholders=[APP_KEY_PLACEHOLDER:key, APP_PID_PLACEHOLDER:name]
            }
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
}

打出的包可以在如下一次性找到:

multi
multi

最后附上打包gif

gif
gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,807评论 25 709
  • 这一章主要针对项目中可以用到的一些实用功能来介绍Android Gradle,比如如何隐藏我们的证书文件,降低风险...
    acc8226阅读 12,299评论 3 25
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,761评论 19 139
  • Android多渠道打包 概述 每当发新版本时,Android客户端会被分发到各个应用市场,比如豌豆荚,360手机...
    砺雪凝霜阅读 6,452评论 2 11
  • 今天上午有朋友说你最感谢的人是谁呢?试着给ta写封感谢的信吧。 应该感激的人很多,但是其中最感激的当然是自己的父母...
    握瑜阅读 2,890评论 3 4