Rectangle 27 67

For the latest version of the recommended proguard configuraiton file, please see the gson supplied android proguard example at: https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg share|improve this answer edited Oct 27 '15 at 9:31 Dmytro Danylyk 14.3k65065 answered Jul 1 '12 at 12:52 Guy 8,74054162
Caused by: java.lang.AbstractMethodError: abstract method "void com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$a.a(com.google.gson.stream.JsonWriter, java.lang.Object)"        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)        at com.google.gson.Gson.toJson(Gson.java:546)        at com.google.gson.Gson.toJson(Gson.java:525)        at com.google.gson.Gson.toJson(Gson.java:480)        at com.google.gson.Gson.toJson(Gson.java:460)

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 67

For the latest version of the recommended proguard configuraiton file, please see the gson supplied android proguard example at: https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg share|improve this answer edited Oct 27 '15 at 9:31 Dmytro Danylyk 14.4k65165 answered Jul 1 '12 at 12:52 Guy 8,75054162
Caused by: java.lang.AbstractMethodError: abstract method "void com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$a.a(com.google.gson.stream.JsonWriter, java.lang.Object)"        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)        at com.google.gson.Gson.toJson(Gson.java:546)        at com.google.gson.Gson.toJson(Gson.java:525)        at com.google.gson.Gson.toJson(Gson.java:480)        at com.google.gson.Gson.toJson(Gson.java:460)

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 67

For the latest version of the recommended proguard configuraiton file, please see the gson supplied android proguard example at: https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg share|improve this answer edited Oct 27 '15 at 9:31 Dmytro Danylyk 14.7k65265 answered Jul 1 '12 at 12:52 Guy 8,88154162
Caused by: java.lang.AbstractMethodError: abstract method "void com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$a.a(com.google.gson.stream.JsonWriter, java.lang.Object)"        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)        at com.google.gson.Gson.toJson(Gson.java:546)        at com.google.gson.Gson.toJson(Gson.java:525)        at com.google.gson.Gson.toJson(Gson.java:480)        at com.google.gson.Gson.toJson(Gson.java:460)

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 25

These settings in the config worked for me in one of my apps:

# Add the gson class
-keep public class com.google.gson

# Add any classes the interact with gson
-keep class com.someapp.android.models.ChatModel { *; }
-keep class com.someapp.android.models.FeedModel { *; }

# Add the path to the jar
-libraryjars /Users/someuser/Documents/workspace/someapp/lib/gson-1.7.1.jar
-keepattributes Signature

@jerry # Add any classes the interact with gson, it works for me but when i applied reverse engg on apk, i got all the model classes with not change in members, i am able to decompile all model, it breaks the security purpose, isn't there any other option ?

@blindstuff You shouldn't add -libraryjars. In this document, proguard.sourceforge.net/index.html#manual/troubleshooting.html, it says "You should never explicitly specify the input jars yourself (with -injars or -libraryjars), since you'll then get duplicate definitions."

I think if someone wants to keep all classes in the Models package he should write: -keep class com.someapp.android.models.** { *; }

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 25

These settings in the config worked for me in one of my apps:

# Add the gson class
-keep public class com.google.gson

# Add any classes the interact with gson
-keep class com.someapp.android.models.ChatModel { *; }
-keep class com.someapp.android.models.FeedModel { *; }

# Add the path to the jar
-libraryjars /Users/someuser/Documents/workspace/someapp/lib/gson-1.7.1.jar
-keepattributes Signature

@jerry # Add any classes the interact with gson, it works for me but when i applied reverse engg on apk, i got all the model classes with not change in members, i am able to decompile all model, it breaks the security purpose, isn't there any other option ?

@blindstuff You shouldn't add -libraryjars. In this document, proguard.sourceforge.net/index.html#manual/troubleshooting.html, it says "You should never explicitly specify the input jars yourself (with -injars or -libraryjars), since you'll then get duplicate definitions."

I think if someone wants to keep all classes in the Models package he should write: -keep class com.someapp.android.models.** { *; }

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 25

These settings in the config worked for me in one of my apps:

# Add the gson class
-keep public class com.google.gson

# Add any classes the interact with gson
-keep class com.someapp.android.models.ChatModel { *; }
-keep class com.someapp.android.models.FeedModel { *; }

# Add the path to the jar
-libraryjars /Users/someuser/Documents/workspace/someapp/lib/gson-1.7.1.jar
-keepattributes Signature

@jerry # Add any classes the interact with gson, it works for me but when i applied reverse engg on apk, i got all the model classes with not change in members, i am able to decompile all model, it breaks the security purpose, isn't there any other option ?

