7

I need to keep all model classes to be unobfuscated, so I added this line in proguard rules to keep all model classes:

-keep class my_package_name.model.** { *; }

All model classes are getting kept by this command but still, it is obfuscating the annotations inside the Model classes. I tried adding the following line:

-keepattributes *Annotation*
-keepattributes EnclosingMethod

But still, results are same. My model classes contain these two annotations:

@SerializedName("message")
@Expose
private String message;

How can I keep the two annotations unobfuscated?

gabhor
  • 669
  • 9
  • 23
Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
  • It looks like you are using gson, so I would have a look to the recommended gson proguard configuration https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg – dmarin Feb 07 '18 at 10:34

6 Answers6

10

Try this:

-keepattributes *Annotation*
-keepattributes Signature
-dontnote sun.misc.**

-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

Actually, there is a proguard config in official repo on github https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

Alexander Deych
  • 446
  • 2
  • 6
7

Gson uses generic type information stored in a class file when working with fields. Proguard removes such information by default, so configure it to keep all of it.

Trying adding

-keepattributes Signature
-keepattributes EnclosingMethod
-keepattributes InnerClasses
-keepattributes Annotation

For using GSON @Expose annotation

-keepattributes *Annotation*

For Gson specific classes

-keep class sun.misc.Unsafe { *; }

Prevent proguard from stripping interface information from TypeAdapterFactory,JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)

-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
Muzammil Husnain
  • 1,218
  • 1
  • 10
  • 24
3

-keep @package.annotationclassname public class *

Robert K.
  • 1,043
  • 1
  • 8
  • 20
3

The rule

-keep class com.google.gson.annotations.*

will keep all annotations in the package com.google.gson.annotations including the SerializedName and Expose ones that you have used.

T. Neidhart
  • 6,060
  • 2
  • 15
  • 38
1

Add to your proguard : It's preventing specific classes to be obfuscated.

  1. Mandatory : -dontshrink. : https://www.guardsquare.com/en/proguard/manual/usage
  2. Not mandatory : try to use -dontoptimize.
  3. I don't really understand the issue, are you referring to a field or inner class ? If you have an inner class inside a class, you need to specify its inner fields. For example, if this is the class :

    public class Parent {
    
    protected Child child;
    
    protected class Child {
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

    }

In the proguard, you need to specify Child class :

-keep class com.package.name.Parent$Child {*; }

Dus
  • 4,052
  • 5
  • 30
  • 49
1

You may Need To Create an annotation called DontObfuscate in your project for more check this Managing obfuscation with annotations

Anh Tuan
  • 1,728
  • 1
  • 13
  • 25