walle

Introduction: Android Signature V2 Scheme 签名下的新一代渠道包打包神器
More: Author   ReportBugs   
Tags:

Release Version Build Status PRs Welcome License

Walle(瓦力):Android Signature V2 Scheme 签名下的新一代渠道包打包神器

瓦力通过在 Apk 中的APK Signature Block区块添加自定义的渠道信息来生成渠道包,从而提高了渠道包生成效率,可以作为单机工具来使用,也可以部署在 HTTP 服务器上来实时处理渠道包 Apk 的升级网络请求。

Quick Start

为了方便大家的使用,我们提供了 2 种使用方式:

  • Gradle 插件方式,方便快速集成
  • 命令行方式,最大化满足各种自定义需求

Gradle 插件使用方式

配置 build.gradle

在位于项目的根目录 build.gradle 文件中添加 Walle Gradle 插件的依赖, 如下:

buildscript {
    dependencies {
        classpath 'com.meituan.android.walle:plugin:1.1.7'
    }
}

并在当前 App 的 build.gradle 文件中 apply 这个插件,并添加上用于读取渠道号的 AAR

apply plugin: 'walle'

dependencies {
    compile 'com.meituan.android.walle:library:1.1.7'
}

配置插件

walle {
    // 指定渠道包的输出路径
    apkOutputFolder = new File("${project.buildDir}/outputs/channels");
    // 定制渠道包的 APK 的文件名称
    apkFileNameFormat = '${appName}-${packageName}-${channel}-${buildType}-v${versionName}-${versionCode}-${buildTime}.apk';
    // 渠道配置文件
    channelFile = new File("${project.getProjectDir()}/channel")
}

配置项具体解释:

  • apkOutputFolder:指定渠道包的输出路径, 默认值为new File("${project.buildDir}/outputs/apk")
  • apkFileNameFormat:定制渠道包的 APK 的文件名称, 默认值为'${appName}-${buildType}-${channel}.apk'
    可使用以下变量:

          projectName - 项目名字
          appName - App 模块名字
          packageName - applicationId (App 包名 packageName)
          buildType - buildType (release/debug 等)
          channel - channel 名称 (对应渠道打包中的渠道名字)
          versionName - versionName (显示用的版本号)
          versionCode - versionCode (内部版本号)
          buildTime - buildTime (编译构建日期时间)
          fileSHA1 - fileSHA1 (最终 APK 文件的 SHA1 哈希值)
          flavorName - 编译构建 productFlavors 名
    
  • channelFile:包含渠道配置信息的文件路径。 具体内容格式详见:渠道配置文件示例,支持使用#号添加注释。

如何获取渠道信息

在需要渠道等信息时可以通过下面代码进行获取

String channel = WalleChannelReader.getChannel(this.getApplicationContext());

如何生成渠道包

生成渠道包的方式是和assemble${variantName}Channels指令结合,渠道包的生成目录默认存放在 build/outputs/apk/,也可以通过walle闭包中的apkOutputFolder参数来指定输出目录

用法示例:

  • 生成渠道包 ./gradlew clean assembleReleaseChannels
  • 支持 productFlavors ./gradlew clean assembleMeituanReleaseChannels

更多用法

插入额外信息

channelFile只支持渠道写入,如果想插入除渠道以外的其他信息,请在 walle 配置中使用configFile

walle {
    // 渠道&额外信息配置文件,与 channelFile 互斥
    configFile = new File("${project.getProjectDir()}/config.json")
}

configFile是包含渠道信息和额外信息的配置文件路径。
配置文件采用 json 格式,支持为每个 channel 单独配置额外的写入信息。具体内容格式详见:渠道&额外信息配置文件示例

注意:

  • 此配置项与channelFile功能互斥,开发者在使用时选择其一即可,两者都存在时configFile优先执行。
  • extraInfo 不要出现以channel为 key 的情况

而对应的渠道信息获取方式如下:

ChannelInfo channelInfo= WalleChannelReader.getChannelInfo(this.getApplicationContext());
if (channelInfo != null) {
   String channel = channelInfo.getChannel();
   Map<String, String> extraInfo = channelInfo.getExtraInfo();
}
// 或者也可以直接根据 key 获取
String value = WalleChannelReader.get(context, "buildtime");
临时生成某渠道包

我们推荐使用 channelFile/configFile 配置来生成渠道包,但有时也可能有临时生成渠道包需求,这时可以使用:

  • 生成单个渠道包: ./gradlew clean assembleReleaseChannels -PchannelList=meituan
  • 生成多个渠道包: ./gradlew clean assembleReleaseChannels -PchannelList=meituan,dianping
  • 生成渠道包&写入额外信息:

    ./gradlew clean assembleReleaseChannels -PchannelList=meituan -PextraInfo=buildtime:20161212,hash:xxxxxxx

    注意: 这里的 extraInfo 以key:value形式提供,多个以,分隔。

  • 使用临时 channelFile 生成渠道包: ./gradlew clean assembleReleaseChannels -PchannelFile=/Users/xx/Documents/channel
  • 使用临时 configFile 生成渠道包: ./gradlew clean assembleReleaseChannels -PconfigFile=/Users/xx/Documents/config.json

使用上述-P 参数后,本次打包 channelFile/configFile 配置将会失效,其他配置仍然有效。 -PchannelList,-PchannelFile, -PconfigFile三者不可同时使用。

命令行工具使用方式

可以使用命令行工具来支持各类自定义的需求,具体使用方式详见:Walle CLI 使用说明

其他使用方式

为了更好的满足大家的各类自定义需求,我们把对APK Signing Block区块进行读写操作的模块进行了封装。

读写模块的使用说明详见:

Q&A

原理介绍

对该工具的原理感兴趣的同学,可以移步美团 Android 新一代渠道包生成工具进行了解。

注意事项

  • 使用 apksigner 重新对 Apk 签名会导致渠道信息丢失,需要再次写入渠道信息
  • 1.1.3 版本起,walle 支持对含有 comment 的 apk 进行渠道写入, 详见issue 52

技术支持

  • Read The Fucking Source Code
  • 通过提交 issue 来寻求帮助
  • 联系我们寻求帮助

贡献代码

  • 欢迎提交 issue
  • 欢迎提交 PR

参考

License

Copyright 2017 Meituan-Dianping

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools