Search code examples
javamavenantyguard

Obfuscating specific packages in a fat jar (maven project) with yGuard


I'm trying to create an obfuscated fat jar for my tool. After trying proguard, I found yGuard for the same purpose.

The following is the steps for obfuscation

  • Step 1: Create fat jar with maven shaded plugin

  • Step 2: The fat jar created in step 1 shall be used by yGuard ant task.

I need to obfuscate the custom packages only, since obfuscating external jars like batik library gives errors. Eg:

Caused by: java.io.IOException: An error ('No mapping found for: Field org/apache/batik/bridge/SVGPathElementBridge.ctx') occured during the remapping! See the log!)
    at com.yworks.yguard.obf.GuardDB.remapTo(GuardDB.java:547)
    at com.yworks.yguard.ObfuscatorTask.execute(ObfuscatorTask.java:1064)

Based on the ygurd documentation I have modified the ant task to include only the custom packages. But this is not happening, yGuard not considering this configuration. The following is the current ant task configuration.

    <target name="yguard" depends="jar">
    <taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" classpath="${projectDir}/lib/yguard-${version}.jar" />
    <yguard>
        <inoutpair in="${jar}" out="${obfjar}" />

        <shrink logfile="${shrinklog}">
            <keep>
                <!-- main method -->
                <method name="void main(java.lang.String[])" class="${mainclass}" />
            </keep>
        </shrink>

        <rename mainclass="${mainclass}" logfile="${renamelog}">
            <property name="error-checking" value="pedantic" />
            <keep>
                <class>
                    <patternset>
                        <!-- Custom library - should be obfuscated -->
                        <include name="com.acme.**.*" />
                        <!-- Excluded library -->
                        <exclude name="org.apache.**.*" />
                        <exclude name="javx.**.*" />
                    </patternset>
                </class>
            </keep>
        </rename>
    </yguard>
</target>

Any pointers to solve this issue highly appreciated. Thanks in advance


Solution

  • The <rename> element's <keep> child works exactly the opposite way: If you <include> a class in <keep>, this class is not renamed. I.e. <include> means "include in the set of classes whose names are not changed".

    Since you want to prevent Batik stuff from being renamed, your configuration should be

    <keep>
      <class>
        <patternset>
          <include name="org.apache.**.*"/>
          <include name="javax.**.*"/>
        </patternset>
      </class>
    </keep>
    

    You do not need <exclude> for your use case. <exclude> is only necessary, if you want to rename a subset of the classes that should not be renamed.