java - ProGuard 破坏 JavaFX 应用程序

标签 java javafx proguard classnotfoundexception fxml

我试图混淆我的 JavaFX 应用程序,但它失败了。生成的结果不起作用,我不明白为什么。生成的 jar 会失败,因为 fxml 文件无法再加载所有导入(ClassNotFoundException)。

部署工作流:

  1. 构建可运行的 jar(在 IntelliJ 中被称为工件)
  2. 用 ProGuard 混淆那个 jar
  3. 修复该 jar 中 ProGuard 无法执行的一些问题

1) 最小的示例应用程序

示例应用程序“GuardTest”是一个由 3 个类组成的 IntelliJ 项目。

  • sample.Main:包含应用程序入口点并加载 GUI fxml 文件“sample.fxml”
  • sample.Controller:“sample.fxml”的 Controller 类
  • controls.CustomControl:一个简单的javafx 控件,继承自HBox。这用于“sample.fxml”

'sample.fxml'的内容:

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>

<?import controls.CustomControl?>

<VBox fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml">
    <children>
        <CustomControl>
            <children>
                <Button text="Test"></Button>
            </children>
        </CustomControl>
    </children>
</VBox>

2) 混淆

现在,我将 ProGuard 用于从上述项目生成的结果 jar 文件。我使用以下设置:

-target 8

-injars ./out/artifacts/JavaFXApp/JavaFXApp.jar

-outjars ./out/obfuscated/Obfuscated.jar
-ignorewarnings

-printmapping ./out/obfuscated/proguard.map
-dontusemixedcaseclassnames
-dontshrink
-dontoptimize
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers

#-flattenpackagehierarchy
-repackageclasses 'p'
-allowaccessmodification

-libraryjars "<java.home>/lib/rt.jar"
-libraryjars "<java.home>/lib/javaws.jar"
-libraryjars "<java.home>/lib/ext/jfxrt.jar"

-adaptresourcefilecontents **.fxml,**.properties,META-INF/MANIFEST.MF,images/*.jar,publicCerts.store,production.version

-keepattributes javafx.fxml.FXML,Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod
-keepclassmembers class * {
    @javafx.fxml.FXML *;
}

-keepclassmembernames public class com.javafx.main.Main, com.nywelt.sharkload.application.Main {
    public static void main(java.lang.String[]);
}
-keepclasseswithmembers public class com.javafx.main.Main, com.product.main.EntryFX, net.license.LicenseEntryPoint {
    public *; public static *;
}

3) 修复一些(明显的)ProGuard 故障

生成的 jar 文件“Obfuscated.jar”具有以下结构:

**Obfuscated.jar**
- META-INF
--> MANIFEST.MF
- p
--> a.class
--> b.class
--> c.class
- sample
--> sample.fxml

主类通过使用以下行加载“sample.fxml”文件来启动 GUI:

Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

因此,我必须将“sample.fxml”文件也移动到文件夹 p 中,以使上面的行再次运行。我还修复了 fxml 文件中 ProGuard 忘记更改(现已混淆)类名的一些问题。

现在结构如下所示:

**Obfuscated_fixed.jar**
- META-INF
--> MANIFEST.MF
- p
--> a.class
--> b.class
--> c.class
--> sample.fxml

sample.fxml 文件现在看起来像这样:

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>

<?import p.a?>

<VBox fx:controller="p.b"
          xmlns:fx="http://javafx.com/fxml">
    <children>
        <a>
            <children>
                <Button text="Test"></Button>
            </children>
        </a>
    </children>
</VBox>

问题

现在这个 jar 应该真的可以再次工作了,因为一切都恢复正常了。但事实并非如此! fxml 加载器无法加载 CustomControl(现在已命名/混淆为“a.class”)。这是为什么?

我在启动 jar 文件时得到以下错误输出(我正在运行 java 版本 1.8.0_40):

E:\Eigene Programme\GuardTest\out\obfuscated>java -jar Obfuscated_fixed.jar
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown So
urce)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(
Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda$49/849460928.run(Unkn
own Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
file:/E:/Eigene%20Programme/GuardTest/out/obfuscated/Obfuscated_fixed.jar!/p/sam
ple.fxml

        at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
        at javafx.fxml.FXMLLoader.importClass(Unknown Source)
        at javafx.fxml.FXMLLoader.processImport(Unknown Source)
        at javafx.fxml.FXMLLoader.processProcessingInstruction(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.load(Unknown Source)
        at p.c.start(Main.java:13)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159
(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda$52/663980963.run(Unkn
own Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown
 Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$46/410424423.run(Unkn
own Source)
        at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Sourc
e)
        at com.sun.javafx.application.PlatformImpl$$Lambda$48/1149216748.run(Unk
nown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown S
ource)
        at com.sun.javafx.application.PlatformImpl$$Lambda$47/1963387170.run(Unk
nown Source)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
        at com.sun.glass.ui.win.WinApplication$$Lambda$36/237061348.run(Unknown
Source)
        ... 1 more
Caused by: java.lang.ClassNotFoundException
        at javafx.fxml.FXMLLoader.loadType(Unknown Source)
        ... 26 more

E:\Eigene Programme\GuardTest\out\obfuscated>Pause
Drücken Sie eine beliebige Taste . . .

在主类中设置默认类加载器

FXMLLoader.setDefaultClassLoader(this.getClass().getClassLoader());

也没有帮助。

项目文件

您可以在此处找到示例项目 (IntelliJ): https://www.dropbox.com/s/ot51spvwk6lzo4k/GuardTest.zip?dl=0

IntelliJ 生成的 jar 工件被编译为: ./out/artifacts/JavaFXApp/JavaFXApp.jar

混淆后的 Jar 位于: ./out/obfuscated/Obfuscated.jar

混淆但固定(至少应该是)的 jar,如上所述: ./out/obfuscated/Obfuscated_fixed.jar

为了表明“sample.fxml”文件中的导入语句导致了问题,我从 fxml 文件中删除了我的自定义控件并将其保存到(工作)jar: ./out/obfuscated/Obfuscated_fixed_work.jar

很抱歉问了这么长的问题。无论如何,我希望你能帮助我 :)

最佳答案

我找到了解决方案!问题是 FXML 无法导入不以大写字母开头的类。因此,必须提供自己的可用名称列表,ProGuard 会使用该列表进行混淆。这是通过以下方式完成的:

-classobfuscationdictionary obfuscationClassNames.txt

使用 obfuscationClassNames.txt 包含可用类名的行分隔列表:

A
B
C
D
...

关于java - ProGuard 破坏 JavaFX 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29307262/

相关文章:

java - 长按在 ListView 行上不起作用

java - 多片段文件名的正则表达式模式

JavaFX : What is the proper method for scaling controls on a scene/stage/window?

java - 使用 ProGuard 导出 Android 应用程序会出现 ParseException 错误

java - 标识符预期困惑

java - 这个路径在java nio中的位置

java - 使用记事本在java中播放.mp3文件

java - 如何在 Java 中使用 ComboBox 数据填充 ArrayList

android - Proguard 返回错误代码 1。(文件名、目录名或卷标语法不正确)

Android Proguard 请更正上述警告