5

我目前正在开发我自己的 Java 库,以便在我办公室的其他应用程序中使用,并且我正在探索混淆库本身的方法。

我正在使用 Eclipse Indigo,并且正在使用 yGuard v2.3.0.1。

首先,我的Java代码:

package com.test.ObfuscateTest;

public class MainClass {

    private String secretClassVariable;

    public static void main(String[] args) {

    }

    protected void SecretMethod(String secretParameter) {

        secretClassVariable = secretParameter;
    }
}

将 yGuard 与以下 ANT 脚本一起使用,可以得到一个空的 JAR 文件:

<?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>

我可以就我做错的事情寻求一些建议吗?

提前非常感谢!

4

1 回答 1

3

收缩元件的用途不同。它指定 yguard 开始查找依赖类的入口点。所有无法到达的类都将被删除。通常你只需提供这样的主要方法:

<shrink logfile="${shrinklog}">
  <keep>
    <method name="void main(java.lang.String[])" class="${mainclass}" />
  </keep>
</shrink>
于 2011-09-28T10:32:52.773 回答