HttpClient and org.apache.http classes will not import in Android Studio
Cannot resolve symbol 'HttpClient'Short answer
Android 6.0 (API 23) removed the Apache HTTP client from the platform, so a project compiled against API 23 or newer cannot see org.apache.http classes. Either keep the old code alive by declaring useLibrary 'org.apache.http.legacy' in the android block, or move the calls to HttpURLConnection.
Fix it fast
1Find out which cause you have
Check which API level the module compiles against. The Apache client is gone from the platform jar from API 23 on, so any compileSdk of 23 or higher shows this.
grep -n "compileSdk" app/build.gradle app/build.gradle.kts 2>/dev/null2Match what you see, then jump to its fix
- 1
Imports of org.apache.http.* are red, or compile fails with 'package org.apache.http... does not exist'The module compiles against API 23 or newer and the legacy library is not declaredFix - 2
Your own code calls DefaultHttpClient, HttpGet or HttpPostThe networking code should have moved to HttpURLConnection long agoFix
- 1
What does the error look like?
Cannot resolve symbol 'HttpClient'error: package org.apache.http.client does not existUnresolved reference: HttpClient
Cause 1 of 2
The module compiles against API 23 or newer and the legacy library is not declared
1Check
Android 6.0 removed support for the Apache HTTP client. Search the module for imports from that package to see how much code still uses it.
grep -rn "org.apache.http" app/src --include=*.java --include=*.kt2Fix
If you need the old classes for now, declare the legacy library inside the android block of the module's build file, then sync Gradle.
android {
useLibrary 'org.apache.http.legacy'
}3Verify
Rebuild the module. The imports should resolve and compilation should pass.
./gradlew :app:compileDebugJavaWithJavacConfirmed by developer.android.com
Cause 2 of 2
The networking code should have moved to HttpURLConnection long ago
1Check
List the classes your code uses from the removed client. Each request using them has to be rewritten.
grep -rnE "DefaultHttpClient|HttpGet|HttpPost|HttpResponse" app/src2Fix
Replace those calls with java.net.HttpURLConnection. Android's own notes say it reduces network use through transparent compression and response caching, and uses less power. Then remove the useLibrary line again.
URL url = new URL("https://example.com/api");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
int status = conn.getResponseCode();
InputStream in = new BufferedInputStream(conn.getInputStream());
// read the stream here
} finally {
conn.disconnect();
}3Verify
Run the request on a device or emulator and confirm the status code and body.
./gradlew :app:installDebugConfirmed by developer.android.com
How do I stop it coming back?
- Do not add useLibrary 'org.apache.http.legacy' as a permanent answer; treat it as a bridge while you rewrite the calls.
- Keep networking in one class so a future client change touches one file.
- Read the platform behavior-change notes when you raise compileSdk or targetSdk.
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
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 fixMobile apps
adb tcpip 5555 fails because no device is found
adb: more than one device/emulator
adb tcpip only works while the device is attached and listed by adb, because it tells the phone to start listening on port 5555.
4 causes, each with a fix