@blindstuff You shouldn't add -libraryjars. In this document, proguard.sourceforge.net/index.html#manual/troubleshooting.html, it says "You should never explicitly specify the input jars yourself (with -injars or -libraryjars), since you'll then get duplicate definitions."

I think if someone wants to keep all classes in the Models package he should write: -keep class com.someapp.android.models.** { *; }

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 14

Applying the changes found in the Android example in the Gson project worked for me

-keepattributes Signature
-keep class sun.misc.Unsafe { *; }
# and keeping the classes that will be serialized/deserialized

The lines you mentioned was not enough for me but i applied the things in the example you provied and worked for me. Thank you.

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 14

Applying the changes found in the Android example in the Gson project worked for me

-keepattributes Signature
-keep class sun.misc.Unsafe { *; }
# and keeping the classes that will be serialized/deserialized

The lines you mentioned was not enough for me but i applied the things in the example you provied and worked for me. Thank you.

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 14

Applying the changes found in the Android example in the Gson project worked for me

-keepattributes Signature
-keep class sun.misc.Unsafe { *; }
# and keeping the classes that will be serialized/deserialized

The lines you mentioned was not enough for me but i applied the things in the example you provied and worked for me. Thank you.

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 6

I know the original question was resolved by taking a different approach, but I was having a very similar issue using flexjson and Proguard on Android, and I've solved it, in case anyone runs into it themselves.

When converting back from JSON to my value object which included some ArrayLists, I would get the same ClassCastException. I got it to work by basically having obfuscation enabled but turning all parts of obfuscation off (-keep everything, -keepclassmembers everything and -keepattributes everything) and then working backwards by enabling things a bit at a time.

The result; keeping the entire flexjson library:

-keep class flexjson**
--keepclassmembers class flexjson** {
   *;
}

and keeping the Signature and Annotation attribute:

-keepattributes Signature, *Annotation*

I was able to use the flexjson library without incident after that in a proguarded, release version of my app.

+1. If using e.g. @SerializedName annotation from Gson in your model classes, -keepattributes *Annotation* is necessary!

