java.lang.NoClassDefFoundError:failed resolution o

2019-01-10 08:30发布

问题:

I 've met such error when I use Android studio 3.1 to build an Android P 's app, the apk can be made ,but when I use it on Android P emulator , it will crash and throw below info, more details see the pic.

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

part of my build.gradle under App module is below , anybody meet this ?And give some suggesstion ? 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'

        }

回答1:

Update: This is no longer 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 (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" />



回答2:

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



回答3:

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.