JavaFX Proguard 混淆

JavaFX Proguard Obfuscation

我正在努力解决 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]))

Proguard 配置文件: -不要优化 -不要收缩

-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 与 Java11 一起工作,我们需要:

  • 最新的 Proguard beta version,在本例中为 Gradle。

  • 修改构建 gradle 文件以包含混淆器任务。

  • 添加混淆器配置文件,包括 Java 11.

  • 所需的更改

Build.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'

除了添加 proguard 任务之外,我将添加更多任务以将默认的 build/classes 替换为受保护的任务。这有助于检查结果。

// 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")
}

最后,将任务添加到 运行 具有保护 classes 的应用程序。

// 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 中找到。如果你下载了源代码,查看examples文件夹,里面有不同的配置文件。

查看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 是主要的 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申请成功运行