支持360自动加固task
2019-02-25 本文已影响167人
陈桐Caliburn
支持360自动加固task
执行命令
执行命令 gradle jiaguDebug
引用
apply from: "jiagu.gradle"
配置
def executeCmd(cmd){
def jiaGuPluginPath = getJiaGuProperty()
println "执行命令行:" + cmd
println "jiagu.dir=" + getJiaGuProperty()
def p = cmd.execute(null, new File(jiaGuPluginPath))
println p.text
p.waitFor() // 用以等待外部进程调用结束
println p.exitValue()
}
def getJiaGuProperty(){
File file = rootProject.file('local.properties')
if(file.exists()){
//加载资源
InputStream inputStream = rootProject.file('local.properties').newDataInputStream()
Properties properties = new Properties()
properties.load(inputStream)
if (properties.containsKey("jiagu.dir")){
return properties.getProperty("jiagu.dir")
}else {
println "请在local.properties 添加jiagu.dir 例子如下"
println "jiagu.dir=/Users/chentong/Android/360jiagubao_mac/jiagu"
return ""
}
}
}
task jiaguDebug(dependsOn: 'assembleDebug') {
group "jiaguapk"
doLast {
jiagu("debug")
}
}
task jiaguRelease(dependsOn: 'assembleRelease') {
group "jiaguapk"
doLast {
jiagu("release")
}
}
def jiagu(buildType){
println "360加固--------begin---------"
//获得apk路径
def appFilePath = getAppFilePath(buildType)
if (!new File(appFilePath).exists()) {
println "apk not exist"
return
}
def cmdBase = 'java -jar jiagu.jar'
//获得加固宝输出路径
def outPath = getJiaGuPath()
File outFile = new File(outPath)
if (!outFile.exists()){
outFile.mkdirs()
}
def cmdJiaGu = cmdBase + ' -jiagu ' + appFilePath + ' ' + outPath + ' -autosign' + ' -automulpkg'
executeCmd(cmdJiaGu)
println "360加固--------end---------"
}
def getAppFilePath(buildType){
// 生成的apk的路径
def appFilePath = getRootDir().getAbsolutePath() + File.separator + "app" +
File.separator + "build" + File.separator + "outputs" + File.separator + "apk" +File.separator + buildType + File.separator +
"app-" + buildType + ".apk"
println "appFilePath=" + appFilePath
return appFilePath
}
def getJiaGuPath(){
def outPath = getRootDir().getAbsolutePath() + File.separator + "app" + File.separator + "360jiagu"
println "jiaguPath=" + outPath
return outPath
}
//清理加固文件
tasks.whenTaskAdded { theTask ->
if (theTask.name == "assembleRelease" | theTask.name == "assembleDebug") {
theTask.dependsOn "cleanJiaguDir"
}
}
task cleanJiaguDir {
def jiaguPath = getJiaGuPath()
new File(jiaguPath).deleteDir()
}
感谢月亮的后羿提出的问题
我的task命令的打印日志:可以看一下
https://www.jianshu.com/p/fb538da65d28