Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

HttpClient and org.apache.http classes will not import in Android Studio

ErrorCannot 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.

  • Applies to Android Gradle Plugin 8.x, compileSdk 36, Android Studio
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/null
  2. 2Match what you see, then jump to its fix

    1. 1Imports 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. 2Your own code calls DefaultHttpClient, HttpGet or HttpPostThe networking code should have moved to HttpURLConnection long agoFix

What does the error look like?

  • Cannot resolve symbol 'HttpClient'
  • error: package org.apache.http.client does not exist
  • Unresolved 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=*.kt

2Fix

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:compileDebugJavaWithJavac

Confirmed by developer.android.com

Did this fix it?

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/src

2Fix

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:installDebug

Confirmed by developer.android.com

Did this fix it?

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

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 fix

Mobile 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.