Obfuscating JavaFX


Introduction

Java classes are compiled into '.class' file from '.jar' file so that it could be loaded into machine memory and execute the program. In case of java '.class' file contains java byte code that can be executed in JVM (Java Virtual Machine). Unfortunately there are lots of open-source tools available (free of cost) for decompiling Java byte code back to source code. Yep! any one can easily decompile and modify source-code from your .jar file. There are lots of preventive methods for this. There are various tools available which can protect your java '.class' files.

Here, we are going to use 'ProGuard' which is a good Obfuscation tool. It does not encrypt your '.class' file but it makes source-code obtained by decompiling '.class' file not readable or difficult to understand.

oh... i have used JD (Java Decompiler) to decompile my .class file.

You can clearly see difference in the figure below:

.class file before decompiling with Java Decompiler
.class file before decompiling with Java Decompiler
.class file after decompiling with Java Decompiler
.class file after decompiling with Java Decompiler


What am I using:

I am using NetBeans 8.0, Java Decompiler 0.3.6, and ProGuard 5.0.
Instead of using GUI for Obfuscating my .class files inside jar with the help of proguard, I am creating a configuration file i.e in the format of  '.pro' with the help of proguard-gui and putting it directly inside ant using XML notation. This creates obfuscated jar file inside desired location during building process itself.


Process:

Process is very simple actually.

**Note: Configuration file is already created with the help of ProGuard-gui.

copy your 'something.pro' file inside netbeans project file (side by side to your 'src' file.) For example:

Now open your configuration file and edit (add) following things:
Line number 20 of above picture (underlined in red) shows path of your main class (class containing main method.)..... You can leave rest as it is.


Now open your 'build.xml' file and add following lines before closing of 'project' tag i.e, </project>

<target name="-post-jfx-jar">
        <!-- obfuscate and optimize by ProGuard -->
        <taskdef resource="proguard/ant/task.properties" classpath="lib/proguard.jar" />
        <proguard configuration="config.pro">
        </proguard>
        <move 
            file="${dist.jar.dir}/${ant.project.name}.jar" 
            tofile="${dist.jar}" verbose="true" overwrite="true" />
    </target>

You must also assign the path of 'proguard.jar' file as shown above. 'config.pro' is your configuration file.

And that's all. You will have Obfuscated '.jar' file every time you build or clean-build your damn project.


For Maven Project

Open your 'pom.xml' file add following lines:


<plugin>
                <groupId>com.pyx4me</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <version>2.0.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <proguardVersion>5.0</proguardVersion>
                    <obfuscate>true</obfuscate>
                    <injar>${project.build.finalName}.jar</injar>
                    <outjar>${project.build.finalName}-small.jar</outjar>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                    <proguardInclude>${basedir}/config.pro</proguardInclude>
                    <libs>
                        <lib>${java.home}/lib/rt.jar</lib>
                        <lib>${java.home}/lib/jfxrt.jar</lib>
                        <lib>${java.home}/lib/jsse.jar</lib>
                    </libs>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.proguard</groupId>
                        <artifactId>proguard</artifactId>
                        <version>5.0</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks a lot. You saved me :)

    ReplyDelete
  3. I have followed the exact steps but still i'm getting

    Jul 29, 2019 12:47:05 PM com.orangegroup.gems.app.LoginApplication start
    SEVERE: Error while loading login page
    javafx.fxml.LoadException:

    ReplyDelete
  4. Hi, thank you for your helpful post.
    However, i would like to know how you approach the obfuscation of fxml controller classes

    ReplyDelete