Home > Mobile >  Proguard with custom control and custom StringProperty
Proguard with custom control and custom StringProperty

Time:11-19

I want to use proguard with a javafx application with some custom controls.

  1. I have a custom control that contains a 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. I use my custom control in an fxml file.

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

The Menu class is my custom control all the import statements are ok.

  1. In my pom file I keep the StringProperties as is.

     <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>
    

I get this error when I start the application.

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

EDIT MRE

The 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>

The launcher class.

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

The 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");
    }
}

Menu class

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 class

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>

Stack trace

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)

CodePudding user response:

I found out that my wild cards were not correct. Should have been, in a generic way:

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

This will, in fact, fix the problem for all the properties in the custom control files.

  • Related