Gradle Java应用的Proguard示例

huangapple go评论104阅读模式
英文:

Proguard example for gradle java application

问题

  1. 我对混淆还不太了解,正在尝试弄清楚如何混淆使用Gradle创建的Java应用程序。思路是在Gradle构建后创建的可运行JAR文件进行混淆。以下是Gradle文件的内容
  2. plugins {
  3. // 应用java插件以添加对Java的支持
  4. id 'java'
  5. // 应用application插件以添加构建CLI应用程序的支持
  6. id 'application'
  7. }
  8. repositories {
  9. mavenCentral()
  10. }
  11. dependencies {
  12. // 应用使用的依赖项
  13. implementation 'com.google.guava:guava:29.0-jre'
  14. // 使用JUnit测试框架
  15. testImplementation 'junit:junit:4.13'
  16. }
  17. application {
  18. // 定义应用程序的主类
  19. mainClassName = 'com.abc.gradle.hello.App'
  20. }
  21. jar {
  22. manifest {
  23. attributes 'Main-Class': 'com.abc.gradle.hello.App'
  24. }
  25. }
英文:

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

  1. plugins {
  2. // Apply the java plugin to add support for Java
  3. id 'java'
  4. // Apply the application plugin to add support for building a CLI application.
  5. id 'application'
  6. }
  7. repositories {
  8. mavenCentral()
  9. }
  10. dependencies {
  11. // This dependency is used by the application.
  12. implementation 'com.google.guava:guava:29.0-jre'
  13. // Use JUnit test framework
  14. testImplementation 'junit:junit:4.13'
  15. }
  16. application {
  17. // Define the main class for the application.
  18. mainClassName = 'com.abc.gradle.hello.App'
  19. }
  20. jar {
  21. manifest {
  22. attributes 'Main-Class': 'com.abc.gradle.hello.App'
  23. }
  24. }

答案1

得分: 8

以下是翻译好的内容:

最终,我通过以下步骤实现了这个目标:

  1. 创建一个可运行的 JAR 文件,将所有依赖库复制到一个名为 "dependencies" 的目录中,并在清单中添加类路径。
  1. task createJar(type: Jar) {
  2. println("Cleaning...")
  3. clean
  4. manifest {
  5. attributes('Main-Class': 'com.abc.gradle.hello.App',
  6. 'Class-Path': configurations.default.collect { 'dependencies/' +
  7. it.getName() }.join(' ')
  8. )
  9. }
  10. from {
  11. configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  12. }
  13. with jar
  14. println "${outputJar} created"
  15. }
  1. 复制依赖项。
  1. task copyDepends(type: Copy) {
  2. from configurations.default
  3. into "${dependsDir}"
  4. }
  1. 使用 Proguard 对库进行混淆。
  1. task proguard(type: proguard.gradle.ProGuardTask) {
  2. println("Performing obfuscation..")
  3. configuration 'proguard.conf'
  4. injars "${outputJar}"
  5. outjars "${buildDir}/libs/${rootProject.name}_proguard.jar"
  6. libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
  7. libraryjars "${dependsDir}"
  8. }

以下是完整的 build.gradle 文件:

  1. buildscript {
  2. repositories {
  3. mavenCentral()
  4. }
  5. dependencies {
  6. classpath 'net.sf.proguard:proguard-gradle:6.0.3'
  7. classpath 'net.sf.proguard:proguard-base:6.0.3'
  8. }
  9. }
  10. plugins {
  11. id 'java'
  12. id 'application'
  13. }
  14. repositories {
  15. mavenCentral()
  16. }
  17. dependencies {
  18. implementation 'org.slf4j:slf4j-api:1.7.30'
  19. implementation 'ch.qos.logback:logback-classic:1.2.3'
  20. implementation 'ch.qos.logback:logback-core:1.2.3'
  21. testImplementation 'junit:junit:4.13'
  22. }
  23. def outputJar = "${buildDir}/libs/${rootProject.name}.jar"
  24. def dependsDir = "${buildDir}/libs/dependencies/"
  25. def runnableJar = "${rootProject.name}_fat.jar"
  26. task copyDepends(type: Copy) {
  27. from configurations.default
  28. into "${dependsDir}"
  29. }
  30. // createJar 任务的定义
  31. // proguard 任务的定义

proguard.conf 文件内容:

  1. -keep public class * {
  2. public * ;
  3. }

使用 Gradle 命令进行混淆:

  1. gradle createJar
  2. gradle copyDepends
  3. gradle proguard

请注意,这是根据您提供的内容进行的翻译。如果您有任何进一步的问题或需要进一步的帮助,请随时提问。

英文:

Finally I could achieve this with the following steps

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

    1. task createJar(type: Jar) {
    2. println("Cleaning...")
    3. clean
    4. manifest {
    5. attributes('Main-Class': 'com.abc.gradle.hello.App',
    6. 'Class-Path': configurations.default.collect { 'dependencies/' +
    7. it.getName() }.join(' ')
    8. )
    9. }
    10. from {
    11. configurations.compile.collect { it.isDirectory() ? it : zipTree(it)
    12. }
    13. } with jar
    14. println "${outputJar} created"
    15. }
  2. Copy the dependencies

    1. task copyDepends(type: Copy) {
    2. from configurations.default
    3. into "${dependsDir}"
    4. }
  3. Obfuscate the library with Proguard

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

Here is the complete build.gradle

  1. buildscript {
  2. repositories {
  3. mavenCentral()
  4. }
  5. dependencies {
  6. classpath 'net.sf.proguard:proguard-gradle:6.0.3'
  7. classpath 'net.sf.proguard:proguard-base:6.0.3'
  8. }
  9. }
  10. plugins {
  11. id 'java'
  12. id 'application'
  13. }
  14. repositories {
  15. mavenCentral()
  16. }
  17. dependencies {
  18. implementation 'org.slf4j:slf4j-api:1.7.30'
  19. implementation 'ch.qos.logback:logback-classic:1.2.3'
  20. implementation 'ch.qos.logback:logback-core:1.2.3'
  21. testImplementation 'junit:junit:4.13'
  22. }
  23. def outputJar = "${buildDir}/libs/${rootProject.name}.jar"
  24. def dependsDir = "${buildDir}/libs/dependencies/"
  25. def runnableJar = "${rootProject.name}_fat.jar";
  26. task copyDepends(type: Copy) {
  27. from configurations.default
  28. into "${dependsDir}"
  29. }
  30. task createJar(type: Jar) {
  31. println("Cleaning...")
  32. clean
  33. manifest {
  34. attributes('Main-Class': 'com.abc.gradle.hello.App',
  35. 'Class-Path': configurations.default.collect { 'dependencies/' +
  36. it.getName() }.join(' ')
  37. )
  38. }
  39. from {
  40. configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  41. } with jar
  42. println "${outputJar} created"
  43. }
  44. task proguard(type: proguard.gradle.ProGuardTask) {
  45. println("Performing obfuscation..")
  46. configuration 'proguard.conf'
  47. injars "${outputJar}"
  48. outjars "${buildDir}/libs/${rootProject.name}_proguard.jar"
  49. libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
  50. libraryjars "${dependsDir}"
  51. }

Proguard.conf

  1. -keep public class * {
  2. public * ;
  3. }

Gradle commands to obfuscate

  1. gradle createJar
  2. gradle copyDepends
  3. gradle proguard

huangapple
  • 本文由 发表于 2020年10月14日 22:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64355998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定