Using org.apache.http.legacy in Android 9

<uses-library android:name="org.apache.http.legacy" android:required="false"/>
android api level 28
android 9 (api level)
api not working in android pie
android p changes for developers
android 9 migration guide
android 9 location permission
targetsdkversion 28

I have an old android app that uses the apache legacy Http library (org.apache.http.legacy). After the changes on Android 6, I was able to keep the app working including the legacy library.

useLibrary 'org.apache.http.legacy'.

Now in Android 9 devices I get the below exceptions at runtime.

Caused by: java.lang.IncompatibleClassChangeError: Class 'org.apache.http.conn.ssl.SSLSocketFactory' does not implement interface 'org.apache.http.conn.scheme.SchemeSocketFactory' in call to ' org.apache.http.conn.scheme.SchemeSocketFactory.createSocket(org.apache.http.params.HttpParams)' (declaration of 'org.apache.http.impl.conn.DefaultClientConnectionOperator' appears in /data/app/com.theprojectfactory.wmp-RdZODaT5h-l1diSw3oNCvA==/base.apk)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(
    at org.apache.http.impl.client.DefaultRequestDirector.execute(
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(
    at org.apache.http.impl.client.CloseableHttpClient.execute(
    at org.apache.http.impl.client.CloseableHttpClient.execute(
    at org.apache.http.impl.client.CloseableHttpClient.execute(

Reading the Android 9 changes docs, there is a mention to add <uses-library android:name="org.apache.http.legacy" android:required="false"/> in the AndroidManifes.xml file, however that does not seem to work for me either as I get the below exception instead when launching the app.

java.lang.VerifyError: Superclass org.apache.http.params.BasicHttpParams of org.apache.http.params.SyncBasicHttpParams is declared final (declaration of 'org.apache.http.params.SyncBasicHttpParams' appears in /data/app/com.theprojectfactory.wmp-R2P9RNgoJ82ZupwxVa7iNA==/base.apk)
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(
    at com.activeandroid.ModelInfo.scanForModelClasses(
    at com.activeandroid.ModelInfo.scanForModel(
    at com.activeandroid.ModelInfo.<init>(
    at com.activeandroid.Cache.initialize(
    at com.activeandroid.ActiveAndroid.initialize(
    at com.activeandroid.ActiveAndroid.initialize(
    at com.activeandroid.content.ContentProvider.onCreate(
    at android.content.ContentProvider.attachInfo(
    at android.content.ContentProvider.attachInfo(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)

Any pointers on how to keep using the org.apache.http.legacy on Android 9 api will be much appreciated.

You have to add attribute of android:usesCleartextTraffic="true" on application tag in AndroidManifes.xml file.

And also add this.

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

Using org.apache.http.legacy in Android 9, To run org.apache.http.legacy perfectely in Android 9.0 Pie create an xml file res/​xml/network_security_config.xml <?xml version="1.0"  Add Org.Apache.Http.Legacy in Android Studio Admin December 31, 2016 December 31, 2016 Android Apache library is specially used to add HTTP ( Hyper Text Transfer Protocol ) into android applications.

So after further troubleshooting found out that issue was due to a third party library (activeandroid). Library was using reflection to check all classes available in the classpath and for some reason the legacy apache HTTP classes conflicted when calling java.lang.Class.classForName. Skipped the reflection code and app worked as expected. On versions below Android 9 this is not an issue though.

So correct way is as per documentation using: <uses-library android:name="org.apache.http.legacy" android:required="false"/>. If you come across random exceptions check you are not using reflection to try to traverse the legacy HTTP classes.

org.apache.http.legacy doesn't work on API 28, RuntimeException: Stub!. “[Android 9.0] Crash with org.apache.http. android:​name="org.apache.http.legacy" android:required="false"/> Make sure that you are using a fairly recent Gradle for Android plugin. My guess is that this is really new, meaning you would need something like 1.3.0-rc2.You might also consider using Apache's own Android-compatible edition of HttpClient.

Your classes seem to be conflicting.

As an alternative to using the runtime Apache library, apps can bundle their own version of the org.apache.http library in their APK. If you do this, you must repackage the library (with a utility like Jar Jar) to avoid class compatibility issues with the classes provided in the runtime.

See here

[Android 9.0] Crash with org.apache.http. - CodeSpace69, I'v tried to uselibrary 'org.apache.http.legacy' in the root build.gradle. but it doesn't work. Finally Fix bug on API level 28 (Android 9.0) #299. android { compileSdkVersion 28 defaultConfig { applicationId "your application id" minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "" useLibrary 'org.apache.http.legacy' }

App Crash on Android P or Android 6.0 above due to lack of org , Adding the org.apache.http.legacy package from the android-sdk platforms android-23 Apps that target Android 9 or higher and use foreground services must  Google allows you to still use this API, just not as a built in one, by adding this line to the gradle file: useLibrary 'org.apache.http.legacy' So, this is what I did: dependencies {classpath ''} And:

Org Apache Http Legacy Jar Download For Android - digitree, Developing Android 9 Apps Using Android Studio 3.3, Java and Android Jetpack Neil Smyth name="org.apache.http.legacy" android:required="false" /> . Beginning with Android 9, that library is removed from the bootclasspath and is not available to apps by default. To continue using the Apache HTTP client, apps that target Android 9 and above can add the following to their AndroidManifest.xml: <uses-library android:name="org.apache.http.legacy" android:required="false"/>

Android Studio 3.3 Development Essentials, Legacy. Commons HttpClient Google Android 1.0 was released with a pre-​BETA snapshot of Apache HttpClient. To coincide with the first Android release Apache HttpClient 4.0 APIs had to be frozen One can increase compatibility of with the stock version of HttpClient by avoiding 'org.apache.http.**. Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption.

  • Thanks @Kaushal, but this does't work either. Just checked docs and seems usesCleartextTraffic="true" is the default value anyways.
  • Thank you, your solution helped me a lot!
  • I use "org.apache.httpcomponents:httpclient:4.3.6" on Android 9 and your response help me. Nice. But how you understand that problem is in "org.apache.http.legacy"?