咱們的超管包是須要發給運營人員去使用的,防止泄露致使的安全風險,咱們但願對超管包先進行加當然後再上傳到蒲公英。html
咱們的應用在發佈的時候通常都須要進行加固和生成多渠道包,你們一般的作法應該是下載加固客戶端,或者將 apk 文件上傳到加固服務的管理後臺進行加固,而後等着加固完成,再下載安裝包文件。java
再次引用個人名言:android
時間是最寶貴的財富,咱們的時間得用在刀刃上。git
本文基於 使用 Gradle 實現一套代碼開發多個應用 中的 Gradle 配置進行迭代開發,帶領你們實現 360加固
的自動化 Gradle 腳本。github
咱們的目標是全自動化,而且在每一個團隊成員的電腦上都可以實現一行命令執行,不須要作額外的配置。apache
完整的 config.gradle
配置:windows
ext {
//簽名文件配置
signing = [keyAlias : 'xxxxx',
keyPassword : 'xxxxx',
storeFile : '../sign.keystore',
storePassword: 'xxxxxx']
//蒲公英配置
pgy = [apiKey : "xxxx",
uploadUrl: "https://www.pgyer.com/apiv2/app/upload"]
//360加固配置
jiagu = [name : 'xxxxx',
password : 'xxxxx',
zipPath : "../jiagu/360jiagu.zip",
unzipPath : "../jiagu/360jiagubao/",
jarPath : '../jiagu/360jiagubao/jiagu/jiagu.jar',
channelConfigPath: '../jiagu/Channel.txt',
jiagubao_mac : "http://down.360safe.com/360Jiagu/360jiagubao_mac.zip",
jiagubao_windows : "http://down.360safe.com/360Jiagu/360jiagubao_windows_64.zip",
]
android = [compileSdkVersion: 28,
minSdkVersion : 19,
targetSdkVersion : 28]
//版本號管理
APP1_VERSION_NAME = "2.0.2"
APP1_TEST_NUM = "0001"
APP2_VERSION_NAME = "1.0.5"
APP2_TEST_NUM = "0005"
}
複製代碼
新建一個 jiagu.gradle
文件:api
import org.apache.tools.ant.taskdefs.condition.Os
def downloadUrl = Os.isFamily(Os.FAMILY_WINDOWS) ? rootProject.ext.jiagu["jiagubao_windows"] : rootProject.ext.jiagu["jiagubao_mac"]
def zipPath = rootProject.ext.jiagu["zipPath"]
def unzipPath = rootProject.ext.jiagu["unzipPath"]
task download360jiagu() {
doFirst {
//若是 Zip 文件不存在就進行下載
File zipFile = file(zipPath)
if (!zipFile.exists()) {
if (!zipFile.parentFile.exists()) {
zipFile.parentFile.mkdirs()
}
exec {
executable = 'curl'
args = ['-o', zipPath, downloadUrl]
}
}
}
doLast {
//解壓 Zip 文件
ant.unzip(src: zipPath, dest: unzipPath, encoding: "GBK")
//將解壓後的文件開啓讀寫權限,防止執行 Jar 文件沒有權限執行
exec {
executable = 'chmod'
args = ['-R', '777', unzipPath]
}
}
}
複製代碼
執行 download360jiagu
就能夠自動下載並解壓 360 的加固程序啦。安全
import org.apache.tools.ant.taskdefs.condition.Os
def downloadUrl = Os.isFamily(Os.FAMILY_WINDOWS) ? rootProject.ext.jiagu["jiagubao_windows"] : rootProject.ext.jiagu["jiagubao_mac"]
def zipPath = rootProject.ext.jiagu["zipPath"]
def unzipPath = rootProject.ext.jiagu["unzipPath"]
//加固後全部apk的保存路徑
def APP1_OUTPUT_PATH = "jiagu/apk/app1/"
def APP1_APK_PATH = "${projectDir.absolutePath}/build/outputs/apk/app1Online/release/${getApkName(rootProject.ext.APP1_VERSION_NAME)}"
/** * 加固 * @param config 配置加固可選項 * @param apkPath 要加固的文件路徑 * @param outputPath 輸出路徑 * @param automulpkg 是否自動生成多渠道包 */
def jiaGu(String config, String apkPath, String outputPath, boolean automulpkg) {
//首次使用必須先登陸
exec {
executable = 'java'
args = ['-jar', rootProject.ext.jiagu["jarPath"], '-login', rootProject.ext.jiagu["name"], rootProject.ext.jiagu["password"]]
}
//升級到最新版本
exec {
executable = 'java'
args = ['-jar', rootProject.ext.jiagu["jarPath"], '-update']
}
//顯示當前版本號
exec {
executable = 'java'
args = ['-jar', rootProject.ext.jiagu["jarPath"], '-version']
}
//導入簽名信息
exec {
executable = 'java'
args = ['-jar', rootProject.ext.jiagu["jarPath"], '-importsign',
rootProject.ext.signing["storeFile"],
rootProject.ext.signing["storePassword"],
rootProject.ext.signing["keyAlias"],
rootProject.ext.signing["keyPassword"]]
}
//配置加固可選項
exec {
executable = 'java'
args = ['-jar', rootProject.ext.jiagu["jarPath"], '-config', config]
}
//加固命令
def jiaGuArgs
if (automulpkg) {
jiaGuArgs = ['-jar', rootProject.ext.jiagu["jarPath"], '-jiagu',
apkPath,
outputPath,
'-autosign',
'-automulpkg',
'-pkgparam',
rootProject.ext.jiagu["channelConfigPath"]
]
} else {
jiaGuArgs = ['-jar', rootProject.ext.jiagu["jarPath"], '-jiagu',
apkPath,
outputPath,
'-autosign'
]
}
exec {
executable = 'java'
args = jiaGuArgs
}
println "加固的文件路徑:${apkPath}"
println "加固後的文件路徑:${outputPath}"
}
/** * App1 * 根據多渠道文件進行加固 * 執行命令:./gradlew releaseApp1 */
task releaseApp1(dependsOn: 'assembleApp1OnlineRelease') {
doFirst {
//判斷加固程序是否存在,不存在則進行下載
File jarFile = file(rootProject.ext.jiagu["jarPath"])
if (!jarFile.exists()) {
download360jiagu.execute()
}
}
group = "publish"
doLast {
File apkOutputFile = new File(APP1_OUTPUT_PATH, getCurTime())
checkOutputDir(apkOutputFile)
File apkFile = file(APP1_APK_PATH)
if (!apkFile.exists()) {
println("apk file is not exists:" + apkFile.absolutePath)
return
}
jiaGu("-", apkFile.absolutePath, apkOutputFile.absolutePath, true)
}
}
private static void checkOutputDir(File apkOutputFile) {
if (apkOutputFile.exists()) {
File[] files = apkOutputFile.listFiles()
if (files != null) {
for (File file : files) {
file.delete()
}
}
} else {
apkOutputFile.mkdirs()
}
}
static def getCurTime() {
return new Date().format("yyyy-MM-dd HH:mm:ss")
}
複製代碼
如今咱們只須要在命令行執行 ./gradlew releaseApp1
就能夠靜待輸出了。微信
在根目錄的 jiagu
文件夾中建立Channel.txt
文件,在其中能夠配置你須要的多渠道信息。
若是須要配置更多的加固選項,能夠在 jiagu/360jiagubao/jiagu/help.txt
中查看全部的加固命令。
咱們的超管包不須要上傳應用商店,直接加固上傳到蒲公英,而後發送二維碼給管理員下載安裝。咱們把自動加固和自動上傳蒲公英整合到一塊兒。
在jiagu.gradle
中添加單獨加固超管包的方法:
def APP1_ADMIN_OUTPUT_PATH = "jiagu/apk/app1Admin/"
def APP1_ADMIN_APK_PATH = "${projectDir.absolutePath}/build/outputs/apk/app1Admin/release/${getApkName(getTestVersionName("管理員"))}"
/** * 加固超管服包 * 執行命令:./gradlew jiaGuApp1Admin */
task jiaGuApp1Admin(dependsOn: 'assembleApp1AdminRelease') {
doFirst {
File jarFile = file(rootProject.ext.jiagu["jarPath"])
if (!jarFile.exists()) {
download360jiagu.execute()
}
}
group = "publish"
doLast {
File apkOutputFile = new File(APP1_ADMIN_OUTPUT_PATH)
checkOutputDir(apkOutputFile)
File apkFile = file(APP1_ADMIN_APK_PATH)
if (!apkFile.exists()) {
println("apk file is not exists:" + apkFile.absolutePath)
return
}
jiaGu("-", apkFile.absolutePath, apkOutputFile.absolutePath, false)
}
}
複製代碼
修改蒲公英上傳方法:
def app1AdminFileDir = "${projectDir.parent}/jiagu/apk/app2Admin/"
/** * 執行 「uploadApp1Admin」 命令自動打超管服包,並上傳到蒲公英 */
task uploadApp1Admin(dependsOn: 'jiaGuApp1Admin') {
group = "publish"
doLast {
File dir = new File(app1AdminFileDir)
if (!dir.exists()) {
println "Alpha dir not exists:" + dir.path
return
}
File[] files = dir.listFiles(new FileFilter() {
@Override
boolean accept(File file) {
return file.isFile() && file.name.endsWith(".apk")
}
})
if (files == null || files.size() == 0) {
println "files == null || files.size() == 0"
return
}
File apkFile = files[0]
uploadPGY(apkFile.path)
}
}
複製代碼
在命令行執行 ./gradlew uploadApp1Admin
就能夠靜待二維碼地址輸出。
若是你不喜歡執行命令行,咱們只點一下鼠標也能夠執行自動化命令:
demo地址:github.com/imliujun/Gr…