360

当我使用 Android Studio 构建我的应用程序时,我遇到了这个错误。APK 已编译,但当我尝试在 Android P 模拟器上运行该应用程序时,它会崩溃并抛出以下错误。请在附件中查看更多详细信息:

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

在此处输入图像描述

这是我的 build.grade 文件。如果有人对可能出现的问题提出建议,我将不胜感激。非常感谢。

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

12 回答 12

892

更新:这不再是错误或解决方法,如果您的应用针对 API 级别 28 (Android 9.0) 或更高版本并使用适用于 Android 16.0.0 或更低版本的 Google Maps SDK(或者如果您的应用使用 Apache HTTP旧库)。它现在包含在官方文档中。公共问题已按预期行为关闭。

这是 Google Play 服务端的一个错误,在修复之前,您应该能够通过将其添加到您的标签AndroidManifest.xml内部来解决此问题:<application>

<uses-library android:name="org.apache.http.legacy" android:required="false" />
于 2018-06-09T23:00:12.077 回答
62

此链接android-9.0-changes-28-->Apache HTTP client deprecation解释了将以下内容添加到 AndroidManifest.xml 的原因:

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

在 Android 6.0 中,我们移除了对 Apache HTTP 客户端的支持。从 Android 9 开始,该库已从引导类路径中移除,并且默认情况下对应用程序不可用。

于 2018-12-18T07:20:38.660 回答
37

执行以下任一操作:

1- 将play-services-maps库更新到最新版本:

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

<application>2-或在 . 的元素中包含以下声明AndroidManifest.xml

<uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />
于 2019-02-12T16:34:18.957 回答
28

在您的 AndroidManifest.xml 中添加这两行。

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

请参阅下面的代码

    <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>
于 2020-02-25T21:14:40.843 回答
23

如果您使用带有旧版 jar 的 Android 9.0,则必须使用。在你的 mainfest 文件中。

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

于 2019-08-19T07:37:59.667 回答
20

要在 Android 9.0 Pie 中完美运行 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.legacy'到您的应用程序构建 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'
}
于 2019-03-12T13:02:03.433 回答
15

它还在 Android 错误跟踪器上报告:https ://issuetracker.google.com/issues/79478779

于 2018-06-07T08:27:33.740 回答
14

如果您正在使用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" />

检查此链接 - https://developers.google.com/maps/documentation/android-sdk/config#specify_requirement_for_apache_http_legacy_library

于 2020-01-30T07:43:29.660 回答
9
于 2020-10-19T10:11:50.290 回答
8

如果您使用 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 级别,则不需要。

于 2019-02-21T05:38:06.983 回答
5

在本机反应中,我出现错误未显示地图并关闭应用程序,运行 adb logcat 并在控制台中显示错误:

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

通过在 androidManifest.xml 中添加来修复它

<uses-library
  android:name="org.apache.http.legacy"
  android:required="false" />
于 2019-11-25T17:11:09.990 回答
1

根据这个 SO answer,它是由于 AWS SDK 错误而发生的,该错误似乎在 SDK 的 2.6.30 版本中得到解决,因此将版本更新为更新版本可以帮助您解决问题。

于 2019-07-02T20:44:52.220 回答