yWorks / yGuard

The open-source Java obfuscation tool working with Ant and Gradle by yWorks - the diagramming experts

Home Page:https://yworks.github.io/yGuard/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

support multiple keep for multiple class

cwengc opened this issue · comments

commented

After keep for public api, I can't include classes belonging to a sub package, see com.example.protocol.* below

 <!------------------- this part doesn't work ----------------------------->
  <class>
      <patternset>
          <include name="com.example.protocol.*"/>
      </patternset>
  </class>

Unfortunately, it is not possible to determine what the problem might be from the given information. Obviously, the correct configuration of yGuard's keep directive depends on your actual class and package names which we do not know.

Here is a working example (be sure to replace PATH_TO_YGUARD_INSTALL_DIR with the actual path to your yGuard installation directory):

<project
 name="yguard-keep-test"
 default="all"
 basedir=".">
  <target name="all">
    <property
     name="gen.src.root"
     value="${basedir}/tgt/generated-sources/com/example"/>

    <mkdir dir="${gen.src.root}/yguard"/>
    <mkdir dir="${gen.src.root}/protocol"/>

    <echo file="${gen.src.root}/yguard/ExampleClass1.java">package com.example.yguard;
      
public class ExampleClass1 {
  public static void main(String[] args) {
    System.out.println("ExampleClass1");
  }
}
</echo>
    <echo file="${gen.src.root}/yguard/ExampleClass2.java">package com.example.yguard;

public class ExampleClass2 {
  public static void main(String[] args) {
    System.out.println("ExampleClass2");
  }
}
</echo>
    <echo file="${gen.src.root}/protocol/ProtocolClass1.java">package com.example.protocol;

public class ProtocolClass1 {
  public static void main(String[] args) {
    System.out.println("ProtocolClass1");
  }
}
</echo>
    <echo file="${gen.src.root}/protocol/ProtocolClass2.java">package com.example.protocol;

public class ProtocolClass2 {
  public static void main(String[] args) {
    System.out.println("ProtocolClass2");
  }
}
</echo>


    <mkdir dir="${basedir}/tgt/classes"/>
    <javac
     includeantruntime="false"
     destdir="${basedir}/tgt/classes"
     srcdir="${basedir}/tgt/generated-sources"/>
 
    <jar
     destfile="${basedir}/tgt/example_unobf.jar"
     basedir="${basedir}/tgt/classes"/>

    <property
     name="yguard.home"
     value="PATH_TO_YGUARD_INSTALL_DIR"/>
    <path id="yguard.classpath">
      <pathelement location="${yguard.home}/lib/yguard-4.0.0.jar"/>
      <pathelement location="${yguard.home}/lib/asm-9.2.jar"/>
    </path>
    <taskdef
     name="yguard"
     classname="com.yworks.yguard.YGuardTask"
     classpathref="yguard.classpath"/>

    <yguard>
      <inoutpair
       in="${basedir}/tgt/example_unobf.jar"
       out="${basedir}/tgt/example_obf.jar"/>

      <rename logfile="${basedir}/tgt/example_obf_log.xml">
        <keep>
          <class>
            <patternset>
              <include name="com.example.protocol.*"/>
            </patternset>
          </class>
        </keep>
      </rename>
    </yguard>
  </target>
</project>

If you cannot solve the problem with the help of the above example, please provide a short, self-contained, complete example that lets us reproduce the problem.

commented

Is there an example with 2 class elements in keep? Is it supported?

    <keep>
      <class>
        <patternset>
          <include name="com.example.protocol.*"/>
        </patternset>
      </class>
      <class>
        <patternset>
          <include name="com.example.test.*"/>
        </patternset>
      </class>
    </keep>

Is there an example with 2 class elements in keep? Is it supported?

How about you take the working example and simply add a second class element to figure that out?

Nevermind the fact that it would be totally unnecessary to use two class elements for your example, because the two class elements can be simplified to

<keep>
  <class>
    <patternset>
      <include name="com.example.protocol.*"/>
      <include name="com.example.test.*"/>
    </patternset>
  </class>
</keep>

See Stackoverflow: yguard keep can't specify more than 1 class element.

You know, the fact that you are using contradictory directives would have been vital information for this question and it would have significantly reduced the effort and time to provide a solution for your problem. And you would only have had to properly format your original question to make that information readily available instead of hiding it.