gpt4 book ai didi

java - Gradle Java应用程序的Proguard示例

转载 作者:行者123 更新时间:2023-12-03 05:03:04 45 4
gpt4 key购买 nike

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

    关于java - Gradle Java应用程序的Proguard示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64355998/

    45 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com