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.
Related
I implemented a library on my gradle that uses Moshi as a dependency. When I tried to compile locally and also creating an android build through jenkins, it shows an error of
Use of generics not allowed for java type at '<1>_<2>_<3>JsonAdapter' in line 43 of file '\.gradle\caches\transforms-1\files-1.1\jetified-moshi-1.9.3.jar\76f6a6d7d5642e8be11f9d49dc24d752\META-INF\proguard\moshi.pro'
I already indicated rules on my proguard.pro file as stated on the documentation of the library I used but still errors are still encountering. I also tried the solution on the issues tab of moshi on github but I still received an error build.
Here's my proguard-rules.pro file
#ChatvisorLibrary
-keep class com.chatvisor.** {*;}
-dontwarn com.chatvisor.**
-dontwarn okhttp3.**
#Dependencies
#Protocol Buffers
-keep class com.google.**
-dontwarn com.google.**
#Moshi (https://github.com/square/moshi/blob/master/moshi/src/main/resources/META-INF/proguard/moshi.pro)
# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**
-keepclasseswithmembers class * {
#com.squareup.moshi.* <methods>;
}
-keep #com.squareup.moshi.JsonQualifier interface *
# Enum field names are used by the integrated EnumJsonAdapter.
# values() is synthesized by the Kotlin compiler and is used by EnumJsonAdapter indirectly
# Annotate enums with #JsonClass(generateAdapter = false) to use them with Moshi.
-keepclassmembers #com.squareup.moshi.JsonClass class * extends java.lang.Enum {
<fields>;
**[] values();
}
-ignorewarnings
-keep class * {
public private protected *;
}
I hope someone could help me and get resolved the issue. Thank you!
Already resolved this issue by upgrading my Proguard version
https://www.guardsquare.com/en/blog/proguard-61-released
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 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!
Since I decompiled my app I noticed that all the members, classes are easy to read and understand, so my goal is to make it harder for someone who decompiles the app, to read the code.
The first step I did was to alter my graddle file:
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-myapp.txt'
}
The file proguard-myapp.txt is located in my project.
My project uses the following:
dependencies {
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.google.code.gson:gson:2.4'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.google.android.gms:play-services-ads:9.2.1'
compile 'com.github.lecho:hellocharts-library:1.5.8#aar'
compile('com.crashlytics.sdk.android:crashlytics:2.6.1#aar') {
transitive = true;
}
}
I've kept playing with proguard rules but the more I test the more it gets out of hand.
I've added the following rules:
-dontshrink
-dontoptimize
#charts
-keep class lecho.lib.hellocharts.** { *; }
#support library
-dontwarn android.support.**
-keep class android.support.** { *; }
#ads
-keep public class com.google.android.gms.ads.** {
public *;
}
-keep public class com.google.ads.** {
public *;
}
#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
-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.** { *; }
# 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
#Crashlytics
-keep class com.crashlytics.** { *; }
-dontwarn com.crashlytics.**
-keep class io.fabric.sdk.** { *; }
My questions are:
Is it possible to only obfuscate/optimize or whatever it is called, only the files that I actually use in the app, the ones I wrote and leave all the other libraries as they are? I mean, only rename, optimize my classes and my files and ignore crashlytics, admob, support library?
2.I have tried adding as rules only:
-keep class !com.mypackage.**{*;}
While the code looks fine on decompile, the app does not work properly.
This may not seem as a proper answer but I found out the reason my compiled app did not work.
After a lot of trying to find out what's wrong, I finally got it working.
GSON as it seems in this answer https://stackoverflow.com/a/30982197/379865 needs to have the classes kept in order to properly work.
After keeping my classes for the objects related to Gson, it works.
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.** { *; }