无法在proguard中添加保持类名如何混淆Javafx项目?

问题描述

我正在尝试对Javafx项目进行混淆,并且在混淆之后无法打开该项目。搜索了一些解决方案,告诉要添加一些keepname属性,但无法弄清楚该怎么做。我在混淆前后添加了控制器类。我已经将@FXML添加到keep属性仍然收到错误

混淆配置:

classobfuscationdictionary 'C:\Users\User\Desktop\obfuscationClassNames.txt'
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod,'@FXML'
-keepparameternames
-adaptresourcefilecontents **.fxml,**.properties,meta-inf/MANIFEST.MF



-keepclasseswithmembers class * {
    *** @javafx.fxml.FXML *;
}

-keepclasseswithmembers public class com.javafx.main.Main,obfuscationexample.ObfuscationExample {
    public static void main(java.lang.String[]);
}

 Keep - Applications. Keep all application classes,along with their 'main' methods.
-keepclasseswithmembers public class * {
    public static void main(java.lang.String[]);
}

 Also keep - Enumerations. Keep the special static methods that are required in
 enumeration classes.
-keepclassmembers enum  * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

控制器类

public class SampleController extends borderpane implements Initializable {
  @FXML
  private Button btnEcrypt;
  
  @FXML
  private Label lblStatus;
  
  @FXML
  private TextField txtEncrypted;
  
  @FXML
  private TextField txtInput;
  
  public SampleController() {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Sample.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);
    try {
      fxmlLoader.load();
    } catch (IOException exception) {
      throw new RuntimeException(exception);
    } 
  }
  
  public void initialize(URL fxmlFileLocation,ResourceBundle resources) {
    assert this.btnEcrypt != null : "fx:id=\"btnEcrypt\" was not injected: check your FXML file 'Sample.fxml'.";
    assert this.lblStatus != null : "fx:id=\"lblStatus\" was not injected: check your FXML file 'Sample.fxml'.";
    assert this.txtEncrypted != null : "fx:id=\"txtEncrypted\" was not injected: check your FXML file 'Sample.fxml'.";
    assert this.txtInput != null : "fx:id=\"txtInput\" was not injected: check your FXML file 'Sample.fxml'.";
  }
  
  @FXML
  public void handleButtonAction(ActionEvent event) {
    EncryptionService service = new EncryptionService();
    String text = service.encrypt(this.txtInput.getText());
    this.txtEncrypted.setText(text);
    String status = messageformat.format("Text ''{0}'' was encrypted.",new Object[] { this.txtInput.getText() });
    this.lblStatus.setText(status);
  }
}

混淆后

public class A extends borderpane implements Initializable {
  public A() {
    FXMLLoader fxmlLoader;
    (fxmlLoader = new FXMLLoader(getClass().getResource("Sample.fxml"))).setRoot(this);
    fxmlLoader.setController(this);
    try {
      fxmlLoader.load();
      return;
    } catch (IOException exception) {
      throw new RuntimeException(exception);
    } 
  }
  
  public void initialize(URL fxmlFileLocation,ResourceBundle resources) {
    if (!a)
      throw new AssertionError("fx:id=\"btnEcrypt\" was not injected: check your FXML file 'Sample.fxml'."); 
    if (!a)
      throw new AssertionError("fx:id=\"lblStatus\" was not injected: check your FXML file 'Sample.fxml'."); 
    if (!a)
      throw new AssertionError("fx:id=\"txtEncrypted\" was not injected: check your FXML file 'Sample.fxml'."); 
    if (!a)
      throw new AssertionError("fx:id=\"txtInput\" was not injected: check your FXML file 'Sample.fxml'."); 
  }
}

错误

Exception in Application start method
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        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(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
        at java.lang.Thread.run(UnkNown Source)
Caused by: java.lang.RuntimeException: javafx.fxml.LoadException: Error resolving onAction='#handleButtonAction',either the event handler is not in the Namespace or there is an error in the script.
file:/C:/Users/User/Downloads/ObfuscationExample_0/dist/Obfuscation.jar!/obfuscationexample/Sample.fxml:27

        at obfuscationexample.A.<init>(SampleController.java:44)
        at obfuscationexample.ObfuscationExample.start(ObfuscationExample.java:16)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
        at com.sun.glass.ui.invokelaterdispatcher$Future.run(invokelaterdispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
        ... 1 more
Caused by: javafx.fxml.LoadException: Error resolving onAction='#handleButtonAction',either the event handler is not in the Namespace or there is an error in the script.
file:/C:/Users/User/Downloads/ObfuscationExample_0/dist/Obfuscation.jar!/obfuscationexample/Sample.fxml:27

        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
        at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
        at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
        at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
        at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
        at obfuscationexample.A.<init>(SampleController.java:42)
        ... 10 more
Exception running application obfuscationexample.ObfuscationExample

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)