移动端
VasDolly 多渠道打包工具使用指南
使用场景与常用任务 项目已经通过 维护 Dev、Production 等多环境,只需要在 assemble 任务后追加 即可生成对应渠道包。所有多渠道包默认会落在 。 输出目录为 ,命...
使用场景与常用任务
项目已经通过 productFlavors 维护 Dev、Production 等多环境,只需要在 assemble 任务后追加 渠道名 + BuildType(Release/Debug) 即可生成对应渠道包。所有多渠道包默认会落在 app/build/channel/。
# 项目清理与预构建
./gradlew clean build
# Dev 环境
./gradlew channelDevRelease
./gradlew channelDevDebug
# Production 环境
./gradlew channelProductionRelease
./gradlew channelProductionDebug
输出目录为
app/build/channel/渠道名/…,命名规则可在 VasDolly 的channel配置里自定义。
排坑:必须使用 JDK 11
Android Gradle Plugin 7.x 及以后要求 JDK 11 才能工作。如果本地仍是 JDK 8,会提示:
Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
临时方案:参考《解决Gradlew因开发环境为JDK8提示需要JDK11的问题》为 gradlew 配置 JDK 11 环境变量,或直接升级本地 JDK。
VasDolly 能做什么
VasDolly 是腾讯开源的多渠道打包工具,可自动检测 Apk 的 V1/V2 签名,并以透明的方式追加渠道信息。3.0.4 版本开始兼容 Android Gradle Plugin 4.2+,也支持通过 fastMode、lowMemory 等参数在速度和内存之间取舍。
若需要禁用默认开启的 V2 签名,可在 signingConfigs 中显式设置:
signingConfigs {
release {
v1SigningEnabled true
v2SigningEnabled false
}
debug {
v1SigningEnabled true
v2SigningEnabled false
}
}
接入流程
1. 在根工程注册 Plugin
// root build.gradle
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
classpath 'com.tencent.vasdolly:plugin:3.0.4'
}
}
2. 在 app 模块应用插件
// app/build.gradle
apply plugin: 'com.tencent.vasdolly'
3. 引入 helper 以读取渠道
dependencies {
api 'com.tencent.vasdolly:helper:3.0.4'
}
4. 配置渠道列表
支持两种方式,最终渠道为两者合集:
- 在
gradle.properties中声明渠道文件,文件需位于项目根目录,每行一个渠道:
channel_file=channel.txt
- 在
channel/rebuildChannel配置里使用channelFile指向任意文件:
channel {
channelFile = file("/Users/leon/Downloads/testChannel.txt")
}
rebuildChannel {
channelFile = file("/Users/leon/Downloads/testReChannel.txt")
}
```
## 直接在 Gradle 中生成多渠道包
`channel {}` 块除了渠道文件,还可以自定义输出目录、命名规则和性能参数:
```groovy
channel {
channelFile = file("/Users/leon/Downloads/testChannel.txt")
outputDir = new File(project.buildDir, "channel")
apkNameFormat = '${appName}-${versionName}-${versionCode}-${flavorName}-${buildType}'
fastMode = false
buildTimeDateFormat = 'yyyyMMdd-HH:mm:ss'
lowMemory = false
}
命名模板可使用以下变量:appName、versionName、versionCode、buildType、flavorName、appId、buildTime。
- Debug 包:
./gradlew channelDebug - Release 包:
./gradlew channelRelease - 临时渠道:
./gradlew channelRelease -Pchannels=yingyongbao,gamecenter(命令行参数优先级更高,并与文件方式互斥)
基于已有基线包二次生成
如果已经有一份基础 APK,可通过 rebuildChannel {} 重新写入渠道并输出到指定目录:
rebuildChannel {
channelFile = file("/Users/leon/Downloads/testReChannel.txt")
baseApk = file("/path/to/app_base.apk")
outputDir = new File(project.buildDir, "rebuildChannel")
fastMode = false
lowMemory = false
}
运行 ./gradlew rebuildChannel 即可,同样支持 -Pchannels= 以便临时测试。
命令行工具与渠道读取
-
自
1.0.5起,VasDolly 附带命令行工具,使用方式详见仓库command/README。 -
App 端可通过 helper 库中的
ChannelReaderUtil读取渠道:String channel = ChannelReaderUtil.getChannel(getApplicationContext());若未写入渠道,方法会返回
null,记得兜底。