Retrofit warning with Proguard - android

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

Related

How to Avoid ProGuard obfuscation in Request Header Model in Retrofit 2 Android?

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

Android signed APK loading json data using retrofit

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

Cannot use Picasso and retrofit together

I'm using Picasso and Retrofit together in my app. Everything works perfectly in debug version, but whenever I try to generate a signed apk I get something like this Error:Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'
I searched for a while and tried using -dontwarn retrofit.* in proguard-rules.pro
This isn't working
Picasso proguard configuration
-dontwarn com.squareup.okhttp.**
Retrofit proguard configuration
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclasseswithmembers class * {
#retrofit2.http.* <methods>;
}

In Android Retrofit2 SimpleXml converter not working with minifyEnabled=true

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.**

Method must be overridden in [proguard.optimize.peephole.ClassMerger] if ever called” in Android Studio

Seen at least 2 similar questions, but both considering eclipse non gradle builds.
I'm trying to assemble release with gradle using:
./gradlew myapp:assembleRelease --stacktrace
Besides newest Google proguard example my proguard.txt contains:
# ButterKnife
-keep class *$$ViewInjector{}
-dontwarn butterknife.Views$InjectViewProcessor
-dontwarn butterknife.internal.**
#JodaTime
-dontwarn org.joda.time.**
#Apache
-dontnote org.apache.**
-dontwarn org.apache.**
This seemed to remove all the warnings but now struggling with mysterious
Caused by: java.lang.UnsupportedOperationException: Method must be overridden in [proguard.optimize.peephole.ClassMerger] if ever called
...and I have no clue what it means.
I had the same issue and never discovered the root of the issue. I did find that if you add a rule to skip optimization in your proguard file it stops the build error from happening.
-dontoptimize
Here is a correct proguard config for those 3 libraries:
# ButterKnife
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }
-keepnames class * { #butterknife.InjectView *;}
#JodaTime
-dontwarn org.joda.time.**
#Apache
-keep public class org.apache.commons.io.**
-keep class org.apache.** { *; }
-dontnote org.apache.**
-dontwarn org.apache.**
Not sure if it was a gradle update, but that doesn't happen anymore
-dontoptimize specifies not to optimize the input class files, I am
not sure if it is a good idea to disable optimization, better will be
to enable or disable individual optimizations.
In your case you can use -optimizations optimization_filter
-optimizations !class/merging/vertical*,!class/merging/horizontal*
Proguard will perform all optimizations, except the ones that merges classes vertically and merges classes horizontally.
more info here

Categories

Resources