英文:
Proguard example for gradle java application
问题
我对混淆还不太了解,正在尝试弄清楚如何混淆使用Gradle创建的Java应用程序。思路是在Gradle构建后创建的可运行JAR文件进行混淆。以下是Gradle文件的内容:
plugins {
// 应用java插件以添加对Java的支持
id 'java'
// 应用application插件以添加构建CLI应用程序的支持
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
// 应用使用的依赖项
implementation 'com.google.guava:guava:29.0-jre'
// 使用JUnit测试框架
testImplementation 'junit:junit:4.13'
}
application {
// 定义应用程序的主类
mainClassName = 'com.abc.gradle.hello.App'
}
jar {
manifest {
attributes 'Main-Class': 'com.abc.gradle.hello.App'
}
}
英文:
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
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
得分: 8
以下是翻译好的内容:
最终,我通过以下步骤实现了这个目标:
- 创建一个可运行的 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"
}
- 复制依赖项。
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}"
}
// createJar 任务的定义
// proguard 任务的定义
proguard.conf 文件内容:
-keep public class * {
public * ;
}
使用 Gradle 命令进行混淆:
gradle createJar
gradle copyDepends
gradle proguard
请注意,这是根据您提供的内容进行的翻译。如果您有任何进一步的问题或需要进一步的帮助,请随时提问。
英文:
Finally I could achieve this with the following steps
-
Create a runnable jar with all dependent libraries copied to a directory "dependencies" and add the classpath in the manifest.
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" }
-
Copy the dependencies
task copyDepends(type: Copy) { from configurations.default into "${dependsDir}" }
-
Obfuscate the library with 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}" }
Here is the complete 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 commands to obfuscate
gradle createJar
gradle copyDepends
gradle proguard
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论