9

I use retrofit 2 & using proguard in my project.

My proguard:

-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
   public *;
}    

# Begin twitter 4j.
-dontwarn twitter4j.**
-keep  class twitter4j.conf.PropertyConfigurationFactory
-keep class twitter4j.** { *; }
# End twitter 4j.


# Start androidsocialnetworks
-keep class com.androidsocialnetworks.lib.** { *; }
# End androidsocialnetworks.

# Google api
-keep class com.google.** { *;}
-keep interface com.google.** { *;}
-dontwarn com.google.**

-dontwarn sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-keepattributes *Annotation*,Signature
-keep class * extends com.google.api.client.json.GenericJson {
*;
}
-keep class com.google.api.services.** {
*;
}

-dontwarn com.google.android.gms.cast.**
-dontwarn com.google.android.gms.**
-keep class com.google.android.gms.** { *; }

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}
# End google api

# Other
-optimizations !class/unboxing/enum
-dontwarn com.google.code.**
-dontwarn oauth.signpost.**
-dontwarn javax.management.**
-dontwarn javax.xml.**
#-dontwarn org.apache.**
#-dontwarn org.slf4j.**
-keep class org.** { *; }
-keep class javax.** { *; }
-assumenosideeffects class * implements org.slf4j.Logger {
    public *** trace(...);
    public *** debug(...);
    public *** info(...);
    public *** warn(...);
    public *** error(...);
}

# Rx java
-dontwarn sun.misc.**
-keep class rx.internal.util.unsafe.** { *; }

-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
   long producerIndex;
   long consumerIndex;
}

-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode producerNode;
}

-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode consumerNode;
}

# End other


# Java mail
-dontwarn java.awt.**
-dontwarn java.beans.Beans
-dontwarn javax.security.**

-keep class javamail.** {*;}
-keep class javax.mail.** {*;}
-keep class javax.activation.** {*;}

-keep class com.sun.mail.dsn.** {*;}
-keep class com.sun.mail.handlers.** {*;}
-keep class com.sun.mail.smtp.** {*;}
-keep class com.sun.mail.util.** {*;}
-keep class mailcap.** {*;}
-keep class mimetypes.** {*;}
-keep class myjava.awt.datatransfer.** {*;}
-keep class org.apache.harmony.awt.** {*;}
-keep class org.apache.harmony.misc.** {*;}
# End java mail

And in build.gradle I also using some proguard file: (I get from: https://github.com/krschultz/android-proguard-snippets)

        proguardFile 'proguards/proguard-support-v7-appcompat.pro'
        proguardFile 'proguards/proguard-google-play-services.pro'
        proguardFile 'proguards/proguard-gson.pro'
        proguardFile 'proguards/proguard-butterknife-7.pro'
        proguardFile 'proguards/proguard-square-retrofit2.pro'
        proguardFile 'proguards/proguard-rx-java.pro'
        proguardFile 'proguards/proguard-rxjava-promises.pro'
        proguardFile 'proguards/proguard-square-okhttp3.pro'
        proguardFile 'proguards/proguard-jsoup.pro'
        proguardFile 'proguards/proguard-jackson-2.pro'
        proguardFile 'proguards/proguard-simple-xml.pro'
        proguardFile 'proguards/proguard-support-design.pro'
        proguardFile 'proguards/proguard-square-okio.pro'
        proguardFile 'proguards/proguard-facebook.pro'

Here is a method define API:

@GET("app/authenticate")
    Observable<AuthenticationModel> authenticate(
            @Query("email") String email,
            @Query("password") String password
    );

Then after proguard I decompile APK file then I got:

@GET("app/authenticate")
    rx.a<AuthenticationModel> a();

I dont know why all parameter is removed. (Notes: Current project working as well as if not using proguard) Please help !

UPDATE

The main problem: I used proguard-android-optimize So I should added (as @EpicPandaForce answer):

-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}
quangson91
  • 1,044
  • 1
  • 16
  • 30

2 Answers2

18

Try the following rule

-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}

Might need to adjust the package with Retrofit2.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • Hi, You mean: @retrofit2.http.* ; – quangson91 Feb 27 '16 at 17:38
  • Because retrofit2 changed package name – quangson91 Feb 27 '16 at 17:39
  • Please update your answer. Should change to `@retrofit2.` It will working. I think Retrofit2 Homepage should update too @@ – quangson91 Feb 27 '16 at 17:45
  • 1
    @EpicPandaForce Thanks you made my day, It would have been nice if square team had mentioned this. :) – Bikash Sep 23 '16 at 12:22
  • Didn't work for me...still got the same error after trying this and all the other rules i could possibly find. Linked the rules since it's too long for the comment. [link](https://docs.google.com/document/d/1wXa0Y-IWQ34nO7E4DdqFDj9_hXMXkBDXFRcPV9u0Z4c/edit?usp=sharing) – stewie Oct 30 '16 at 14:36
  • @Benni Are you also protecting your classes you are sending through as a parameter? – EpicPandaForce Oct 30 '16 at 17:56
  • @EpicPandaForce `@POST("auth/login") Observable login(@Body LoginForm loginForm); ` You mean LoginForm in this case? Protect = keep? – stewie Nov 10 '16 at 23:02
  • yup, that's what I mean – EpicPandaForce Nov 11 '16 at 06:25
  • Actually i forgot keeping my whole package^^ Thanks for the hint :) – stewie Nov 24 '16 at 19:24
  • Unless I'm mistaken, this let proguard not to obfuscate your APIs, which makes your app vulnerable for a man-in-the-middle attack. I'm looking for a solution which will allow me to obfuscate my APIs, anyone? – Alessio Jul 06 '17 at 08:05
  • @Alessio proguard doesn't obfuscate string literals either way. – EpicPandaForce Jul 06 '17 at 08:14
  • thanks @EpicPandaForce to clarify! So, any idea how would I approach the problem that my paths are visible even after proguard obfuscation? Any hint, suggestion, link or comment is welcome :) – Alessio Jul 06 '17 at 10:26
7

from retrofit documentation we should add lines listed below to the proguard file:

#Retrofit
# 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

but as my experience we should add these lines too

-keep class retrofit.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}
-keepclasseswithmembers interface * {
    @retrofit2.http.* <methods>;
}

also we need to keep Model classes used by retrofit:

-keep public class your.package.to.models.** {*;}
Dmila Ram
  • 1,054
  • 1
  • 13
  • 19