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.** { *; }
Related
I have been researching this for the past few hours without any luck. Class names are not obfuscated no matter what. These are just regular classes, not Activities, Services, or something else which is also in Android Manifest (I know those don't get obfuscated). What am I missing here?
Android Gradle Plugin version: 4.0.0
Gradle version: 6.1.1
Android Studio version: 4.0
With these versions, R8 should be enabled by default. Here is my buildType config:
buildTypes {
release {
//useProguard false // even tried this without luck
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
Here is my proguard-rules.pro
-ignorewarnings
# --- Glide ---
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
# --- Billing library ---
-keep class com.android.vending.billing.**
# --- Retrofit2 ---
# Retrofit does reflection on generic parameters. InnerClasses is required to use Signature and
# EnclosingMethod is required to use InnerClasses.
-keepattributes Signature, InnerClasses, EnclosingMethod
# Retrofit does reflection on method and parameter annotations.
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * {
#retrofit2.http.* <methods>;
}
# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*
# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**
# With R8 full mode, it sees no subtypes of Retrofit interfaces since they are created with a Proxy
# and replaces all potential values with null. Explicitly keeping the interfaces prevents this.
-if interface * { #retrofit2.http.* <methods>; }
-keep,allowobfuscation interface <1>
# --- TwitterKit ---
#Picasso Proguard Config https://github.com/square/picasso
-dontwarn com.squareup.okhttp.**
# --- GSON ---
# 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
-dontwarn sun.misc.**
#-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 TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in #JsonAdapter)
-keep class * implements com.google.gson.TypeAdapter
-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>;
}
# --- SciChart ---
# ignore warnings and save classes required for syntax highlighting
-dontwarn java.awt.**
-dontwarn javax.swing.**
-dontwarn syntaxhighlight.**
-keep public class java.awt.** { *; }
-keep public class javax.swing.** { *; }
-keep public class syntaxhighlight.** { *; }
-keep public class prettify.** { *; }
# need to keep these classes and their methods because they are used by resampling code
-keep public class com.scichart.core.model.DoubleValues { *; }
-keep public class com.scichart.core.model.FloatValues { *; }
-keep public class com.scichart.core.model.IntegerValues { *; }
-keep public class com.scichart.data.model.Point2DSeries { *; }
# repack obfuscated classes into single package so it would be hard to find their originall package
-repackageclasses ''
-allowaccessmodification
Similar questions which I checked but didn't offer any solutions to this:
Android studio 3.4.2 R8 obfuscator does not obfuscate class names, but only java code inside
Class no longer obfuscated after upgrading to Android Gradle plugin 3.4.0
Android/java: Transition / Migration from ProGuard to R8?
As per WorkManager's proguard file, it is expected that all classes that extend ListenableWorker (and its subclasses, such as Worker) are kept. This is because the name of the class is the unique key in WorkManager's internal database.
I have an app that uses retrofit to load data from json file on server. When I run the application on a real device or AVD it loads json to my recyclerview's items and every thing is fine. But when I build an signed APK and then install the app, it doesn't load the json's data like my images urls and ...
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-dontwarn javax.annotation.**
-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn javax.annotation.**
-dontwarn org.conscrypt.**
# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
-dontwarn retrofit2.Platform$Java8
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
# Retrofit
-keep class com.google.gson.** { *; }
-keep public class com.google.gson.** {public private protected *;}
-keep class com.google.inject.** { *; }
-keep class org.apache.http.** { *; }
-keep class org.apache.james.mime4j.** { *; }
-keep class javax.inject.** { *; }
-keep class javax.xml.stream.** { *; }
-keep class retrofit.** { *; }
-keep class com.google.appengine.** { *; }
-keepattributes *Annotation*
-keepattributes Signature
-dontwarn com.squareup.okhttp.*
-dontwarn rx.**
-dontwarn javax.xml.stream.**
-dontwarn com.google.appengine.**
-dontwarn java.nio.file.**
# Retrofit 2.X
## https://square.github.io/retrofit/ ##
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclasseswithmembers class * {
#retrofit2.http.* <methods>;
}
-keep class io.plaidapp.data.api.dribbble.model.** { *; }
-keep public class android.support.design.widget.BottomNavigationView { *; }
-keep public class android.support.design.internal.BottomNavigationMenuView { *; }
-keep public class android.support.design.internal.BottomNavigationPresenter { *; }
-keep public class android.support.design.internal.BottomNavigationItemView { *; }
While using proguard, you need to keep your model classes used for Retrofit.
Use -keepclass and specify all your model/pojo classes.
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclasseswithmembers class * {
#retrofit2.http.* <methods>;
}
When you're generating your signed application, ProGuard is executing its minification rules against your code. You mention you are using Retrofit. Retrofit's documentation includes ProGuard rules that need to be included in your application as well:
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
Additionally, since Retrofit is built on top of Okio, you may need to include Okio proguard rules as well:
-dontwarn okio.**
Set minifyEnabled to false in your build.gradle file.
buildTypes {
release {
minifyEnabled false
I am trying to setup proguard on my app and I'm encountering the following error:
java.lang.NoSuchMethodError: No static method getParameterized(Ljava/lang/reflect/Type;[Ljava/lang/reflect/Type;)Lcom/google/gson/reflect/TypeToken; in class Lcom/google/gson/reflect/TypeToken; or its super classes (declaration of 'com.google.gson.reflect.TypeToken' appears in /data/app/com.rw.crm.leads-1/base.apk)
at com.rw.crm.leads.model.response.GsonAdaptersResponse$ListResponseTypeAdapter.<init>(GsonAdaptersResponse.java:185)
at com.rw.crm.leads.model.response.GsonAdaptersResponse.create(GsonAdaptersResponse.java:36)
at com.google.gson.Gson.getAdapter(Gson.java:423)
I am unable to come up with a proguard configuration that resolves this. This is what I have in the gson proguard file the moment:
# 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
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.rw.crm.leads.model.** { *; }
# 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
# To make the app compile
-dontwarn com.google.gson.reflect.TypeToken
# Why doesn't this work?
-keep class com.google.gson.reflect.** { *; }
Edit: here are the relevant dependencies from build.gradle
compile "com.squareup.retrofit2:retrofit:2.3.0"
compile "com.squareup.retrofit2:converter-gson:2.3.0"
annotationProcessor "org.immutables:value:2.5.6"
provided "org.immutables:gson:2.5.6:annotations"
By including the latest gson release in my gradle, I was able to resolve this error:
compile 'com.google.code.gson:gson:2.8.2'
This will be the fix until whichever library is using the older version (immutables or retrofit2) updates their gson.
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!
I'm using following dependencies of retofit2 for xml and gson parsing.
compile 'com.squareup.retrofit2:converter-simplexml:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
All is working fine and data parsing is correctly in debug and released apk. But when I release apk by using minifyEnabled=true and shrinkResources=true ,then xml parsing is not working any more. Gson was also causing problem and was resolved by applying SerializedName annotation to each attribute of Model because proguard renames attributes while generating apk which causes parsing issue.
How can i resolve the xml parsing issue as well? I want to use minifyEnabled necessarily.
Following commands are from Progurad Rules File.
-keepattributes SourceFile,LineNumberTable
-renamesourcefileattribute SourceFile
-dontwarn org.codehaus.mojo.animal_sniffer.**
-dontwarn java.nio.file.**
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
-dontwarn javax.xml.stream.**
# SimpleXMLParsing
-keep public class org.simpleframework.** { *; }
-keep class org.simpleframework.xml.** { *; }
-keep class org.simpleframework.xml.core.** { *; }
-keep class org.simpleframework.xml.util.** { *; }
-keepattributes ElementList, Root
-keepclassmembers class * {
#org.simpleframework.xml.* *;
}
-dontwarn jp.co.cyberagent.android.gpuimage.**