Question

I'm currently developing a Java library of my own to be used in other apps in my office, and I'm exploring ways of obfuscating the library itself.

I'm working with Eclipse Indigo, and am using yGuard v2.3.0.1.

Firstly, my Java code:

package com.test.ObfuscateTest;

public class MainClass {

    private String secretClassVariable;

    public static void main(String[] args) {

    }

    protected void SecretMethod(String secretParameter) {

        secretClassVariable = secretParameter;
    }
}

Using yGuard with the following ANT script, nets me an empty JAR file:

<?xml version="1.0" encoding="UTF-8"?>

<project name="project" default="yguard" basedir=".">

    <target name="init">
        <property name="project_name" value="ObfuscateTest"/>
        <property name="srcDir" value="."/>
        <property name="classDir" value="classes"/>
        <property name="jar" value="${project_name}.jar"/>
        <property name="obfjar" value="${project_name}_obf.jar"/>
        <property name="renamelog" value="${project_name}_renamelog.xml"/>
        <property name="shrinklog" value="${project_name}_shrinklog.xml"/>
        <property name="mainclass" value="com.test.ObfuscateTest"/>
        <mkdir dir="${classDir}" />
    </target>


    <target depends="jar" name="yguard">
        <taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" classpath="/Users/foo/Desktop/yguard-2.3.0.1/lib/yguard.jar"/>
        <!-- the following can be adjusted to your needs -->
        <yguard>

            <inoutpair in="${jar}" out="${obfjar}"/>

            <!-- don't let the obfuscator remove the "Deprecated" -->
            <!-- attributes from the .class file entries -->
            <attribute name="Deprecated"/>

            <shrink logfile="${shrinklog}">
                <keep>
                    <class classes="none" methods="protected" fields="none">
                            <patternset>
                              <include name="com.test.ObfuscateTest.*"/>
                            </patternset>
                          </class>
                </keep>
            </shrink>

            <rename mainclass="${mainclass}" logfile="${renamelog}">
                <keep>
                    <class classes="none" methods="protected" fields="none">
                            <patternset>
                              <include name="com.test.ObfuscateTest.*"/>
                            </patternset>
                          </class>
                </keep>
            </rename>

        </yguard>

    </target>

    <!-- compile -->
    <target name="compile" depends="init">
        <javac srcdir="${srcDir}" includes="com/test/ObfuscateTest/*.java" destdir="${classDir}">
        </javac>
    </target>

    <!-- create .jar -->
    <target name="jar" depends="compile">
        <jar jarfile="${jar}" basedir="${classDir}" includes="com/test/ObfuscateTest/*"/>
    </target>

    <!-- run project -->
    <target name="run" depends="yguard">
        <java classname="${mainclass}" fork="true">
            <classpath>
                <pathelement location="${obfjar}"/>
            </classpath>
        </java>
    </target>

    <!-- removes all that has been built -->
    <target name="clean" depends="init">
        <delete dir="${classDir}" includeEmptyDirs="true" />
    </target>
</project>

Could I kindly ask for some advice as to what I'm doing wrong?

Thanks a lot in advance!

Was it helpful?

Solution

The shrink element is meant to be used differently. It specifies the entry points from which yguard starts looking for dependent classes. All classes that cannot be reached are removed. Usually you just provide the main method like this:

<shrink logfile="${shrinklog}">
  <keep>
    <method name="void main(java.lang.String[])" class="${mainclass}" />
  </keep>
</shrink>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top