4

Is there a way to skip proguard from obfuscating the Volley and Gson including the class using them ?

My build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.myawesomeapp"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    compile 'com.google.android.gms:play-services-appindexing:8.1.0'
}

Here the Proguard config:

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/starsilver/Android/Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# 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 *;
#}

-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

-keep class com.android.volley.** { *; }
-keep class org.apache.commons.logging.**

-keepattributes *Annotation*

-dontwarn org.apache.**

-dontwarn com.itextpdf**

Errors returned for Volley:

Warning: com.android.volley.toolbox.Volley: can't find referenced class android.net.http.AndroidHttpClient
Warning: com.android.volley.toolbox.Volley: can't find referenced class android.net.http.AndroidHttpClient

NB:

  • Application work well without proguard.

  • Signed APK generated with proguard but class using Volley + Gson still not working

Jis Maxi
  • 226
  • 1
  • 4
  • 16

2 Answers2

4

You have to modify your proguard.

For gson use this Gson github config:

-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

For volley use this:

-keep class com.android.volley.** { *; }
-keep class org.apache.commons.logging.**

-keepattributes *Annotation*

-dontwarn org.apache.**

Volley seems not working after ProGuard obfuscate

Community
  • 1
  • 1
Stefan
  • 2,098
  • 2
  • 18
  • 29
  • Thank you for the answer, but the class still not working ! Without using Proguard work well, as soon as i use it and obfuscate the app this class do not work. – Jis Maxi Feb 14 '17 at 15:29
  • Could you include your proguard config to the question? – Stefan Feb 14 '17 at 15:29
  • Warnings still the same? – Stefan Feb 14 '17 at 15:36
  • I got only this warning: `Warning:can't write resource [.readme] (Duplicate zip entry [classes.jar:.readme])` – Jis Maxi Feb 14 '17 at 15:40
  • This warning solved with this answer here: http://stackoverflow.com/a/30609641/7232957 but do not affect the class itself, still not working. – Jis Maxi Feb 14 '17 at 15:43
0

Perhaps, you have no problems with Volley, you have problems with GSON, like in my case. Then, use this rules for GSON. Source: https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

# 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 * extends 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>;
}

##---------------End: proguard configuration for Gson  ----------```
Dyno Cris
  • 1,823
  • 1
  • 11
  • 20