5

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!

David
  • 15,652
  • 26
  • 115
  • 156
Earthling
  • 283
  • 5
  • 17

1 Answers1

3

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>
Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102
  • The end result is still the same - A tiny 350byte jar file with no classes inside. (Just the Meta-INF) folder. The error output is: Failed to load Main-Class manifest attribute from C:\Users\foo\Java\ObsfucateTest\src\ObfuscateTest.jar Your help is very much appreciated though. – David Sep 28 '11 at 11:37
  • It also happens without the shrink lines in the xml altogether. – David Sep 28 '11 at 12:00
  • I just tried this, and I think my answer is correct. With your original shrink configuration the MainClass is removed and the JAR file is empty. With my shrink configuration the class is retained. Please have a look if ObfuscateTest.jar is empty or not, because that is the input to yguard. Maybe you have a problem in your directory layout. – Ingo Kegel Sep 28 '11 at 12:14
  • I went through and fixed some problems with the directory layout. The jar file now contains all the required files, but the Manifest file does not point to the main class for some reason. The manifest file just contains: Manifest-Version: 1.0 Ant-Version: Apache Ant 1.8.2 Created-By: 1.6.0_26-b03 (Sun Microsystems Inc.) (not Class-Path and Main-Class). Consequently, I still get the same error if I use java -jar myJarFile.jar: Failed to load Main-Class attribute... – David Sep 28 '11 at 14:14
  • OK, but that is an entirely different problem. The JAR file is not empty anymore. To set the main class in the manifest, add to your task. – Ingo Kegel Sep 28 '11 at 14:29