364

I'm encountering this error when I use Android Studio to build my app. The APK is compiled, but when I attempt to run the app on Android P emulator, it will crash and throw the following error. Please see more details in the attachments:

java.lang.NoClassDefFoundError:failed resolution of :Lorg/apache/http/ProtocolVersion

enter image description here

This is my build.grade file. If anybody has a suggestion on what the problem could be, I would appreciate it. Many thanks.

android {

     compileSdkVersion 'android-P'
     buildToolsVersion '28-rc1'
   
    useLibrary 'org.apache.http.legacy'

    //for Lambda
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {

        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }
    defaultConfig {
        applicationId "xxx.xxx.xxx"
        minSdkVersion 17
        targetSdkVersion 27
        versionCode xxxx
        versionName "Vx.x.x"

        multiDexEnabled true
     

     //other setting required
        ndk {
            abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a', 'x86', 'x86_64', 'mips', 'mips64'
        }
0

12 Answers 12

900

Update: This is no longer a bug or a workaround, it is required if your app targets API Level 28 (Android 9.0) or above and uses the Google Maps SDK for Android 16.0.0 or below (or if your app uses the Apache HTTP Legacy library). It is now included in the official docs. The public issue has been closed as intended behavior.

This is a bug on the Google Play Services side, until it's fixed, you should be able to workaround by adding this to your AndroidManifest.xml inside the <application> tag:

<uses-library android:name="org.apache.http.legacy" android:required="false" />
11
  • 90
    This is not a workaround anymore. This is the correct way now developers.google.com/maps/documentation/android-sdk/… Aug 13, 2018 at 22:09
  • @AdamK, Android 9 is API level 28. =P (not a bug from serverside, but about P behavior change)
    – Fung
    Aug 16, 2018 at 6:24
  • 13
    Anyone not using maps experiencing this as well? Oct 12, 2018 at 17:51
  • 3
    Yes you may run into it as well if you use the Apache HTTP Legacy library (or one of your dependencies do).
    – AdamK
    Oct 13, 2018 at 2:07
  • 3
    I'm using Apache HTTP Legacy library and this just saved me. Thank you!
    – Eric B.
    Oct 13, 2018 at 23:21
63

This link android-9.0-changes-28-->Apache HTTP client deprecation explains reason for adding the following to your AndroidManifest.xml:

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

With Android 6.0, we removed support for the Apache HTTP client. Beginning with Android 9, that library is removed from the bootclasspath and is not available to apps by default.

37

Do any of the following:

1- Update the play-services-maps library to the latest version:

com.google.android.gms:play-services-maps:16.1.0

2- Or include the following declaration within the <application> element of AndroidManifest.xml.

<uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />
1
  • 3
    Again it is a great shame for Android 9 developers and of course for Google. I hope they revise the Android 9 implementation and behavior as it has many bugs. Unfortunately, Google insists on calling these bugs, behavior changes! Jul 7, 2019 at 10:34
29

In your AndroidManifest.xml add this two-line.

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

See this below code

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning">
        <activity android:name=".activity.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

If You using Android 9.0 with legacy jar than you have to use. in your mainfest file.

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

0
20

To run org.apache.http.legacy perfectely in Android 9.0 Pie create an xml file res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
      <base-config cleartextTrafficPermitted="true">
       <trust-anchors>
        <certificates src="system" />
       </trust-anchors>
      </base-config>
    </network-security-config>

And add 2 tags tag in your AndroidManifest.xml

android:networkSecurityConfig="@xml/network_security_config" android:name="org.apache.http.legacy"

<?xml version="1.0" encoding="utf-8"?>
 <manifest......>
  <application android:networkSecurityConfig="@xml/network_security_config">
   <activity..../> 
   ......
   ......
 <uses-library
        android:name="org.apache.http.legacy"
        android:required="false"/>
</application>

Also add useLibrary 'org.apache.http.legacy' in your app build gradle

android {
compileSdkVersion 28
defaultConfig {
    applicationId "your application id"
    minSdkVersion 15
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    useLibrary 'org.apache.http.legacy'
}
2
  • This looks like something you shouldn't be doing when you intend to launch an app to the public. For development maybe it makes sense, but then it won't still work on production so there is no sense in that. Apr 16, 2019 at 7:08
  • This answer should be accepted answer, I tried many ways but not worked any. Good Job @Gitesh Aug 21, 2019 at 10:49
15

It's also reported on Android bug tracker: https://issuetracker.google.com/issues/79478779

1
14

If you are using com.google.android.gms:play-services-maps:16.0.0 or below and your app is targeting API level 28 (Android 9.0) or above, you must include the following declaration within the element of AndroidManifest.xml

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

Check this link - https://developers.google.com/maps/documentation/android-sdk/config#specify_requirement_for_apache_http_legacy_library

14

This Might be Late Answer. but, I hope its save someone time:

If you are using com.google.android.gms:play-services-maps:16.0.0 or below and your app is targeting API level 28 (Android 9.0) or above, you must include the following declaration within the element of AndroidManifest.xml

<application
        ...
        ...
        android:usesCleartextTraffic="true"
        android:requestLegacyExternalStorage="true">

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

</application>

Note: android:requestLegacyExternalStorage="true" required for Android 10+ to access storage R/W

Android 6.0 introduced the useCleartextTraffic attribute under application element in android manifest. The default value in Android P is “false”. Setting this to true indicates that the app intends to use clear network traffic.

If your app opts out of scoped storage when running on Android 10 devices, it's recommended that you continue to set requestLegacyExternalStorage to true in your app's manifest file. That way, your app can continue to behave as expected on devices that run Android 10

1
  • Worked for me.. well explained Aug 24, 2021 at 8:57
8

If you are using com.google.android.gms:play-services-maps:16.0.0 or below and your app is targeting API level 28 (Android 9.0) or above, you must include the following declaration within the element of AndroidManifest.xml.

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

This is handled for you if you are using com.google.android.gms:play-services-maps:16.1.0 and is not necessary if your app is targeting a lower API level.

6

In react native, I had the error not show maps and close app, run adb logcat and show error within console:

java.lang.NoClassDefFoundError:failed resolution of :Lorg/apache/http/ProtocolVersion

fix it by adding within androidManifest.xml

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

According to this SO answer, it occurs due to an AWS SDK bug that appears to be solved in version 2.6.30 of the SDK, so updating the version to a newer, can help you fixing the problem.

Not the answer you're looking for? Browse other questions tagged or ask your own question.