AndroidStudio配置一键360加固gradle脚本

0、前提

1、下载并安装 360加固助手
2、注册好 360的账号信息

一键打包、加固原理:
1、自定义task:“assembleRelease360”,依赖于 “assembleRelease” (即:先打包、后加固)
2、在assembleRelease360任务中,指向 360加固助手的安装目录,执行相关的cmd命令

  • 登录360加固助手
  • 导入签名信息
  • 执行加固命令

1、新建脚本:plug_360_jiagu.gradle

def userName360 = ''
def password360 = ''
def softPath360 = ''

def keystoreFilepath = ''
def keystorePSW = ''
def keystoreAlias = ''
def keystoreAliasPSW = ''
def keyfile = file('temp')

//签名相关key
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
keystoreFilepath = properties.getProperty("keystorePath")
if (keystoreFilepath) {
    keystorePSW = properties.getProperty("keystorePassword")
    keystoreAlias = properties.getProperty("keystoreAlias")
    keystoreAliasPSW = properties.getProperty("keystoreAliasPassword")
    keyfile = file(keystoreFilepath)
}

//360加固相关key
userName360 = properties.getProperty("360.userName")
password360 = properties.getProperty("360.password")
softPath360 = properties.getProperty("360.packagePath")

task assembleRelease360(dependsOn: 'assembleRelease') {
    group = "360JiaGu"
    //360加固
    //        https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html
    doLast {
        logInfo "=================== 360jiagu begin ==================="
        def apkPath = getReleaseApkPath()//apk位置
        def outputDirectory = getReleaseApkJiaguPath()//输出目录

        if (!new File(apkPath).exists()) {
            logError "apk not exist" + apkPath
            return
        }

        def jiaguDir = new File(outputDirectory)
        if (jiaguDir.exists()) {
            jiaguDir.deleteDir()
        }
        new File(outputDirectory).mkdirs()

        def cmdBase = "java.exe -jar  $softPath360\\jiagu.jar"
        def cmdLogin = cmdBase + " -login  ${userName360}   ${password360}"
        def cmdImportsign = cmdBase + ' -importsign ' +
                keyfile.getCanonicalPath() + ' ' +
                keystorePSW + ' ' +
                keystoreAlias + ' ' +
                keystoreAliasPSW
        def cmdShowSign = cmdBase + ' -showsign'
        def cmdConfig = cmdBase + ' -config -x86'
        def cmdShowConfig = cmdBase + ' -showconfig'
        def cmdVersion = cmdBase + ' -version'

        def cmdJiagu = cmdBase + " -jiagu $apkPath $outputDirectory -autosign"

        logInfo '360加固登录中 cmdLogin'
        execute360JiaguCmd(softPath360, cmdLogin)
        logInfo '360加固导入签名 cmdImportsign'
        execute360JiaguCmd(softPath360, cmdImportsign)
//        logInfo '360加固配置 cmdConfig'
//        execute360JiaguCmd(softPath360, cmdConfig)
        logInfo '360加固开始加固 cmdJiagu'
        execute360JiaguCmd(softPath360, cmdJiagu)
        logInfo "=================== 360jiagu end ==================="
    }
}

在task被添加的时候定义依赖关系
//tasks.whenTaskAdded { theTask ->
//    if (theTask.name == "assembleRelease") {
//        theTask.dependsOn "cleanOutputsDir"
//    }
//}
//
//task cleanOutputsDir {
//    def outputsPath = getBuildDir().getAbsolutePath() + File.separator + "outputs" + File.separator
//    println "delete outputsPath=" + outputsPath
//    // 删除旧的apk
//    new File(outputsPath).deleteDir()
//}

def getReleaseApkPath() {
    def apkDir = getBuildDir().getAbsolutePath() + File.separator +
            "outputs" + File.separator +
            "apk" + File.separator +
            "release"

    def apkDirFile = new File(apkDir)
    if (!apkDirFile.exists()) {
        throw new RuntimeException("apk output path not exists:" + apkDir)
    }

    def apkPath = null
    for (int i = apkDirFile.listFiles().length - 1; i >= 0; i--) {
        File file = apkDirFile.listFiles()[i]
        if (file.isFile() && file.name.endsWith(".apk")) {
            apkPath = file.getPath()
            break
        }
    }
    println("apkPath = $apkPath")
    return apkPath
}

def getReleaseApkJiaguPath() {
    def apkJiaguDir = getBuildDir().getAbsolutePath() + File.separator +
            "outputs" + File.separator +
            "apk" + File.separator +
            "release" + File.separator +
            "jiaguOutput"

    println("加固输出目录 = $apkJiaguDir")
    return apkJiaguDir
}

def execute360JiaguCmd(String package360Path, String cmd) {
    def file = new File(package360Path)
    if (!file.exists()) {
        logError "360 binPackagePath not exist " + package360Path
    }
    logInfo(cmd)
    def p = cmd.execute(null, file)
    logInfo p.text
    p.waitFor()  // 用以等待外部进程调用结束
    logInfo p.exitValue()
}

def logInfo(text) {
    println '【Info】' + text
}

def logError(text) {
    println '【错误】' + text
}

2、在app的build.gradle中引入脚本

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}
apply from: '../plug_360_jiagu.gradle'//引入360加固插件

3、在local.properties中配置签名和360信息

sdk.dir=C\:\\Android\\AndroidSDK
# 360加固保用户名
360.userName=***
# 360加固保登陆密码
360.password=***
# 加固保 安装的路劲
360.packagePath=D\:\\soft\\360jiagubao_windows_x64\\jiagu

## 签名信息
keystorePath=../***.jks
keystorePassword=***
keystoreAlias=***
keystoreAliasPassword=***

4、在Gradle面板中执行加固命令

在这里插入图片描述
点击“assembleRelease360”,自动 进行打包、加固。

加固完成后的apk:…/app/build/outputs/apk/release/jiaguOutput/***.apk

参考源码:JiaGuDemo

5、问题总结

1、根据实际情况,正确的指向360加固助手的安装位置,否则无法执行cmd命令;
2、根据控制台的打印信息、排查问题;
3、AS控制台日志乱码,借助 360加固助手查看错误信息;

问题:加固进度达到了100%,但是输出目录为空;错误信息又乱码了,无法排查:
请添加图片描述
解决:可以启动电脑端的 360加固助手.exe,登录进去后,查看错误信息:
请添加图片描述
4、免费加固次数限制规则:
1)未认证用户:可加固2次/天;
2)认证个人用户:可加固10次/天;
3)认证企业用户:可加固20次/天;
专业防篡改、高级防逆向、企业版加固服务用户及其他加固套餐用户,日加固次数不限。

5、官方 错误码常见问题

参考链接:

https://jiagu.360.cn/#/global/index
https://yanwenjing.blog.csdn.net/article/details/108519491
https://blog.csdn.net/m0_37048188/article/details/82758584

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值