Maaz Zindani

Loading the index…

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

The build cannot find the org.apache.http classes in an Android project

Errorerror: package org.apache.http does not exist

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

  • Applies to Android Gradle plugin 8.x, Android 9 and newer
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1error: 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. 2The 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. 3You own the code that uses HttpClient, HttpGet or HttpPostThe code should leave the Apache client behindFix

What does the error look like?

  • error: package org.apache.http does not exist
  • error: 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.kts

2Fix

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

Confirmed by developer.android.com

Did this fix it?

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

2Fix

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

Confirmed by developer.android.com

Did this fix it?

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

2Fix

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

Confirmed by developer.android.com

Did this fix it?

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 fix

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

Next step

Want this handled for good?

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