Gson: java.io.NotSerializableException: com.google.gson.internal.LinkedTreeMap$1

Created on 19 Mar 2015  路  5Comments  路  Source: google/gson

What version of the product are you using? Gson 2.3.1
On what operating system? ArchLinux x86

What steps will reproduce the problem?
1. Create a Parcelable object which contains a Map which is filled by Gson
private Map<String, ?> mMap;

2.Write this map as a Serilizable in writeToParcel (I do this because I need to 
maintain Gson property order. If I do dest.writeMap() I will obtain a HashMap 
when creating from Parcel)
dest.writeSerializable((Serializable) mMap);

3. Obfuscate this with Proguard (not obfuscaed code not crashing).
4. Invoke writeToParcel()

What is the expected output?
No crash

What do you see instead?

java.lang.RuntimeException: Parcelable encountered IOException writing 
serializable object (name = com.google.gson.internal.LinkedTreeMap)
            at android.os.Parcel.writeSerializable(Parcel.java:1388)
            at com.smartatoms.lametric.model.device.DeviceInfoApp.writeToParcel(DeviceInfoApp.java:115)
            at android.os.Parcel.writeParcelable(Parcel.java:1357)
            at android.os.Parcel.writeValue(Parcel.java:1262)
            at android.os.Parcel.writeList(Parcel.java:711)
            at android.os.Parcel.writeValue(Parcel.java:1284)
            at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
            at android.os.Bundle.writeToParcel(Bundle.java:1096)
            at android.os.Parcel.writeBundle(Parcel.java:663)
            at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137)
            at android.os.Parcel.writeTypedArray(Parcel.java:1191)
            at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:381)
            at android.os.Parcel.writeParcelable(Parcel.java:1357)
            at android.os.Parcel.writeValue(Parcel.java:1262)
            at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
            at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
            at android.os.Bundle.writeToParcel(Bundle.java:1096)
            at android.os.Parcel.writeBundle(Parcel.java:663)
            at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2884)
            at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3261)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.io.NotSerializableException: com.google.gson.internal.LinkedTreeMap$1
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
            at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
            at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
            at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
            at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
            at android.os.Parcel.writeSerializable(Parcel.java:1383)
聽聽聽聽聽聽聽聽聽聽聽聽at 
com.smartatoms.lametric.model.device.DeviceInfoApp.writeToParcel(DeviceInfoApp.j
ava:115)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Parcel.writeParcelable(Parcel.java:1357)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Parcel.writeValue(Parcel.java:1262)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Parcel.writeList(Parcel.java:711)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Parcel.writeValue(Parcel.java:1284)
聽聽聽聽聽聽聽聽聽聽聽聽at 
android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
聽聽聽聽聽聽聽聽聽聽聽聽at 
android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Bundle.writeToParcel(Bundle.java:1096)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Parcel.writeBundle(Parcel.java:663)
聽聽聽聽聽聽聽聽聽聽聽聽at 
android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Parcel.writeTypedArray(Parcel.java:1191)
聽聽聽聽聽聽聽聽聽聽聽聽at 
android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:3
81)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Parcel.writeParcelable(Parcel.java:1357)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Parcel.writeValue(Parcel.java:1262)
聽聽聽聽聽聽聽聽聽聽聽聽at 
android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
聽聽聽聽聽聽聽聽聽聽聽聽at 
android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Bundle.writeToParcel(Bundle.java:1096)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Parcel.writeBundle(Parcel.java:663)
聽聽聽聽聽聽聽聽聽聽聽聽at 
android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2884
)
聽聽聽聽聽聽聽聽聽聽聽聽at 
android.app.ActivityThread$StopInfo.run(ActivityThread.java:3261)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Handler.handleCallback(Handler.java:739)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Handler.dispatchMessage(Handler.java:95)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Looper.loop(Looper.java:135)
聽聽聽聽聽聽聽聽聽聽聽聽at 
android.app.ActivityThread.main(ActivityThread.java:5221)
聽聽聽聽聽聽聽聽聽聽聽聽at java.lang.reflect.Method.invoke(Native Method)
聽聽聽聽聽聽聽聽聽聽聽聽at java.lang.reflect.Method.invoke(Method.java:372)
聽聽聽聽聽聽聽聽聽聽聽聽at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
聽聽聽聽聽聽聽聽聽聽聽聽at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


Original issue reported on code.google.com by [email protected] on 23 Jan 2015 at 1:45

Most helpful comment

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

to the proguard-rules.pro fix this issue.

All 5 comments

Hi. Today got this crash. Gson parsed my object like 
ArrayList<LinkedTreeMap<Node<String, String>. This object convert to byte[] 
like this :

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
objOutStream = new ObjectOutputStream(outStream);
objOutStream.writeObject(obj);

So, I got exception :

java.io.NotSerializableException: 
ru.tcsbank.wallet.gson.patched.internal.LinkedTreeMap$1
and could not write serialized object to byte array.

Original comment by [email protected] on 2 Feb 2015 at 4:27

And yes. I dont know about true reason, but if I dont have proguard, this code 
works perfect.

Original comment by [email protected] on 2 Feb 2015 at 4:30

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

to the proguard-rules.pro fix this issue.

I don't know how fix it

Any newcomers to this issue, you only need to add the following to your proguard rules:
-keep class com.google.gson.internal.LinkedTreeMap { *; }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GoogleCodeExporter picture GoogleCodeExporter  路  15Comments

GoogleCodeExporter picture GoogleCodeExporter  路  14Comments

LucianWang picture LucianWang  路  42Comments

GoogleCodeExporter picture GoogleCodeExporter  路  20Comments

JakeWharton picture JakeWharton  路  39Comments