Gradle Java应用程序的Proguard示例 [英] Proguard example for gradle java application

查看:114
本文介绍了Gradle Java应用程序的Proguard示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是迷惑的新手,试图弄清楚如何迷惑使用gradle创建的Java应用程序.这个想法是在gradle构建之后创建的可运行jar会被混淆.这是gradle文件

I am new to obfuscation and trying to figure out how to obfuscate a java application created using gradle. The idea is that the runnable jar created after the gradle build is obfuscated. Here is the gradle file

plugins {
    // Apply the java plugin to add support for Java
    id 'java'
    // Apply the application plugin to add support for building a CLI application.
    id 'application'
}
repositories {
     mavenCentral()
}
dependencies {
    // This dependency is used by the application.
    implementation 'com.google.guava:guava:29.0-jre'
    // Use JUnit test framework
    testImplementation 'junit:junit:4.13'
}
application {
    // Define the main class for the application.
    mainClassName = 'com.abc.gradle.hello.App'
}
jar {
    manifest {
        attributes 'Main-Class': 'com.abc.gradle.hello.App'
    }
}
    

推荐答案

最后我可以通过以下步骤实现

Finally I could achieve this with the following steps

  1. 创建一个可运行的jar,将所有依赖库复制到目录"dependencies"中.并在清单中添加类路径.

  1. Create a runnable jar with all dependent libraries copied to a directory "dependencies" and add the classpath in the manifest.

task createJar(type: Jar) {
   println("Cleaning...")
   clean
   manifest {
   attributes('Main-Class': 'com.abc.gradle.hello.App',
     'Class-Path': configurations.default.collect { 'dependencies/' + 
      it.getName() }.join(' ')
      )
   }
   from {
      configurations.compile.collect { it.isDirectory() ? it : zipTree(it) 
      }
   } with jar
   println "${outputJar} created"
   }

  • 复制依赖项

  • Copy the dependencies

    task copyDepends(type: Copy) {
      from configurations.default
      into "${dependsDir}"
    }
    

  • 使用Proguard对库进行模糊处理

  • Obfuscate the library with Proguard

    task proguard(type: proguard.gradle.ProGuardTask) {
       println("Performing obfuscation..")
       configuration 'proguard.conf'
       injars "${outputJar}"
       outjars "${buildDir}/libs/${rootProject.name}_proguard.jar"
       libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
       libraryjars "${dependsDir}"
     }
    

  • 这是完整的build.gradle

    Here is the complete build.gradle

    buildscript {
     repositories {
        mavenCentral()
     }
     dependencies {
        classpath 'net.sf.proguard:proguard-gradle:6.0.3'
        classpath 'net.sf.proguard:proguard-base:6.0.3'
     }
    }
    
    plugins {
     id 'java'
     id 'application'
    }
    
    repositories {
      mavenCentral()
    }
    
    dependencies {
       implementation 'org.slf4j:slf4j-api:1.7.30'
       implementation 'ch.qos.logback:logback-classic:1.2.3'
       implementation 'ch.qos.logback:logback-core:1.2.3'
       testImplementation 'junit:junit:4.13'
    }
    
    def outputJar = "${buildDir}/libs/${rootProject.name}.jar"
    def dependsDir = "${buildDir}/libs/dependencies/"
    def runnableJar = "${rootProject.name}_fat.jar";
    
    task copyDepends(type: Copy) {
     from configurations.default
     into "${dependsDir}"
    }
    
    task createJar(type: Jar) {
     println("Cleaning...")
     clean
     manifest {
        attributes('Main-Class': 'com.abc.gradle.hello.App',
                'Class-Path': configurations.default.collect { 'dependencies/' + 
       it.getName() }.join(' ')
        )
      }
      from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
       } with jar
       println "${outputJar} created"
      }
    
    task proguard(type: proguard.gradle.ProGuardTask) {
       println("Performing obfuscation..")
       configuration 'proguard.conf'
       injars "${outputJar}"
       outjars "${buildDir}/libs/${rootProject.name}_proguard.jar"
    
       libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
       libraryjars "${dependsDir}"
    
      }
    

    Proguard.conf

    Proguard.conf

    -keep public class * {
       public * ;
     }
    

    模糊的分级命令

    gradle createJar
    gradle copyDepends
    gradle proguard
    

    这篇关于Gradle Java应用程序的Proguard示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