首页 > 解决方案 > gradle java 应用程序的 Proguard 示例

问题描述

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

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'
    }
}
    

标签: javagradleproguard

解决方案


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

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

    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"
       }
    
  2. 复制依赖项

    task copyDepends(type: Copy) {
      from configurations.default
      into "${dependsDir}"
    }
    
  3. 使用 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

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

-keep public class * {
   public * ;
 }

用于混淆的 Gradle 命令

gradle createJar
gradle copyDepends
gradle proguard

推荐阅读