我是混淆技术的新手,试图弄清楚如何混淆使用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'
    }
}

最佳答案

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

  • 创建一个可运行的jar,并将所有依赖库复制到目录“dependencies”,然后在 list 中添加类路径。
    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 copyDepends(type: Copy) {
      from configurations.default
      into "${dependsDir}"
    }
    
  • 使用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
    

    07-26 02:01