gpt4 book ai didi

JavaFX Proguard 混淆

转载 作者:行者123 更新时间:2023-12-02 01:03:47 38 4
gpt4 key购买 nike

我正在努力解决 JavaFX 应用程序的混淆问题。使用此项目作为基础:

https://github.com/openjfx/samples/tree/master/IDE/IntelliJ/Non-Modular/Gradle

Proguard 抛出此错误:

java.io.IOException: Can't write [Path\infile.jar] (Can't read [Path\outfile.jar] (Duplicate jar entry [a.class]))

混淆器配置文件: -不优化 -不要缩小

-libraryjars 'E:\Prog\jdk-11.0.2\jmods'
-libraryjars 'E:\Prog\javafx-sdk\lib'

# Save meta-data for stack traces
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

# Rename FXML files together with related views
-adaptresourcefilenames **.fxml,**.png,**.css
-adaptresourcefilecontents **.fxml
-adaptclassstrings

# Keep all annotations and meta-data
-keepattributes *Annotation*,Signature,EnclosingMethod

# Keep entry-point class
-keep classpackage.App {
public static void main(java.lang.String[]);
}

# Keep all classes inside application
-keep,allowobfuscation class package.** {
}

# Keep names of fields marked with @FXML attribute
-keepclassmembers class * {
@javafx.fxml.FXML *;
}

有人有 JavaFX 混淆的经验吗?

最佳答案

要让 Proguard 与 Java 11 配合使用,我们需要:

  • 最新的 Proguard beta version ,在本例中适用于 Gradle。

  • 修改构建 gradle 文件以包含 proguard 任务。

  • 添加 proguard 配置文件,其中包括 Java 11 所需的更改。

构建.gradle

从 HelloFX sample 开始,构建将修改为:

// 1. Include proguard dependency
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'net.sf.proguard:proguard-gradle:6.1.0beta2'
}
}

plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.7'
}

repositories {
mavenCentral()
}

dependencies {
}

javafx {
modules = [ 'javafx.controls', 'javafx.fxml' ]
}

mainClassName = 'org.openjfx.MainApp'

我将添加更多任务来用混淆器替换默认的构建/类,而不是仅仅添加混淆器任务。这有助于检查结果。

// 2. Add tasks

// 2.1 Clean buildDir before running proguard
task cleanClasses(type: Delete) {
delete "${buildDir}/classes/java/main"
delete "${buildDir}/resources/java/main"
}

classes.dependsOn(cleanClasses)

// 2.2 Add proguard task
task proguard(type: proguard.gradle.ProGuardTask, dependsOn: classes) {
injars project.sourceSets.main.output
outjars "${buildDir}/proguard/output.jar"

libraryjars project.sourceSets.main.compileClasspath

configuration 'proguard.conf'
}

// 2.3 Clean after proguard task
task cleanAfterProguard(type: Delete, dependsOn: proguard) {
delete "${buildDir}/classes/java/main"
delete "${buildDir}/resources/java/main"
}

// 2.4 Extract output jar to buildDir
task unpackProguardOutput (type: Copy, dependsOn: cleanAfterProguard) {
from zipTree("${buildDir}/proguard/output.jar")
into file("${buildDir}/classes/java/main")
}

最后,添加一个任务来运行带有混淆类的应用程序。

// 3. Create a task to run the app with the proguarded buildDir
task runProguard(type: JavaExec, dependsOn: unpackProguardOutput) {
classpath = sourceSets.main.runtimeClasspath
jvmArgs = ['--module-path', classpath.asPath,
'--add-modules', 'javafx.controls,javafx.fxml' ]
main = 'a.a.b' // <-- this name will depend on the proguard result
}

proguard.conf

如何让它与 Java 9+ 一起工作的关键可以在这个 comment 中找到。如果您下载源代码,并检查示例文件夹,会发现有不同的配置文件。

检查applications.pro,您可以阅读:

# Before Java 9, the runtime classes were packaged in a single jar file.
#-libraryjars <java.home>/lib/rt.jar

# As of Java 9, the runtime classes are packaged in modular jmod files.
-libraryjars <java.home>/jmods/java.base.jmod(!**.jar;!module-info.class)

就是这样!

这是我在 HelloFX 示例中使用的配置文件(当然可以使用其他许多选项进行扩展):

-dontoptimize
-dontshrink

#Java 9+
-libraryjars <java.home>/jmods/java.base.jmod(!**.jar;!module-info.class)

# Save meta-data for stack traces
-printmapping out.map
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

# Rename FXML files together with related views
-adaptresourcefilenames **.fxml,**.png,**.css,**.properties
-adaptresourcefilecontents **.fxml
-adaptclassstrings

# Keep all annotations and meta-data
-keepattributes *Annotation*,Signature,EnclosingMethod

# Keep entry-point class
-keep class org.openfjx.MainApp {
public static void main(java.lang.String[]);
}

# Keep names of fields marked with @FXML, @Inject and @PostConstruct attributes
-keepclassmembers class * {
@javafx.fxml.FXML *;
@javax.inject.Inject *;
@javax.annotation.PostConstruct *;
}

结果

如果运行./gradlew proguard,您将获得output.jar

如果运行./gradlew unpackProguardOutput,您可以在build/classes中看到结果:

main
|____a
| |____a
| | |____styles.css
| | |____scene.fxml
| | |____b.class
| | |____a.class

在本例中,b.class 是主类,因此这就是为什么在 runProguard 任务中我设置了 main = 'a.a.b' 。显然,这取决于具体情况。

此外,您可以查看out.map:

org.openjfx.FXMLController -> a.a.a:
javafx.scene.control.Label label -> label
10:10:void <init>() -> <init>
17:20:void initialize(java.net.URL,java.util.ResourceBundle) -> initialize
org.openjfx.MainApp -> a.a.b:
11:11:void <init>() -> <init>
15:23:void start(javafx.stage.Stage) -> start
26:27:void main(java.lang.String[]) -> a

最后,./gradlew runProguard 将成功运行应用程序。

关于JavaFX Proguard 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54892406/

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