The build cannot find the org.apache.http classes in an Android project
error: package org.apache.http does not existShort answer
Android removed the Apache HTTP client from the SDK in version 6.0, so code that imports org.apache.http no longer compiles by default. Add useLibrary 'org.apache.http.legacy' to the android block, and for Android 9 and newer also declare the library in the manifest. The longer fix is to move to HttpURLConnection.
Fix it fast
1Find out which cause you have
List the files that import the Apache classes. Files in your own source folders mean the fix is in your build and manifest. A match only in a library's name means that library needs the same setup.
grep -rn "org.apache.http" app/src --include=*.java --include=*.kt2Match what you see, then jump to its fix
- 1
error: package org.apache.http does not exist, pointing at an import line in your own codeThe legacy library is not on the compile classpathFix - 2
The build passes after cause 1, then the classes are missing when the app runs on an Android 9 or newer deviceIt compiles, but the app crashes on Android 9 or newerFix - 3
You own the code that uses HttpClient, HttpGet or HttpPostThe code should leave the Apache client behindFix
- 1
What does the error look like?
error: package org.apache.http does not existerror: package org.apache.http.client does not exist
Cause 1 of 3
The legacy library is not on the compile classpath
1Check
Android 6.0 removed the Apache HTTP client. To keep using those APIs you must declare a compile-time dependency in the module's build file. Check that the android block does not already have it.
grep -n "useLibrary" app/build.gradle app/build.gradle.kts2Fix
Add the useLibrary line to the android block of the app module.
// app/build.gradle
android {
useLibrary 'org.apache.http.legacy'
}3Verify
Build the module again. The import errors should be gone.
./gradlew :app:compileDebugJavaWithJavacConfirmed by developer.android.com
Cause 2 of 3
It compiles, but the app crashes on Android 9 or newer
1Check
From Android 9 the library is removed from the bootclasspath and is not available to apps by default. Check the manifest for a uses-library entry.
grep -n "org.apache.http.legacy" app/src/main/AndroidManifest.xml2Fix
Inside the application element of AndroidManifest.xml, declare the library. Keep android:required set to false when minSdk is 23 or lower, because the library does not exist as a separate library on devices below API level 24.
<application ...>
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
</application>3Verify
Rebuild, install on an Android 9 or newer device and run the code path that makes the request.
./gradlew :app:installDebugConfirmed by developer.android.com
Cause 3 of 3
The code should leave the Apache client behind
1Check
Google's guidance for apps that target API level 9 or higher is to use HttpURLConnection instead of the Apache client. Find the places that use it.
grep -rln "HttpClient\|HttpGet\|HttpPost" app/src2Fix
Rewrite those calls with HttpURLConnection and remove the useLibrary line and the manifest entry. If you must keep the Apache classes, the other documented route is to bundle your own copy of org.apache.http in the APK, repackaged (with a tool such as Jar Jar) so it does not clash with the classes in the runtime.
URL url = new URL("https://example.com/api");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
int status = conn.getResponseCode();
} finally {
conn.disconnect();
}3Verify
Build, then run the request on a device and check the response code.
./gradlew :app:assembleDebugConfirmed by developer.android.com
How do I stop it coming back?
- Treat org.apache.http imports in new code as a warning sign and use HttpURLConnection or a maintained HTTP library instead.
- When you add an old library, search its sources for org.apache.http before you commit to it.
- Test on an Android 9 or newer device as well as an older one when the legacy library is in use.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Mobile apps
Android app crashes because the Apache HTTP classes are missing
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.
2 causes, each with a fixMobile apps
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
The phone shows as unauthorized and Android Studio will not run the app
adb devices lists the phone but Android Studio cannot deploy to it
An unauthorized phone means it has not trusted this computer's RSA key.
3 causes, each with a fixMobile apps
Android Studio cannot pair the phone over Wi-Fi
The Pair devices over Wi-Fi dialog lists no device
Wireless pairing needs the phone on Android 11 or higher and on the same Wi-Fi network as the computer, with current Platform-Tools.
4 causes, each with a fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fix