Search code examples
javaenumsgsonobfuscationyguard

Obfuscating Java in Gradle using yGuard ~ How to exclude enum?


My project obfuscates Java code in Gradle using yGuard.

I have a package of classes and enums that are being serialized with Gson.

Each enum value has a @SerializedName annotation, like:

  package com.package.whatever;

  import com.google.gson.annotations.SerializedName;

  public enum Enum1 {
    @SerializedName("item1")
    item1,
    @SerializedName("item2")
    item2,
    ;
  }

I have configured yguard on gradle to stop obfuscating the classes on the package where Enum1 is located, like this:

ant.yguard() {
  inoutpair(in: shadowJar.outputs.files.singleFile, out: 
     "${buildDir}/libs/${shadowJar.archiveName}")
  rename(logfile: "${buildDir}/libs/yguard.log.xml", replaceClassNameStrings: 'true') {
    keep {
      'class'(classes: 'public', methods: 'public') {
        patternset {
          include(name: 'com.package.whatever.*')
        }
      }
    }

This is stopping obfuscating for methods in the classes within the com.package.whatever package, but it's not stopping obfuscation from happening within the enum values.

The resulting yguard.log.xml file ends up having records like this:

<field class="com.package.whatever.Enum1" name="item1" map="D"/>
<field class="com.package.whatever.Enum1" name="item2" map="E"/>

How can I avoid this obfuscating and cause yguard to skip the enum values?


Solution

  • Try to filter by the class name com.package.whatever.Enum1 and fields: 'friendly':

    keep {
      'class'(classes: 'public', methods: 'public', fields: 'friendly') {
        patternset {
          include(name: 'com.package.whatever.Enum1')
        }
      }
    }
    

    See the class element (which also explains the access modifiers).