Using Proguard with Gson for an Android application

7,515 views
Skip to first unread message

inde...@gmail.com

unread,
Jul 7, 2011, 3:37:54 PM7/7/11
to googl...@googlegroups.com

I am trying to configure my Android application to use Proguard. The primary reason is to rename the Gson packages to avoid conflicts with an older Gson version shipped with the phone platform. See http://code.google.com/p/google-gson/issues/detail?id=255 

I am running into a problem while using TypeToken. I have defined a field like this:
    private static final Type MAP_TYPE = new TypeToken<Map<String, String>>(){}.getType();

Dalvik VM throws the following error:
07-07 13:08:33.189: ERROR/AndroidRuntime(1000): Caused by: java.lang.RuntimeException: Missing type parameter.
07-07 13:08:33.189: ERROR/AndroidRuntime(1000):     at com.google.gson.reflect.TypeToken.<init>(Unknown Source)
07-07 13:08:33.189: ERROR/AndroidRuntime(1000):     at com.google.greaze.definition.rest.MetaDataBase$GsonTypeAdapter$1.<init>(Unknown Source)
07-07 13:08:33.189: ERROR/AndroidRuntime(1000):     at com.google.greaze.definition.rest.MetaDataBase$GsonTypeAdapter.<clinit>(Unknown Source)

My hunch is that Proguard threw away the type arguments to the anonymous class definition in TypeToken and that is resulting in this error. 
I tried setting:
 -keep class * extends com.google.gson.reflect.TypeToken

but that didnt help anything. 

When I essentially turn off proguard by setting the following in proguard.cfg: 

-dontobfuscate
-dontoptimize

The application works correctly. 
Anyone has done this successfully before? Any ideas? 

Thanks
Inder

inde...@gmail.com

unread,
Jul 19, 2011, 11:47:47 AM7/19/11
to googl...@googlegroups.com
Anyone had better luck with this? 

I tried keeping Gson classes as well as AsyncTask. I even tried keeping all java classes (java.**, javax.**, com.sun.**) but I continue to run into this error. The only way to get rid of the error is to use -dontobfuscate. 

Here is how I turned off obfuscation for the classes:

-keep class com.google.gson.** { *; }

-keepclassmembers class * extends android.os.AsyncTask {
protected void onPreExecute();
protected *** doInBackground(...);
protected void onPostExecute(...);
}
-keep public class * implements java.lang.reflect.Type

-keep class java.**,javax.**,com.sun.**,android.** {
   static final %                *;
   static final java.lang.String *;
  *;
}

lauri.andler

unread,
Jul 19, 2011, 12:06:54 PM7/19/11
to google-gson
I'm having exactly the same problem, I've tried (or seems to me that
I've tried) almost every possible -keep combination and even adding
dummy Class.forName's in the hope that proguard would catch them and
keep all the necessary classes/fields/methods unobfuscated. I'm slowly
running out of options, the next solution that comes to my mind would
be dumping GSON, but I really wouldn't want to do that since it's so
awesome. Anyone?

Thanks
Lauri

On Jul 7, 10:37 pm, "inder...@gmail.com" <inder...@gmail.com> wrote:
> I am trying to configure my Android application to use Proguard. The primary
> reason is to rename the Gson packages to avoid conflicts with an older Gson
> version shipped with the phone platform. Seehttp://code.google.com/p/google-gson/issues/detail?id=255

inde...@gmail.com

unread,
Aug 1, 2011, 12:46:31 AM8/1/11
to googl...@googlegroups.com
Hi Lauri, 

As I mentioned in another thread, I have been able to figure out how to use proguard correctly with Gson. 
I have added an example android application (http://code.google.com/p/google-gson/source/browse/#svn%2Ftrunk%2Fexamples%2Fandroid-proguard-example) that uses the correct proguard settings for Gson. In short, you need to add the following for your app:

# 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.
-keepattributes Signature

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

HTH
Inder

Tony Chan

unread,
Oct 25, 2014, 4:46:44 PM10/25/14
to googl...@googlegroups.com
Why is this line in your configuration commented out:
#-keep class com.google.gson.stream.** { *; }

Was it a mistake? Should we or should we not enable that flag? Thanks.

zhiying hu

unread,
Jul 11, 2019, 1:09:18 AM7/11/19
to google-gson
I'm having the same problem. but it occurs only on Android 4.x devices. I don't want to close proguard config. I tried to use the config below, but didn't work.
# 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.
-keepattributes Signature
-keepattributes EnclosingMethod
-keepattributes InnerClasses
-keepattributes Annotation

# For using GSON @Expose annotation
-keepattributes *Annotation*


# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.** { *; }
-keep class * implements java.lang.reflect.Type

#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { <fields>; }

# 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

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}

I have no idea how to solve the problem without turning off proguard. Any ideas,please?
在 2011年7月8日星期五 UTC+8上午3:37:54,inde...@gmail.com写道:
Reply all
Reply to author
Forward
0 new messages