java.lang.NoClassDefFoundError: 解析失败:Lorg/apache/http/ProtocolVersion

Posted

技术标签:

【中文标题】java.lang.NoClassDefFoundError: 解析失败:Lorg/apache/http/ProtocolVersion【英文标题】:java.lang.NoClassDefFoundError:failed resolution of :Lorg/apache/http/ProtocolVersion 【发布时间】:2018-10-31 21:06:41 【问题描述】:

我在使用 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'
        

【问题讨论】:

【参考方案1】:

更新:这不再是错误或解决方法,如果您的应用针对 API 级别 28 (Android 9.0) 或更高版本并使用适用于 Android 16.0.0 或更低版本的 Google Maps SDK,则需要它(或者如果您的应用程序使用 Apache HTTP Legacy 库)。它现在包含在official docs 中。公开问题已将closed 视为预期行为。

这是 Google Play 服务端的 bug,在修复之前,您应该可以通过将其添加到 <application> 标记内的 AndroidManifest.xml 来解决此问题:

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

【讨论】:

这不再是一种解决方法。这是现在正确的方法developers.google.com/maps/documentation/android-sdk/… @AdamK,Android 9 是 API 级别 28。=P(不是来自服务器端的错误,而是关于 P 行为改变) 任何不使用地图的人也会遇到这种情况? 是的,如果您使用 Apache HTTP Legacy 库(或您的依赖项之一),您也可能会遇到它。 我正在使用 Apache HTTP Legacy 库,这救了我。谢谢!【参考方案2】:

此链接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 开始,该库已从 bootclasspath,默认情况下对应用程序不可用。

【讨论】:

【参考方案3】:

执行以下任一操作:

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

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

2- 或者在AndroidManifest.xml&lt;application&gt; 元素中包含以下声明。

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

【讨论】:

对于 Android 9 开发人员,当然对于 Google 来说,这又是一个巨大的耻辱。我希望他们修改 Android 9 的实现和行为,因为它有很多错误。不幸的是,谷歌坚持称这些错误,行为改变!【参考方案4】:

在您的 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>

【讨论】:

【参考方案5】:

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

&lt;uses-library android:name="org.apache.http.legacy" android:required="false"/&gt;

【讨论】:

【参考方案6】:

要在 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 个 tags 标签

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>

还要在您的应用构建 gradle 中添加 useLibrary 'org.apache.http.legacy'

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'

【讨论】:

当您打算向公众发布应用程序时,这似乎是您不应该做的事情。对于开发来说可能是有道理的,但它仍然无法在生产中工作,所以没有任何意义。 这个答案应该是被接受的答案,我尝试了很多方法但没有奏效。干得好@Gitesh【参考方案7】:

Android bug tracker 上也有报道:https://issuetracker.google.com/issues/79478779

【讨论】:

它也在文档中developers.google.com/maps/documentation/android-sdk/…【参考方案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" />

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

【讨论】:

【参考方案9】:

这可能是迟到的答案。但是,我希望它可以节省一些时间:

如果您使用的是 com.google.android.gms:play-services-maps:16.0.0 或更低版本,并且您的应用面向 API 级别 28 (Android 9.0) 或更高版本,则必须在 AndroidManifest.xml

的元素中包含以下声明
<application
        ...
        ...
        android:usesCleartextTraffic="true"
        android:requestLegacyExternalStorage="true">

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

</application>

注意:Android 10+ 需要 android:requestLegacyExternalStorage="true" 才能访问存储 R/W

Android 6.0 在 android manifest 的 application 元素下引入了 useCleartextTraffic 属性。 Android P 中的默认值为“false”。将此设置为 true 表示该应用打算使用清晰的网络流量。

如果您的应用在 Android 10 设备 上运行时选择退出范围存储,建议您继续在应用的清单文件中将 requestLegacyExternalStorage 设置为 true。这样,您的应用就可以在运行 Android 10

的设备上继续按预期运行

【讨论】:

为我工作..解释得很好【参考方案10】:

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

【讨论】:

【参考方案11】:

在本机反应中,我遇到错误未显示地图并关闭应用程序,运行 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" />

【讨论】:

【参考方案12】:

根据this SO answer 的说法,这是由于似乎已解决in version 2.6.30 of the SDK 的 AWS 开发工具包错误所致,因此将版本更新为新版本可以帮助您解决问题。

【讨论】:

以上是关于java.lang.NoClassDefFoundError: 解析失败:Lorg/apache/http/ProtocolVersion的主要内容,如果未能解决你的问题,请参考以下文章