Resolve! java.lang.ClassNotFoundException: Didn’t find class “org.apache.http.ProtocolVersion” Volley Issue with targetSdkVersion 26 to 28

A few days back I was trying to upload the latest version on my Android application to Playstore when I faced this upload error:

Saying: Your app currently targets API level 26 and must target at least API level 28 to ensure it is built on the latest APIs optimized for security and performance.

Ok so not a big deal, Just change the number and we are done!

But wait, the change in compileSdkVersion and targetSdkVersion from 26 to 28 in build:gradle(:app)  caused a new problem when making HTTP calls using Volley library(v1.0.17 or v1.1.0) in the application.

When making the HTTP Get a call in a real device it was shown below error and crashing the app.

E/UncaughtException: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:108)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/data/app/com.app.hellow-world-o-Ow9VTBTmvZcRR1gasSkg==/base.apk"],nativeLibraryDirectories=[/data/app/com.app.hellow-world-o-Ow9VTBTmvZcRR1gasSkg==/lib/arm64, /data/app/com.app.hellow-world-o-Ow9VTBTmvZcRR1gasSkg==/base.apk!/lib/arm64-v8a, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:171)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:108)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)

So after spending some hours of debugging and looking for online help, I found a solution and also got to know about the issue with this.

What caused this issue?

As explained in Google forums here:

Specify requirement for Apache HTTP Legacy library

If you are using com.google.android.gms:play-services-maps:16.0.0 or below and your app is targeting API level 28 (Android 9.0) or above, you must include the following declaration within the <application> element of AndroidManifest.xml.

The solution to this problem

As explained here jus add the following line

<uses-library android:name="org.apache.http.legacy" android:required="false" />

In the AndroidManifest.xml file under <application> as shown below:

    <application
		...
		...
        android:theme="@style/AppTheme">
        <uses-library android:name="org.apache.http.legacy" android:required="false"/>
		....
		....
    </application>

After adding the above change, the application was uploaded successfully in play store

Hope this will save your time in a similar situation 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *