Android app crashes because the Apache HTTP classes are missing
java.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.
Fix it fast
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 releaseRuntimeClasspath2Match what you see, then jump to its fix
- 1
The 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
You 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
- 1
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:installDebugConfirmed by developer.android.com
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 releaseRuntimeClasspath2Fix
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:assembleReleaseConfirmed by developer.android.com
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
Mobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
Gradle cannot apply the Android application or library plugin
Failed to apply plugin 'com.android.internal.application'.
Gradle loaded the Android Gradle plugin but could not run it.
2 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fixMobile apps
Java and Kotlin compile to different JVM targets in an Android module
Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'kaptGenerateStubsDebugKotlin' (17)
Java and Kotlin tasks in the same module are compiling for different JVM targets, for example 1.8 and 17.
2 causes, each with a fix