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 'java.net.Socket 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(DefaultClientConnectionOperator.java:165)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)

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(Class.java:453)
    at com.activeandroid.ModelInfo.scanForModelClasses(ModelInfo.java:187)
    at com.activeandroid.ModelInfo.scanForModel(ModelInfo.java:152)
    at com.activeandroid.ModelInfo.<init>(ModelInfo.java:63)
    at com.activeandroid.Cache.initialize(Cache.java:66)
    at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:44)
    at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:34)
    at com.activeandroid.content.ContentProvider.onCreate(ContentProvider.java:39)
    at android.content.ContentProvider.attachInfo(ContentProvider.java:1917)
    at android.content.ContentProvider.attachInfo(ContentProvider.java:1892)
    at android.app.ActivityThread.installProvider(ActivityThread.java:6391)
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:5938)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5853)
    at android.app.ActivityThread.access$1100(ActivityThread.java:199)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

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 "android.support.test.runner.AndroidJUnitRunner" 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 'com.android.tools.build:gradle:1.3.0'} 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.

Comments
  • 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"?