微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Gradle Java应用程序的Proguard示例

如何解决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'
    }
}
    

解决方法

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

  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 createJar
gradle copyDepends
gradle proguard

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。