0

I am using Volley and Gson in my project. The project is a Lib project. When I use my Lib in a demo project, the Volley always throw error response with message: "org.json.JSONException: End of input at character 0" and never has success response. I guess it is caused by ProGuard. The weird thing is when I set "minifyEnabled" in either Lib or the Demo project, everything works good. But when I set "minifyEnabled" in both Lib and Demo, volley will always fail. Does anyone now why this happen?

Sorry for missing the content...

this is the error volley throw:

org.json.JSONException: End of input at character 0

and this is my ProGuard for volley and Gson:

## ----------------------------------
##     Gson
## ----------------------------------
-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.** { *; }
-keep class com.google.gson.stream.** { *; }


## ----------------------------------
##     Glide
## ----------------------------------
-keep class com.bumptech.glide.** {*;}

## -------------------------------------------
##     volley
## -------------------------------------------
-keepclassmembers,allowshrinking,allowobfuscation class com.android.volley.NetworkDispatcher {
    void processRequest();
}
-keepclassmembers,allowshrinking,allowobfuscation class com.android.volley.CacheDispatcher {
    void processRequest();
}
-keep class com.android.volley.** {*;}
-keep class com.android.volley.toolbox.Volley
-keepclassmembers class com.android.volley.toolbox.Volley { *; }
-keep class com.android.volley.toolbox.** {*;}
-keep class com.android.volley.Response$* { *; }
-keep class com.android.volley.Request$* { *; }
-keep class com.android.volley.RequestQueue$* { *; }
-keep class com.android.volley.toolbox.HurlStack$* { *; }
-keep class com.android.volley.toolbox.ImageLoader$* { *; }
-keep class org.apache.http.** {*;}

The important thing is this error only happened if both Lib and Demo are obfuscated at the same time. Obfuscate with either Lib or Demo would not have this issue.

Anyone help? I am 100% sure I keep all my model classes in the ProGuard file.

2019-03-18 14:38:15.635 11276-11276 W/System.err: com.a.a.m: org.json.JSONException: End of input at character 0 of 
2019-03-18 14:38:15.635 11276-11276 W/System.err:     at com.a.a.a.k.a(Unknown Source:32)

While the com.a.a.a.k is "com.android.volley.toolbox.JsonObjectRequest"
Meng Tim
  • 408
  • 1
  • 6
  • 19

3 Answers3

0

put this rules in proguard,

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

-keepattributes *Annotation*

-dontwarn org.apache.**
Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25
  • I tried this... But it doesn't work. I post my ProGuard rules above. The weird thing is only when I minify both project then this issue comes. If I just minify one of them, it works perfectly. For example, if I do not minify the Lib project but just minify the demo app, everything works. – Meng Tim Mar 18 '19 at 06:01
0

Add following lines in your proguard-rules.pro, which you can find under Gradle Script

-keepclassmembers,allowshrinking,allowobfuscation class com.android.volley.NetworkDispatcher {
    void processRequest();
}
-keepclassmembers,allowshrinking,allowobfuscation class com.android.volley.CacheDispatcher {
    void processRequest();
}

You can also refer this link

Update

Then you should add these rules in your proguard-rules.pro of your library project. If you see that iamge near proguard-rules.pro it is written that this file is for app module, there should be an another proguard-rules.pro for your library module add rule there too. enter image description here

Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
  • I tried this... But it doesn't work. I post my ProGuard rules above. The weird thing is only when I minify both project then this issue comes. If I just minify one of them, it works perfectly. For example, if I do not minify the Lib project but just minify the demo app, everything works. – Meng Tim Mar 18 '19 at 06:01
  • in which library project you are enabling minifyEnabled? – Suraj Vaishnav Mar 18 '19 at 06:03
  • That is my own Lib. I am using a Lib project in my demo app project. The lib project has volley and Gson used inside. Once I minify both demo app and my lib, volley does not work correctly. If I just minify one of them, then it works good. Weird – Meng Tim Mar 18 '19 at 06:08
  • I did in both file. But still has this issue. The error happened on this class:com.android.volley.toolbox.JsonObjectRequest The exception is"org.json.JSONException: End of input at character 0" – Meng Tim Mar 18 '19 at 06:39
  • Then check these links: https://stackoverflow.com/questions/32104423/volley-jsonexception-end-of-input-at-character-0-of https://stackoverflow.com/questions/34434596/volley-parse-error-org-json-jsonexception-end-of-input-at-character-0-of – Suraj Vaishnav Mar 18 '19 at 06:44
0

Try using following configuration in your build.gradle.

android {
...
   useLibrary 'org.apache.http.legacy'
}

I had similar issue with volley but different error, above configuration solved my issue. The root cause was Gradle plugin does not include all optional libraries by default anymore.

Ashwin
  • 118
  • 1
  • 8