带有自定义控件和自定义StringProperty的Proguard



我想将proguard与带有一些自定义控件的javafx应用程序一起使用。

  1. 我有一个自定义控件,它包含一个poperty

    StringProperty  textProperty = new SimpleStringProperty(this,"text");
    public final StringProperty textProperty() {return textProperty;}
    public final String getText() {return textProperty().get();}
    public final void setText(String text) { textProperty.set(text);}
    
  2. 我在fxml文件中使用自定义控件。

    <Menu [...] text="logo"/>
    

Menu类是我的自定义控件,所有导入语句都可以。

  1. 在我的pom文件中,我保持StringProperties原样。

    <option>-keepclassmembers class * extends javafx.scene.control.Control {javafx.beans.property.StringProperty *;
    public final javafx.beans.property.StringProperty *;
    public final java.lang.String *;
    public final void *;
    }</option>
    

启动应用程序时出现此错误。

Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "text" does not exist or is read-only.

编辑MRE

pom

<project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Example</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Java Fx libraries -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>16</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscate>true</obfuscate>
<optimize>true</optimize>
<shrink>true</shrink>
<injar>${project.build.finalName}.jar</injar>
<outjar>${project.build.finalName}.jar</outjar>
<includeDependency>true</includeDependency>
<options>
<option>-keep public class com.example.Main { *; }</option>
<option>-keep public class com.example.Launcher { *; }</option>
<option>-keepnames class com.example.custombuttons.*</option>
<option>-adaptresourcefilecontents **.fxml </option>
<option>-dontnote jdk.internal.jimage.*</option>
<option>-dontnote module-info</option>
<option>-dontnote jdk.internal.jrtfs.*</option>
<option>-dontnote jdk.internal.jimage.decompressor.*</option>
<option>-classobfuscationdictionary ${project.basedir}/keywords.txt</option>
<option>-keepclassmembers class * extends javafx.scene.control.Control {javafx.beans.property.StringProperty *;
public final javafx.beans.property.StringProperty *;
public final java.lang.String *;
public final void *;
}</option>
</options>
<libs>
<lib>${java.home}/jmods/java.base.jmod</lib>
<lib>${java.home}/jmods/java.xml.jmod</lib>
<lib>${java.home}/jmods/java.datatransfer.jmod</lib>
<lib>${java.home}/jmods/java.desktop.jmod</lib>
</libs>
<archive>
<manifest>
<mainClass>Main</mainClass>
<packageName>com.example</packageName>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Launcher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

启动器类

package com.example;
import javafx.application.Application;
public class Launcher
{
public static void main(String[] args) {
Application.launch(Main.class);
}
}

主要类别

package com.example;
import com.example.custombuttons.Menu;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application{
Menu menu;
@Override
public void start(Stage primaryStage) throws Exception {
AnchorPane pane;
FXMLLoader mainLoader = new FXMLLoader(getClass().getClassLoader().getResource("layouts/main.fxml"));
pane = mainLoader.load();
Scene scene = new Scene(pane, 600, 445);
primaryStage.setScene(scene);
primaryStage.show();
menu = (Menu) scene.lookup("#menu");
}
}

菜单类

package com.example.custombuttons;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
public class Menu extends Control{
StringProperty  textProperty = new SimpleStringProperty(this,"text");
public final StringProperty textProperty() {return textProperty;}
public final String getText() {return textProperty().get();}
public final void setText(String text) { textProperty.set(text);}
public Menu() {
super();
}
public Menu(String text){
super();
}
@Override protected Skin<?> createDefaultSkin() {
return new MenuSkin(this);
}
}

MemuSkin类

package com.example.custombuttons;
import javafx.scene.control.SkinBase;
public class MenuSkin extends SkinBase<Menu> {
String text;
public MenuSkin(Menu control) {
super(control);
text = control.getText();
}
}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import com.example.custombuttons.Menu?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" >
<children>
<Menu layoutX="238.0" layoutY="224.0" prefHeight="63.0" prefWidth="125.0" text="menu" />
</children>
</AnchorPane>

堆栈跟踪

Caused by: javafx.fxml.LoadException:
file:C:/USER/example/target/Example-1.0-SNAPSHOT.jar!/layouts/main.fxml:10
[...]
Caused by: com.sun.javafx.fxml.PropertyNotFoundException: Property "text" does not exist or is read-only.
at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:357)
at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:334)
at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:244)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:777)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2924)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639)

我发现我的通配符不正确。应该是,以一种通用的方式:

<option>-keepclassmembers class * extends javafx.scene.control.Control {
javafx.beans.property.* *;
void set*(***);
boolean is*();
*** get*();
}</option>

事实上,这将解决自定义控制文件中所有属性的问题。

最新更新