IllegalArgumentException on Gson classes while running Proguard on Android project

2,232 views
Skip to first unread message

Henrique Nunes

unread,
Jul 15, 2011, 1:35:59 PM7/15/11
to googl...@googlegroups.com
Hi all.

I'm trying to run my Android app through proguard and I'm getting this:

[proguard] Note: com.google.gson.UnsafeAllocator accesses a declared field 'theUnsafe' dynamically
[proguard] Note: com.google.gson.UnsafeAllocator accesses a method 'allocateInstance(java.lang.Class)' dynamically
[proguard] Maybe this is library method 'sun.misc.Unsafe { java.lang.Object allocateInstance(java.lang.Class); }'
[proguard] Note: there were 2 accesses to class members by means of introspection.
[proguard]       You should consider explicitly keeping the mentioned class members
[proguard]       (using '-keep' or '-keepclassmembers').
[proguard] Unexpected error while evaluating instruction:
[proguard]   Class       = [com/google/gson/JsonElement]
[proguard]   Method      = [toString()Ljava/lang/String;]
[proguard]   Instruction = [13] invokevirtual #19
[proguard]   Exception   = [java.lang.IllegalArgumentException] (Can't find common super class of [java/lang/Appendable] and [java/lang/StringBuilder])
[proguard] Unexpected error while performing partial evaluation:
[proguard]   Class       = [com/google/gson/JsonElement]
[proguard]   Method      = [toString()Ljava/lang/String;]
[proguard]   Exception   = [java.lang.IllegalArgumentException] (Can't find common super class of [java/lang/Appendable] and [java/lang/StringBuilder])
[proguard] Error: Can't find common super class of [java/lang/Appendable] and [java/lang/StringBuilder]


I'm kinda new to proguard and I can't really tell whether all this is connected, but it seems clear that there is an issue with Gson.
I'm using Gson 1.7.1 from Maven repo.
Thanks very much for any insight anyone can provide!

-Henrique

PS: there's another thread about Gson and Proguard. Perhaps a section about it in the docs would be justified...

inde...@gmail.com

unread,
Jul 22, 2011, 11:31:10 AM7/22/11
to googl...@googlegroups.com
Hi Henrique, 

You are running into this error because Proguard obfuscated sun.misc.Unsafe class which Gson needs to deserialize classes that do not define no-args constructors. 

So, you have two options: 

1. declare -keep sun.misc.Unsafe { *; } in proguard.cfg  or
2. define no-args constructor for all of your classes that will be used with Gson.

We will update Gson user-guide with a section on Proguard and also provide an example Android app.
HTH
Inder

inde...@gmail.com

unread,
Jul 25, 2011, 2:33:21 AM7/25/11
to googl...@googlegroups.com
Hi Henrique, 

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

Diệu Nguyễn Văn

unread,
Sep 9, 2015, 10:53:32 AM9/9/15
to google-gson
Hi Google!.
But I get error
java.lang.AssertionError
       at com.google.gson.internal.bind.TypeAdapters$EnumTypeAdapter.<init>(SourceFile:733)
       at com.google.gson.internal.bind.TypeAdapters$26.create(SourceFile:759)
       at com.google.gson.Gson.getAdapter(SourceFile:359)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(SourceFile:122)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(SourceFile:46)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(SourceFile:92)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(SourceFile:91)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(SourceFile:142)
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(SourceFile:83)
       at com.google.gson.Gson.getAdapter(SourceFile:359)
       at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(SourceFile:52)
       at com.google.gson.Gson.getAdapter(SourceFile:359)
       at com.google.gson.Gson.fromJson(SourceFile:809)
       at com.google.gson.Gson.fromJson(SourceFile:775)
       at com.google.gson.Gson.fromJson(SourceFile:724)
       at td.a(SourceFile:680)
       at td.onResponse(SourceFile:674)
       at com.android.volley.toolbox.StringRequest.deliverResponse(SourceFile:60)
       at com.android.volley.toolbox.StringRequest.deliverResponse(SourceFile:30)
       at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(SourceFile:99)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:211)
       at android.app.ActivityThread.main(ActivityThread.java:5333)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

What is solution?
Thanks
Reply all
Reply to author
Forward
0 new messages