当我使用Android Studio构建我的应用程序时,我遇到了这个错误。APK被编译,但当我试图在Android P模拟器上运行应用程序时,它会崩溃并抛出以下错误。详情请见附件:

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

这是我的身材。级文件。如果有人对问题有什么建议,我将不胜感激。多谢。

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'
        }

当前回答

如果你正在使用com.google.android.gms:play-services-maps:16.0.0或以下,并且你的应用程序是API级别28 (Android 9.0)或以上,你必须在AndroidManifest.xml的元素中包含以下声明。

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

如果你使用com.google.android.gms:play-services-maps:16.1.0,这是为你处理的,如果你的应用程序的目标是较低的API级别,这是不必要的。

其他回答

这个链接android-9.0-changes-28——>Apache HTTP客户端弃用解释了在你的AndroidManifest.xml中添加以下内容的原因:

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

在Android 6.0中,我们删除了对Apache HTTP客户端的支持。 从Android 9开始,该库从 Bootclasspath,默认情况下对应用程序不可用。

更新:这不再是一个错误或解决方案,如果你的应用程序目标API级别28 (Android 9.0)或以上,并使用谷歌地图SDK for Android 16.0.0或以下(或如果你的应用程序使用Apache HTTP Legacy库),这是必需的。它现在被包含在官方文档中。公开的问题已被关闭作为有意的行为。

这是谷歌播放服务端的一个错误,直到它被修复,你应该能够通过将此添加到<application>标签内的AndroidManifest.xml来解决:

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

如果你使用安卓9.0的遗留jar比你必须使用。 在你的mainfest文件里。

< uses-library android: name = " org.apache.http。遗产”android:要求= " false " / >

为了在Android 9.0中完美地运行org.apache.http.legacy,请创建一个xml文件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>

在你的AndroidManifest.xml中添加2个标签

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>

还可以添加useLibrary ` org.apache.http。遗留'在你的应用程序构建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'
}

Android漏洞追踪器https://issuetracker.google.com/issues/79478779上也有报道