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.**
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
1.without proguard Everything working fine. while enabling the Proguard, all the
header and body models are getting obfuscated.
2.Retrofit2 not able to pass data with model objects.
D/OkHttp: Content-Type: application/json
Content-Length: 80
{"a":{"a":"123","b":"***","c":"","d":"Data","f":"1.0"},"b":{"a":""}}
--> END POST (80-byte body)
below mentioned Proguard rules are already added in my code still getting same problem, kindly help me to fix this issue.
# Retrofit
-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepattributes *Annotation*
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations
-keepattributes EnclosingMethod
-keepclasseswithmembers class * {
#retrofit2.* <methods>;
}
-keepclasseswithmembers interface * {
#retrofit2.* <methods>;
}
# 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
-keepclasseswithmembers class * {
#retrofit2.http.* <methods>;
}
-keepclassmembers class * {
*** get*();
void set*(***);
}
-keepattributes Signature
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
Update:
My Request header and data
RequestPojo requestPojo = null;
try {
requestPojo = new RequestPojo();
RequestHeader requestHeader = new RequestHeader();
requestHeader.setID("123");
requestHeader.setNo("*******");
requestHeader.setName("XYZ");
requestHeader.setCode("ABC");
requestHeader.setAge("1");
/*Request DATA*/
RequestData requestData = new RequestData();
requestData.setData("Hai");
requestPojo.requestHeader = requestHeader;
requestPojo.requestData = requestData;
} catch (Exception e) {
e.printStackTrace();
}
When proguard is enabled then it will obfuscate all the request parameters. So to overcome this issue you have to do the following changes in your Request class:
Suppose this is your request class:
class DemoRequest{
#SerializedName("name")//here this SerializedName will avoid obfuscate of variables
private String name;
.
.
.
}
Hope it will help you.
Thanks
lets say your package name is com.example.android, and you have all your API request model classes in a package named 'requests'
then add this line to the proguard of your app module:
-keep class com.example.android.requests.** { *; }
add this line to all similar packages which might have the request models. So that those classes wont be obfuscated.
Happy coding!
Use following.
# Retrofit
-dontnote retrofit2.Platform # Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor # Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8 # Platform used when running on Java 8 VMs. Will not be used at runtime.
# OkHttp 3
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
Use following.
-keep class retrofit2.** { *; }
-keep class okhttp3.internal.** { *; }
-dontwarn okhttp3.internal.**
-dontwarn retrofit2.**
-keepclasseswithmembers class * {
#retrofit2.http.* <methods>;
}
This will work for proguard.
Rest of the things not needed
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 turned proguard on in my android project and got the warning below. Searched on Google but couldn't find much info.
Warning:retrofit.Platform$Java8: can't find referenced class
org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
I've put the retrofit2 proguard rules in my project.
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclasseswithmembers class * {
#retrofit2.http.* <methods>;
}
Use following proguard rules for Retrofit 2.3.0 and okhttp 3.8.0
-dontnote retrofit2.Platform
-dontwarn retrofit2.Platform$Java8adapters.
-keepattributes Signature
-keepattributes Exceptions
-dontwarn org.xmlpull.v1.**
-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn javax.annotation.**
change it like this:
-dontwarn okio.**
-dontwarn retrofit2.Platform$Java8
Retrofit page has noted about this proguard build,when running on Java 8 VMs:
Why this happens?
Reason is:
Compiler tries to reference Java 8 classes,When u don't mention it through Proguard,After u explicitly mention it,Java 8 classes won't be used.
For example
Package java.nio.* of Java 8 isn't available on Android and will be never called
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.** { *; }