Android Question

Android Volley always fails with Proguard

The goal is to deploy an application with

applied. Usual builds without
work fine. But when
is switched to true, everything compiles too, but all
requests fail with error callback (
) regardless on successful result.

Minification config in

buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), ''
debug {
debuggable true

##---------------Begin: proguard configuration for 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** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements
-keep class * implements
-keep class * implements

##---------------End: proguard configuration for Gson ----------

##---------------Begin: proguard configuration for Spongy Castle ----------

-keep class org.spongycastle.crypto.* {*;}
-keep class org.spongycastle.crypto.digests.* {*;}
-keep class org.spongycastle.crypto.encodings.* {*;}
-keep class org.spongycastle.crypto.engines.* {*;}
-keep class org.spongycastle.crypto.macs.* {*;}
-keep class org.spongycastle.crypto.modes.* {*;}
-keep class org.spongycastle.crypto.paddings.* {*;}
-keep class org.spongycastle.crypto.params.* {*;}
-keep class org.spongycastle.crypto.prng.* {*;}
-keep class org.spongycastle.crypto.signers.* {*;}

-keep class org.spongycastle.jcajce.provider.digest.** {*;}
-keep class org.spongycastle.jcajce.provider.keystore.** {*;}
-keep class org.spongycastle.jcajce.provider.symmetric.** {*;}
-keep class org.spongycastle.jcajce.spec.* {*;}
-keep class org.spongycastle.jce.** {*;}

-dontwarn javax.naming.**

##---------------End: proguard configuration for Spongy Castle ----------

# Configuration for Guava 18.0
# disagrees with instructions provided by Guava project:

-keep class {
public static <methods>;
-keep class {
public static ** reverse(**);
-keep class {
public static <fields>;

-keep class {
public static on(java.lang.String);
public ** join(...);

-keep class$ReferenceEntry
-keep class$ReferenceEntry

-dontwarn javax.annotation.**
-dontwarn javax.inject.**
-dontwarn sun.misc.Unsafe

# Guava 19.0
-dontwarn java.lang.ClassValue
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

# Security classes for keystore support
-dontwarn java.awt.**,**, java.beans.**

# Volley
-keep class** { *; }
-keep class** { *; }
-keep class$* { *; }
-keep class$* { *; }
-keep class$* { *; }
-keep class$* { *; }
-keep class$* { *; }
-keep interface** { *; }
-keep class org.apache.commons.logging.*

All used dependencies:

compile ''
compile ''

compile ''
compile ''
compile ''
compile ''

compile 'com.madgag.spongycastle:core:'
compile 'com.madgag.spongycastle:prov:'
compile 'com.madgag.spongycastle:pkix:'
compile 'com.madgag.spongycastle:pg:'

And in addition to Volley failures,
doesn't work correctly too (subscribe events are not fetching). Do we have any solutions for these troubles? Should I add any additional information here?

Answer Source

Should admit, that even with all information provided, my question was very difficult to analyze, because of many possible sources of described errors.

I'll begin from the end of my question. Guava didn't work correctly, because ProGuard just excluded Guava's Subscribe-methods from my code to be packed. ProGuard removes unused code, and as far as Subscribe-methods are analyzed as unused (even IDE don't highlight them as used ones) ProGuard has decided to remove these methods. To solve this issue, we should keep Subscribe-methods from ProGuard's processing:

# Keep subscribe-methods from deletion
-keepclassmembers class ** { <methods>;

And my first problem - when Volley always calls onErrorResponse callbacks in all requests being fired. I used a custom deserializer for Json-repsonses which also checks, if server has provided some required fields (marked with a corresponding annotation). And, of course, ProGuard by default could not work correctly with these annotations and deserializer - that's why I had to keep these entities too:

# To make right deserialization
-keepclassmembers class ** {
  @com.some.package.server.JsonDeserializerWithOptions$FieldRequired public *;
-keep @interface com.some.package.server.JsonDeserializerWithOptions$FieldRequired
-keep class com.some.package.server.JsonDeserializerWithOptions
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download