How to integrate Proguard obfuscation in my JavaFX's IntelliJ artifact?


I'm developing a JavaFX application using IntelliJ IDEA as the IDE.

So far, everything runs smoothly, I have all my external libs configured and my JavaFX artifact being correctly created.

Now I would like to integrate obfuscation (using Proguard) when the artifact is created.

IntelliJ have a 'Pre-processing' and 'Post-processing' option in artifact's properties where I can define an Ant task to be runned.

But I have no idea how to create that task and tell the compiler my obfuscation options.

Thank you very much in advance for any clues,

Best regards

12/7/2013 8:01:42 PM

Accepted Answer

There are 3 basic ways you do it:

  1. Create a normal proguard.cfg file, and reference it from ant
  2. Put your proguard settings directly inside ant using XML notation
  3. Put your proguard settings directly inside ant using the proguard.cfg format

Here's a basic example of using Proguard with Ant with the 3rd approach:

The important thing to remember about Proguard is that everything you want to obfuscate has to be inside a JAR file, and you'll need to explicitly tell it not to obfuscate certain things (like your program entry point, things you access via reflection, etc).

JavaFX creates a file used as an entry point you want to prevent obfuscating:

-keepclasseswithmembers public class com.javafx.main.Main {
    public *; public static *;

Make sure to include Java/JavaFX libs

-libraryjars "${java.home}/lib/rt.jar"
-libraryjars "${java.home}/lib/ant-javafx.jar"
-libraryjars "${java.home}/lib/jfxrt.jar"

If you're using FXML files, you'll want to make sure your FXML files are renamed similarly to their respective controller file:

-adaptresourcefilecontents **.fxml

Anything annotated with @FXML is accessed through reflection, so don't obfuscate them:

-keepclassmembernames class * {
        @javafx.fxml.FXML *;

The Proguard website has a lot of information, but it can be difficult to grok.

Honestly, there are plenty of examples on the web that show you how to do this. Just google javafx proguard, and you'll probably find some good complete examples.

Edit: as far as how IntelliJ passes information to Ant.. I don't know. There are probably some variables it passes in that you reference like a normal Ant propertly. I'm sure JetBrains website has info on that on their website if you can't find it on the net.

If it was me, I'd just create an ant script to compile my application without obfuscation, then add in proguard once you've got that squared away.

12/7/2013 11:13:41 PM

Just some complementing information, regarding running an Ant task in idea's 'post-processing'.

Make sure you have a 'default' target. Else the task wont execute. Example of build.xml:

<?xml version="1.0"?>
<project name="ant_task" default="obfuscate" basedir=".">
    <target name="obfuscate">
        <taskdef resource="proguard/ant/"
             classpath="/opt/proguard5.2.1/lib/proguard.jar" />
        <proguard configuration="proguard.cfg"/>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow