1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| import org.apache.tools.ant.taskdefs.condition.Os
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)
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 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 }
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() } }
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) } }
|