Maaz Zindani

Loading the index…

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

Android app crashes because the Apache HTTP classes are missing

Errorjava.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;

Short answer

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. Declare the legacy library in AndroidManifest.xml with android:required set to false, or bundle your own repackaged copy of the library.

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

Fix it fast

  1. 1Find out which cause you have

    Find out which of your dependencies pulls in the org.apache.http classes, because the fix depends on whether you call them yourself or a library does.

    ./gradlew :app:dependencies --configuration releaseRuntimeClasspath
  2. 2Match what you see, then jump to its fix

    1. 1The crash happens only on devices running Android 9 (API 28) or newer, in code that touches org.apache.http.The app or a library still uses the removed Apache HTTP clientFix
    2. 2You cannot depend on a runtime library that the device may not provide, or you need a newer version of the classes.You want the app to carry its own Apache HTTP libraryFix

What does the error look like?

  • java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;

Cause 1 of 2

The app or a library still uses the removed Apache HTTP client

1Check

Android 6.0 dropped support for the Apache HTTP client, and Android 9 removed it from the bootclasspath, so it is not available to apps by default. Look in the stack trace for the first frame that is yours or a library's.

adb logcat -d | grep -n "org/apache/http"

2Fix

Add the legacy library to the application element of the manifest. Keep android:required set to false when minSdk is 23 or lower, because the library does not exist on devices below API 24.

<application ...>
    <uses-library android:name="org.apache.http.legacy" android:required="false"/>
</application>

// app/build.gradle (only if your own code compiles against the classes)
android {
    useLibrary 'org.apache.http.legacy'
}

3Verify

Rebuild, install on an Android 9 or newer device or emulator and repeat the action that crashed.

./gradlew :app:installDebug

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 2

You want the app to carry its own Apache HTTP library

1Check

Check whether the classes you need come from a dependency you can swap, or from your own code that could move to a maintained HTTP client.

./gradlew :app:dependencyInsight --dependency httpclient --configuration releaseRuntimeClasspath

2Fix

Bundle your own copy of org.apache.http in the APK. The Android docs say you must repackage it, for example with Jar Jar, so its classes do not clash with the ones in the runtime.

# 1. Repackage the library with a tool such as Jar Jar so the package name changes
# 2. Add the repackaged jar as a dependency of the app module
dependencies {
    implementation files("libs/httpclient-repackaged.jar")
}

3Verify

Build the app and confirm the original package name no longer appears in the APK's own classes.

./gradlew :app:assembleRelease

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Test on a current Android emulator before every release, not only on the oldest device you support.
  • Check what a library needs from the platform before adding it to a project that targets API 28 or higher.
  • Prefer a maintained HTTP client over the legacy Apache one in new code.

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

Next step

Want this handled for good?

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