Android Studio Gradle 多渠道打包


之前集成UMeng提供的一些服务时接触过多渠道打包,UMeng要根据各应用市场渠道号来进行一些统计和分析,比如各渠道的下载数、活跃度、自动更新等等。UMeng提供了一个打包工具(这里),但官方很久没有更新了,自己也切换到Android Studio IDE上,Gradle插件可以很好的实现多渠道打包的需求。

多渠道打包的大概思想是(针对UMeng): 动态的更改AndroidManifest.xml文件中的一个属性值(渠道号),从而来标识不同的渠道。

Gradle的Product flavors特性(这里)可以很方便的实现功能,简单的说下实现方式:不同的flavor指定不同渠道的AndroidManifest.xml文件。这种方式有很大的弊端,重复性工作太多了,只为了更改其中的一个属性而要维护太多的AndroidManifest.xml文件。可喜的是Android Studio 1.0 提供了更强大的Manifest Merger(这里)功能,其中的Placehodler Support(这里)特性可以大大的简化上面的做法,不需要再维护这么多的AndroidManifest.xml文件了!但把所有的渠道都配置在build.gradle文件中毕竟不好维护(应用市场多达几十个),下面主要介绍通过程序的方式来读取配置文件,生成不同的渠道包。

通过程序的方式也要借助于 Placehodler Support特性,所以Android Studio请升级到1.0以上。

具体的思路是通过读取配置文件中的渠道号来动态的生成build type。具体的代码如下:

// 多渠道打包
def channels() {
    if (project.hasProperty("channel")) {
        // 渠道号配置文件路径
        def path = "./build-types/channels.txt"
        file(path).eachLine { line ->
            if(!line.startsWith("//")){ //剔除注释行
                android.buildTypes.create(line, {
                    manifestPlaceholders = [ channel:line ]
                })
            }
        }
    }
}

AndroidManifest.xml文件更改如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tubb.cityindex" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.tubb.cityindex.CitySelectorActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="UMENG_CHANNEL" android:value="${channel}"/>
    </application>

</manifest>

最后只需要在工程app目录下新建build-types文件夹添加channels配置文件

// 百度应用市场
baidu
// 360应用市场
m360

加了一个编译参数channel来作为多渠道打包的判断,我是在Windows环境下,命令变为:gradlew build -Pchannel

测试代码托管在(这里),欢迎大伙来讨论~~

-------------------------------------------------------------end----------------------------------------------------------

有些朋友问我怎么添加签名和混淆支持,其实跟我们平常的用法是一样的,下面是完整的build文件(为了方便说明问题,签名文件的信息直接明文给出大笑
apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.tubb.cityindex"
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    // 签名
    signingConfigs {
        myConfig {
            storeFile file("tubb.jks")
            storePassword "123456"
            keyAlias "tubb"
            keyPassword "123456"
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.myConfig
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
        channel {
            manifestPlaceholders = [ channel:"placeholder" ]
        }
    }
    channels()
}

// 多渠道打包
def channels() {
    if (project.hasProperty("channel")) {
        // 渠道号配置文件路径
        def path = "./build-types/channels.txt"
        file(path).eachLine { line ->
            if(!line.startsWith("//")){ //剔除注释行
                android.buildTypes.create(line, {
                    manifestPlaceholders = [ channel:line ]
                    signingConfig android.buildTypes.release.signingConfig
                    minifyEnabled android.buildTypes.release.minifyEnabled
                    proguardFiles android.buildTypes.release.proguardFiles
                })
            }
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}










  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值