前兩篇文章將咱們的打包加固和上傳內測平臺進行了自動化,節省了開發人員的時間。html
可是還有一些問題並無解決,好比咱們正在吭哧吭哧的寫代碼,測試忽然來讓你給他打個包,必需要暫停當前的工做,去對應分支執行打包命令,打包期間也只能等着,不能繼續寫代碼。java
這時,Jenkins 服務器就派上用場了,咱們能夠將打包命令在 Jenkins 服務器進行配置,讓測試人員本身去選擇想要的分支版本進行打包,同時提高雙方的效率。android
Jenkins 相關的知識網上有不少文章,請你們自行搜索瞭解,本文只講解在 Jenkins 上配置如下幾點內容:git
因爲將 360 加固替換爲梆梆加固,而梆梆加固的命令行工具不支持自動簽名和本地多渠道包生成,因此增長了從新簽名和 VasDolly 多渠道打包的命令。github
完整的 config.gradle
配置:apache
ext { curGitCommit = rootProject.properties['GIT_COMMIT'] == null ? getGitCommit() : rootProject.properties['GIT_COMMIT'] curTime = rootProject.properties["BUILD_TIMESTAMP"] == null ? getCurTime() : rootProject.properties["BUILD_TIMESTAMP"] backupPath = "../buildBackup/" if (rootProject.properties["isJenkins"].toBoolean()) { androidHome = rootProject.properties['ANDROID_HOME'] } else { Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) androidHome = properties.getProperty('sdk.dir') } //簽名文件配置 signing = [keyAlias : 'xxxxx', keyPassword : 'xxxxx', storeFile : '../sign.keystore', storePassword: 'xxxxxx'] //蒲公英配置 pgy = [apiKey : "xxxx", uploadUrl: "https://www.pgyer.com/apiv2/app/upload"] //加固配置 jiagu = [ app1Online : "${backupPath}${curGitCommit}/${curTime}/app1Online_jiagu/", app1Admin : "${backupPath}${curGitCommit}/${curTime}/app1Admin_jiagu/", app2Online : "${backupPath}${curGitCommit}/${curTime}/app2Online_jiagu/", app2Admin : "${backupPath}${curGitCommit}/${curTime}/app2Admin_jiagu/", channelConfigPath: '../jiagu/Channel.txt', VasDollyJar : "../jiagu/VasDolly.jar", banbanJar : "../jiagu/secapi.jar", banbanName : rootProject.properties['banbanName'] == null ? "banbanName" : rootProject.properties['banbanName'], banbanApiKey : rootProject.properties['banbanApiKey'] == null ? "banbanApiKey" : rootProject.properties['banbanApiKey'], banbanSecretKey : rootProject.properties['banbanSecretKey'] == null ? "banbanSecretKey" : rootProject.properties['banbanSecretKey'], banbanIp : rootProject.properties['banbanIp'] == null ? "banbanIp" : rootProject.properties['banbanIp'], ] android = [compileSdkVersion: 28, buildToolsVersion: "29.0.3", minSdkVersion : 19, targetSdkVersion : 28] apksignerDir = "${androidHome}/build-tools/${android['buildToolsVersion']}/" //版本號管理 APP1_VERSION_NAME = "2.0.2" APP1_TEST_NUM = "0001" APP2_VERSION_NAME = "1.0.5" APP2_TEST_NUM = "0005" dependencies = [ "androidx-appcompat" : "androidx.appcompat:appcompat:1.1.0", "androidx-constraintlayout": "androidx.constraintlayout:constraintlayout:1.1.3", "VasDolly-helper" : "com.leon.channel:helper:2.0.3", ] } static def getCurTime() { return new Date().format("yyyy-MM-dd_HH-mm-ss") } static def getGitCommit() { def exceptionStr = "git rev-parse HEAD 執行失敗" try { def cmd = 'git rev-parse HEAD' def gitCommit = cmd.execute().text.trim() if (gitCommit.isEmpty()) { throw new GradleException(exceptionStr) } return gitCommit } catch (Exception e) { throw new GradleException(exceptionStr, e) } }
在jiagu.gradle
中實現加固簽名和多渠道包邏輯:api
import org.apache.tools.ant.taskdefs.condition.Os /** * 加固 * @param config 配置加固策略 * @param apkPath 要加固的文件路徑 * @param outputPath 輸出路徑 * @param channelName 渠道名,爲 Null 時讀取 rootProject.ext.jiagu["channelConfigPath"] 多渠道配置文件 */ def jiaGu(String config, String apkPath, String outputPath, String channelName) { println("開始加固 \nconfig:$config \napkPath:$apkPath \noutputPath:$outputPath \nchannelName:$channelName") exec { executable = 'java' args = ['-jar', rootProject.ext.jiagu["banbanJar"], '-i', rootProject.ext.jiagu["banbanIp"], '-u', rootProject.ext.jiagu["banbanName"], '-a', rootProject.ext.jiagu["banbanApiKey"], '-c', rootProject.ext.jiagu["banbanSecretKey"], '-f', '0', '-t', config, '-p', apkPath, '-d', outputPath, ] } println "加固的文件路徑:${apkPath}" println "加固後的文件路徑:${outputPath}" def signApkPath = apkSigner(outputPath) generatingMultipleChannels(signApkPath, outputPath, channelName) //在 Jenkins 上運行時,將構建產物備份到硬盤根目錄, if (isJenkins.toBoolean()) { String sourceDir = file(outputPath).parentFile.absolutePath String destinationDir = "/Users/buildBackup/${rootProject.properties["JOB_NAME"]}/${rootProject.ext.curGitCommit}/${rootProject.ext.curTime}/" new groovy.util.AntBuilder().copy(todir: destinationDir) { fileset(dir: sourceDir) } println "將構建產物備份到:${destinationDir}" } } def apkSigner(String apkDir) { def apkFile = getApkFile(apkDir) if (apkFile == null || !apkFile.exists()) { throw GradleException("加固後的 apk 文件不存在") } def signDir = new File(apkDir, 'sign') if (!signDir.exists()) { signDir.mkdirs() } def outputApkPath = new File(signDir, apkFile.name) def storeFile = rootProject.ext.signing["storeFile"] def storeFilePath = file(storeFile).absolutePath //兼容 Windows、Mac、Linux 系統 def execuTable = Os.isFamily(Os.FAMILY_WINDOWS) ? 'cmd' : 'sh' def apksigner = Os.isFamily(Os.FAMILY_WINDOWS) ? 'apksigner.bat' : './apksigner' println("對加固包進行簽名") def parameter = [apksigner, 'sign', '--ks', storeFilePath, '--ks-key-alias', rootProject.ext.signing["keyAlias"], '--ks-pass', "pass:${rootProject.ext.signing["storePassword"]}", '--key-pass', "pass:${rootProject.ext.signing["keyPassword"]}", '--out', outputApkPath, apkFile.absolutePath] if (Os.isFamily(Os.FAMILY_WINDOWS)) { parameter.add(0, '/c') } exec { workingDir = rootProject.ext.apksignerDir executable = execuTable args = parameter } println "apk 簽名成功:" + outputApkPath return outputApkPath } /** * 生成多渠道包 * @param signApkPath 基準包 * @param outputDir 輸出目錄 * @param channelName 渠道名,爲 Null 時讀取 rootProject.ext.jiagu["channelConfigPath"] 多渠道配置文件 */ private void generatingMultipleChannels(signApkPath, outputDir, channelName) { def channelDir = new File(outputDir, 'channels') if (!channelDir.exists()) { channelDir.mkdirs() } println("生成多渠道包") def channel = channelName == null ? rootProject.ext.jiagu["channelConfigPath"] : channelName exec { executable = 'java' args = ['-jar', rootProject.ext.jiagu["VasDollyJar"], 'put', '-c', channel, signApkPath, channelDir.absolutePath ] } println("生成多渠道包完畢") } def getApkPath(String flavor) { if ("app1Online" == flavor) { return "${projectDir.absolutePath}/build/outputs/apk/production/release/${getApkName(rootProject.ext.android["versionName"])}" } else { return "${projectDir.absolutePath}/build/outputs/apk/${flavor}/release/${getApkName(getTestVersionName(flavor))}" } } private def getApkFile(String fileDir) { def dir = file(fileDir) if (!dir.exists()) { println "dir not exists:" + dir.path return null } 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 null } return files[0] } 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() } } /** * App1 * 根據多渠道文件進行加固 * 執行命令:./gradlew releaseApp1 */ task releaseApp1(dependsOn: ['assembleApp1OnlineRelease']) { group = "publish" doLast { def apkOutputFile = file(rootProject.ext.jiagu["app1OnlineOutputPath"]) checkOutPutDir(apkOutputFile) def apkFile = file(getApkPath("App1Online")) if (!apkFile.exists()) { println("apk file is not exists:" + apkFile.absolutePath) return } jiaGu("1", apkFile.absolutePath, apkOutputFile.absolutePath, null) } }
咱們只須要在命令行執行 ./gradlew releaseApp1
就會執行編譯—>加固—>簽名—>生成多渠道包服務器
上傳蒲公英的代碼和前文同樣,只須要改動一下 apk 路徑,取生成的渠道包進行上傳。微信
首先咱們建立好任務,配置 task 命令參數:app
再配置 Gradle 插件執行上面選擇的命令,並配置 Project properties:
若是執行上傳蒲公英的任務,咱們但願直接顯示蒲公英的二維碼,增長以下Set build description
配置:
咱們保存配置,執行任務:
執行成功後的效果:
全文沒有作太多講解,直接上代碼上配置圖片,簡單粗暴,若是 Gradle 部分有疑惑的建議看看前面幾篇文章。
對於 Jenkins 的配置本文只是一筆帶過,相信你們簡單瞭解一下相關的概念和原理就能快速上手,基本流程跑通之後能夠觸類旁通實現更多功能。
最後上 demo 地址: https://github.com/imliujun/G...
歡迎關注微信公衆號:大腦好餓,更多幹貨等你來嘗