I am having problems with GSON on Android with Proguard.
APK is compiled, installed on phone.
Application is not crashing, it is just not parsing object correctly.
I log all the data and it is like that:
I get correct string with data to parse.
(CookieValue is correct)
Token token = new Gson().fromJson(cookieValue, Token.class);
After this line, I am logging this object and it is having only null values inside.
My Proguard GSON:
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# For using GSON #Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-dontwarn sun.misc.**
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keep class com.google.gson.examples.android.model.** { *; }
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
-keep public class com.google.gson
Token rules:
-dontwarn com.project.package.model.oauth.**
-keep,allowshrinking class com.project.package.oauth.Token { *; }
Yes, I have tried this configuration https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg
Any suggestions?
In this line:
- keep,allowshrinking class com.project.package.oauth.Token { *; }
Do you actually have a space between the dash and keep? If so, take that out and see if it helps.
You might also want to remove the space after the dash in the previous dontwarn line, as well.
You can find it in my collection :
https://github.com/thientvse/awesome-mobile-collections/blob/master/README.md
It will help you config almost lib in proguard file.
I hope it will help your problem!
Related
I have the following code that works fine before obfuscation
YouTube.LiveBroadcasts.List liveBroadcastsRequest =
mYouTube.liveBroadcasts().list("id,snippet,contentDetails,status");
liveBroadcastsRequest.setMine(true);
liveBroadcastsRequest.setBroadcastType("persistent");
LiveBroadcastListResponse broadcastResponse = liveBroadcastsRequest.execute();
List<LiveBroadcast> broadcasts = broadcastResponse.getItems();
LiveBroadcast broadcast = broadcasts.get(0);
But in release version with obfuscation the last line of the code throws the following exception
java.lang.ClassCastException: com.google.api.client.util.ArrayMap cannot be cast to com.google.api.services.youtube.model.LiveBroadcast
I suppose that the issue happens because my obfuscation somehow breaks gson deserialization in google api, but I cannot find how to modify my proguard rules to make it working.
Current obfuscation rules
-keep class sun.misc.Unsafe { *; }
-dontwarn sun.misc.Unsafe
-keepattributes *Annotation*
-keep class com.google.** { *; }
-keep class com.google.api.services.youtube.model.** { *; }
-dontwarn com.google.**
Any help will be appreciated
Found a correct answer here
I needed to keep the com.google.api.client.util.Key annotation from obfuscation.
-keepclassmembers class * {
#com.google.api.client.util.Key <fields>;
}
-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault
Thanks anyway.
I am using the following Proguard configuration with GSON library:
# 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
# For using GSON #Expose annotation
-keepattributes *Annotation*
# 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.** { *; }
# Keep any class that intereracts with GSON
-keep class com.trifecta.engine.api.** { *; }
But while testing exported apk i am getting the error:
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)
I have researched for the problem here on SO and i followed the Proguard configuration from: [https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg] but still no success. 1
try this snippet and let me know
## GSON 2.2.4 specific rules ##
# 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
# For using GSON #Expose annotation
-keepattributes *Annotation*
-keepattributes EnclosingMethod
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
If I build my app in debug it works perfectly. However, when I build for release with proguard, I get several errors and then the build fails. The error summary:
Warning:there were 445 unresolved references to classes or interfaces.
Warning:there were 13 instances of library classes depending on program classes.
I modified my proguard-rules.txt file as shown below. This allowed the build to succeed, but when I install the app, the amazon downloads do not work (works in the debug version).
-keep class org.xmlpull.v1.**
-keep class org.apache.http.**
-keep class org.ietf.jgss.**
-keep class com.amazonaws.services.**
-keep class org.w3c.dom.bootstrap.**
-dontwarn org.xmlpull.v1.**
-dontwarn org.apache.http.**
-dontwarn org.ietf.jgss.**
-dontwarn com.amazonaws.services.**
-dontwarn org.w3c.dom.bootstrap.**
Any suggestions as to how I can get this fixed please?
Update:
Have narrowed it down to a GSON/JSONPARSER issue. For the release version, JSONPARSER does not convert downloaded json to a supplied class like it does in debug. The GSON proguard rules (see update 2 ) have also been added but the issue persists.
-keep class org.apache.commons.logging.** { *; }
-keep class com.amazonaws.services.sqs.QueueUrlHandler { *; }
-keep class com.amazonaws.javax.xml.transform.sax.* { public *; }
-keep class com.amazonaws.javax.xml.stream.** { *; }
-keep class com.amazonaws.services.**.model.*Exception* { *; }
-keep class com.amazonaws.internal.** { *; }
-keep class org.codehaus.** { *; }
-keep class org.joda.convert.* { *; }
-keepattributes Signature,*Annotation*,EnclosingMethod
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class com.amazonaws.** { *; }
-dontwarn com.amazonaws.auth.policy.conditions.S3ConditionFactory
-dontwarn org.joda.time.**
-dontwarn com.fasterxml.jackson.databind.**
-dontwarn javax.xml.stream.events.**
-dontwarn org.codehaus.jackson.**
-dontwarn org.apache.commons.logging.impl.**
-dontwarn org.apache.http.conn.scheme.**
-dontwarn org.apache.http.annotation.**
-dontwarn org.ietf.jgss.**
-dontwarn org.w3c.dom.bootstrap.**
-dontwarn org.xmlpull.v1.**
-dontwarn com.amazonaws.services.**
-libraryjars libs/aws-android-sdk-1.7.1.1-core.jar
-libraryjars libs/aws-android-sdk-1.7.1.1-s3.jar
-libraryjars libs/gson-2.2.4.jar
-libraryjars libs/ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar
Update 2
# 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
# For using GSON #Expose annotation
-keepattributes *Annotation*
# 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.** { *; }
Finally solved. GSON could not convert my JSON into a class using the fromJson method as proguard had obfuscated my class's fields. I had to add for each of my classes used during json deserialization:
-keepclassmembers class fully.qualified.path.to.class$innerclass {
private <fields>;
}
As per usual, no mention of this in the GSON docs. This was a project I took over - I personally have never used GSON and wont do so in future.
A few notes to those facing similar issues:
Always make sure when you use a library to include all the
recommended (by the library - see their web pages) entries in your
proguard-rules.text file.
They have mentioned in the comments in the second last line of update2.
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
Change the last line to your actual path inside code to classes that will be serialized. Don't copy paste as it is. Example:
-keep class com.myclass.model.** { *; }
That worked for me.
I'm trying to configure Proguard, but I can't manage to get it working.
This is the error:
I've tried things like:
-keep class com.android.auth.TwitterHandle.** { *; }
-keep class oauth.** { *; }
Without any luck.
Anyways, I don't really think ignoring is the answer. Because that might mean something is broken.
Any tips?
Thanks!
The warnings indicate that the AndroidQuery library depends on the OAuth library. Apparently, you're using the former library in your project, but the latter library is missing. You could add the missing library, but if your application is working fine without it in debug mode, you can just tell ProGuard to ignore the missing dependency. In this case:
-dontwarn com.androidquery.auth.**
or, to the same effect:
-dontwarn oauth.signpost.**
See the ProGuard manual > Troubleshooting > Warning: can't find referenced class
(I am the developer of ProGuard)
Add these lines to your proguard file.
-dontwarn oauth.**
-dontwarn com.android.auth.TwitterHandle.**
-keep class oauth.** { *; }
-keep class com.android.auth.TwitterHandle.** { *; }
Edit:
Anyways, I don't really think ignoring is the answer. Because that
might mean something is broken.
If you want to use Proguard and you are having some errors such as class not found, then you must disable/ignore their obfuscation. Because Proguard renames names, fields and methods of classes while obfuscating. This becomes a big problem if reflection is used for that classes. So you have to say proguard to ignore(not obfuscate) some classes to prevent this problem.
Please try this post https://stackoverflow.com/a/15477898/1665964
Insert to skip obfuscated in ProGuard your libs, jars, Classes and Subclasses with this example
-optimizationpasses 5
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-allowaccessmodification
-repackageclasses ''
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.MapActivity
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-libraryjars libs/commons-io-2.2.jar
-libraryjars libs/gson-2.2.2.jar
-keep public class org.apache.commons.io.**
-keep public class com.google.gson.**
-keep public class com.google.gson.** {public private protected *;}
##---------------Begin: proguard configuration for Gson ----------
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.mypackage.ActivityMonitor.ClassMultiPoints.** { *; }
-keep public class com.mypackage.ActivityMonitor$ClassMultiPoints { public protected *; }
-keep public class com.mypackage.ActivityMonitor$ClassMultiPoints$ClassPoints { public protected *; }
-keep public class com.mypackage.ActivityMonitor$ClassMultiPoints$ClassPoints$ClassPoint { public protected *; }
##---------------End: proguard configuration for Gson ----------
The reason for error must be that you might be using an external jar file (in libs folder).
If this is the case, adding the following line before -keep class ... lines should solve your problem.
-libraryjars libs/<jar_filename>
If u are unfamiliar with proguard cmd tool then you can try proguard gui located at tools/proguard/lib/proguardgui of your android SDK folder.
open proguardgui and load your configuration file.
then in Input / output section add your input and output jar as well as add your android lib used in your project.
***** your main problem is that you havent added your library (Aquery.jar and others) jar that is used in your project , so that proguard can't find the required Classes.***
When I use Proguard on project with OrmLite. I recieve this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.name/com.package.name.activities.StartActivity}:
java.lang.IllegalStateException: Could not find OpenHelperClass because none of the generic parameters of class class com.package.name.activities.StartActivity extends OrmLiteSqliteOpenHelper. You should use getHelper(Context, Class) instead.
I've tried all recomendation from Proguard with OrmLite on Android and from others resources but without results
Put the below in both your proguard-project file and your proguard-optimization file (if you use optimization).
# Your application may contain more items that need to be preserved;
# typically classes that are dynamically created using Class.forName:
# ormlite uses reflection
-keep class com.j256.** { *; }
-keep class com.j256.**
-keepclassmembers class com.j256.**
-keep enum com.j256.**
-keepclassmembers enum com.j256.**
-keep interface com.j256.**
-keepclassmembers interface com.j256.**
-keepclassmembers class * {
public <init>(android.content.Context);
}
-keepattributes *Annotation*
and for every model class:
-keep class com.xyz.components.**
-keepclassmembers class com.xyz.components.** { *; }
I don't like the last part one bit, but I'm tired of trying to find a better solution.
I asked much the same question crash using ORMLite on Android with proguard and the answer was to add
-keepattributes Signature
to the proguard configuration.
You can use the following proguard configuration to Keep all model classes that are used by OrmLite
-keep #com.j256.ormlite.table.DatabaseTable class * {
#com.j256.ormlite.field.DatabaseField <fields>;
#com.j256.ormlite.field.ForeignCollectionField <fields>;
# Add the ormlite field annotations that your model uses here
<init>();
}
Just a small addition for the latest version OrmLite 5.
You may want to add these rows to hide some new warnings:
-dontwarn com.j256.ormlite.android.**
-dontwarn com.j256.ormlite.logger.**
-dontwarn com.j256.ormlite.misc.**
Look for more details into this thread: "how can i write the config of proguard for ormlite?"