美文网首页Gradle
使用gradle脚本自动化完成360加固

使用gradle脚本自动化完成360加固

作者: 2f479b1467f1 | 来源:发表于2019-04-04 10:29 被阅读0次

    1.首先我们要完成360固件包的下载,下载后解压
    2.编写自动化加固的脚本,这里是用在gradle里面去执行命令行的方式完成的,这个脚本放在App module的根目录下 命名为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/kevin/jiagu/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 cmdversion = cmdBase + ' -version '
            //这里要替换360加固的用户名密码
        def cmdLogin = cmdBase + ' -login ' + '360加固用户名' + ' ' + '360加固密码'
          //这里要替换你的签名相关
        def cmdSign = cmdBase + ' -importsign ' + getJksPath() + ' storePassword ' + 'keyAlias ' + 'keyPassword'
        def cmdJiaGu = cmdBase + ' -jiagu ' + appFilePath + ' ' + outPath + ' -autosign' + ' -automulpkg'
        executeCmd(cmdversion)
        executeCmd(cmdLogin)
        executeCmd(cmdSign)
        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
    }
    
    def getJksPath(){
            //这里要替换keystore文件名 支持keystore 和 jks
        def outPath = getRootDir().getAbsolutePath() + File.separator + "app" + File.separator + "kestore name"
        println "jksPath=" + 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()
    }
    

    3.在app的gradle里面导入jiagu.gradle
    apply from: 'jiagu.gradle'

    4.找到根目录下local.properties 在里面添加jiagu.jar的路径 格式如下
    这里修改成你本地存放加固jar包的地址 指向到上层目录即可
    jiagu.dir=/Users/kevin/jiagu/jiagu
    此处应该注意:1.目录是到jiagu.jar路径上层的目录
    2.解压好的360加固包请不要单独把jiagu.jar单独复制出来,上面的路径一定要是解压包的完成地址
    否则会出现classnotfoundexception
    5.完成后,编译项目,此时打开右侧的gradle



    app下会出现jiaguapk的group,双击jiaguRelease即可

    相关文章

      网友评论

        本文标题:使用gradle脚本自动化完成360加固

        本文链接:https://www.haomeiwen.com/subject/rlxfiqtx.html