SerializeName
-keepattributes *Annotation*
java.lang.AbstractMethodError: abstract method "void com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$a.a(com.google.gson.stream.JsonWriter, java.lang.Object)"        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)        at com.google.gson.Gson.toJson(Gson.java:546

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 6

I know the original question was resolved by taking a different approach, but I was having a very similar issue using flexjson and Proguard on Android, and I've solved it, in case anyone runs into it themselves.

When converting back from JSON to my value object which included some ArrayLists, I would get the same ClassCastException. I got it to work by basically having obfuscation enabled but turning all parts of obfuscation off (-keep everything, -keepclassmembers everything and -keepattributes everything) and then working backwards by enabling things a bit at a time.

The result; keeping the entire flexjson library:

-keep class flexjson**
--keepclassmembers class flexjson** {
   *;
}

and keeping the Signature and Annotation attribute:

-keepattributes Signature, *Annotation*

I was able to use the flexjson library without incident after that in a proguarded, release version of my app.

+1. If using e.g. @SerializedName annotation from Gson in your model classes, -keepattributes *Annotation* is necessary!

SerializeName
-keepattributes *Annotation*
java.lang.AbstractMethodError: abstract method "void com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$a.a(com.google.gson.stream.JsonWriter, java.lang.Object)"        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)        at com.google.gson.Gson.toJson(Gson.java:546

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 6

I know the original question was resolved by taking a different approach, but I was having a very similar issue using flexjson and Proguard on Android, and I've solved it, in case anyone runs into it themselves.

When converting back from JSON to my value object which included some ArrayLists, I would get the same ClassCastException. I got it to work by basically having obfuscation enabled but turning all parts of obfuscation off (-keep everything, -keepclassmembers everything and -keepattributes everything) and then working backwards by enabling things a bit at a time.

The result; keeping the entire flexjson library:

-keep class flexjson**
--keepclassmembers class flexjson** {
   *;
}

and keeping the Signature and Annotation attribute:

-keepattributes Signature, *Annotation*

I was able to use the flexjson library without incident after that in a proguarded, release version of my app.

+1. If using e.g. @SerializedName annotation from Gson in your model classes, -keepattributes *Annotation* is necessary!

SerializeName
-keepattributes *Annotation*
java.lang.AbstractMethodError: abstract method "void com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$a.a(com.google.gson.stream.JsonWriter, java.lang.Object)"        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)        at com.google.gson.Gson.toJson(Gson.java:546

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 2

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

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 2

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

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 2

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

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 1

So, I ended up ditching the Gson library and instead of converting my object to json using gson, I created a custom class in my app to serialize and deserialize the object and store the data that way.

I am overall more happy, even though this has cost me over 12 hours of trying to figure it out. Apparently, PROGUARD and gson must not like each other too much?

As A HUGELY added benefit to not having to use GSON, I noticed that by taking out the GSON library, my app size is cut in half. My app was 577kb and is now only 260kb after removing the gson lib.

gson uses reflection at runtime to dynamically map the json into classes by string matching the properties. This will never work with obfuscated sources, since model.name is replaced with a.b by Proguard. It has less to do with GSON specifically and more to do with any library that uses reflection in this manner. In order to use proguard with GSON, you need to exclude the classes you are serializing / deserializing from obfuscation in the proguard config file.

Welcome to 2014 where 577Kb and 260Kb are equally nothing.

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 1

So, I ended up ditching the Gson library and instead of converting my object to json using gson, I created a custom class in my app to serialize and deserialize the object and store the data that way.

I am overall more happy, even though this has cost me over 12 hours of trying to figure it out. Apparently, PROGUARD and gson must not like each other too much?

As A HUGELY added benefit to not having to use GSON, I noticed that by taking out the GSON library, my app size is cut in half. My app was 577kb and is now only 260kb after removing the gson lib.

gson uses reflection at runtime to dynamically map the json into classes by string matching the properties. This will never work with obfuscated sources, since model.name is replaced with a.b by Proguard. It has less to do with GSON specifically and more to do with any library that uses reflection in this manner. In order to use proguard with GSON, you need to exclude the classes you are serializing / deserializing from obfuscation in the proguard config file.

Welcome to 2014 where 577Kb and 260Kb are equally nothing.

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 1

So, I ended up ditching the Gson library and instead of converting my object to json using gson, I created a custom class in my app to serialize and deserialize the object and store the data that way.

I am overall more happy, even though this has cost me over 12 hours of trying to figure it out. Apparently, PROGUARD and gson must not like each other too much?

As A HUGELY added benefit to not having to use GSON, I noticed that by taking out the GSON library, my app size is cut in half. My app was 577kb and is now only 260kb after removing the gson lib.

gson uses reflection at runtime to dynamically map the json into classes by string matching the properties. This will never work with obfuscated sources, since model.name is replaced with a.b by Proguard. It has less to do with GSON specifically and more to do with any library that uses reflection in this manner. In order to use proguard with GSON, you need to exclude the classes you are serializing / deserializing from obfuscation in the proguard config file.

Welcome to 2014 where 577Kb and 260Kb are equally nothing.

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 1

It does look like your are keeping everything from your class (fields, methods and the class itself). But to make sure you can add -printseeds outputfile.txt to the proguard.cfg file to verify that proguard really keeps everything you need once obfuscation is done.

BTW, you might think about adding something like ACRA or Android Remote stacktrace that allows you to inspect stacktraces on a built app.

Thanks,I checked my seeds file and it has the following: 'org.scanner.scanlog.SingleFrame org.scanner.scanlog.SingleFrame: int Position org.scanner.scanlog.SingleFrame: int Time org.scanner.scanlog.SingleFrame: java.util.Map MainDataMap org.scanner.scanlog.SingleFrame: java.util.Map DataMap org.scanner.scanlog.SingleFrame: SingleFrame(int,int,java.util.Map,java.util.Map)'

Maybe it has something to do with the obfuscation of java.util.Map? Not sure what to do about it though?

i added the stack trace

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard
Rectangle 27 1

It does look like your are keeping everything from your class (fields, methods and the class itself). But to make sure you can add -printseeds outputfile.txt to the proguard.cfg file to verify that proguard really keeps everything you need once obfuscation is done.

BTW, you might think about adding something like ACRA or Android Remote stacktrace that allows you to inspect stacktraces on a built app.

Thanks,I checked my seeds file and it has the following: 'org.scanner.scanlog.SingleFrame org.scanner.scanlog.SingleFrame: int Position org.scanner.scanlog.SingleFrame: int Time org.scanner.scanlog.SingleFrame: java.util.Map MainDataMap org.scanner.scanlog.SingleFrame: java.util.Map DataMap org.scanner.scanlog.SingleFrame: SingleFrame(int,int,java.util.Map,java.util.Map)'

Maybe it has something to do with the obfuscation of java.util.Map? Not sure what to do about it though?

i added the stack trace

ProGuard and Gson on Android (ClassCastException) - Stack Overflow

android gson proguard