Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proguard obfuscating Annotations

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?

like image 671
Jinesh Francis Avatar asked Jan 22 '18 07:01

Jinesh Francis


People also ask

Does ProGuard obfuscate XML?

ProGuard can't obfuscate strings. xml. You can use other software to obfuscate your file like DexGuard. You can get more info here.

Does ProGuard obfuscate package name?

By default, ProGuard obfuscates the code: it assigns new short random names to classes and class members. It removes internal attributes that are only useful for debugging, such as source files names, variable names, and line numbers.

How does ProGuard obfuscation work?

In the obfuscation step, ProGuard renames classes and class members that are not entry points. In this entire process, keeping the entry points ensures that they can still be accessed by their original names. The preverification step is the only step that doesn't have to know the entry points.


4 Answers

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

like image 113
Alexander Deych Avatar answered Oct 07 '22 11:10

Alexander Deych


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
like image 37
Muzammil Husnain Avatar answered Oct 07 '22 12:10

Muzammil Husnain


-keep @package.annotationclassname public class *

like image 3
Robert K. Avatar answered Oct 07 '22 10:10

Robert K.


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.

like image 3
T. Neidhart Avatar answered Oct 07 '22 12:10

T. Neidhart