关于Android 9.0 ClassNotFoundException: Didn’t find class “org.apache.http.protocol.BasicHttpContex…
近期在项目中适配了Android9.0-P,但是在部分用户的手机上出现闪退的现象,通过捕获错误日志发现报的错如下:
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.protocol.BasicHttpContext" on path: DexPathList[[zip file "/data /app/com.jyc99.jyc-v6asoBfubX-NaDf0IoqSvQ==/base.apk", dex file "/data /app/com.jyc99.jyc-v6asoBfubX-NaDf0IoqSvQ==/base.apk"],nativeLibraryDirectories=[/data /app/com.jyc99.jyc-v6asoBfubX-NaDf0IoqSvQ==/lib/arm64, /data/app/com.jyc99.jyc- v6asoBfubX-NaDf0IoqSvQ==/base.apk!/lib/arm64-v8a, /system/lib64, /product/lib64]]
在Google官方文档上查找到了原因,从 Android 9 开始,默认情况下该内容库已从 bootclasspath 中移除且不可用于应用。 要继续使用 Apache HTTP 客户端,以 Android 9 及更高版本为目标的应用可以向其 AndroidManifest.xml的application节点下 添加以下内容:
<uses-library android:name="org.apache.http.legacy" android:required="false" />
在application下面添加 <uses-library>即可:
<application android:name=".MyApp" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <uses-library android:name="org.apache.http.legacy" android:required="false" /> <activity android:name=".activity.MainActivity" android:exported="true" android:screenOrientation="portrait" android:theme="@style/nomor"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application>
其中android:required=”false” 属性是为了SDK 版本 在23 或更低版本的app上起作用 ,因为在 API 级别低于 24 的设备上,org.apache.http.legacy 库不可用。 (在这些设备上,Apache HTTP 类在 bootclasspath 中提供。)
所以作为使用运行时 Apache 库的替代,应用可以在其 APK 中绑定自己的 org.apache.http 库版本。 如果进行此操作,您必须将该库重新打包(使用一个类似 Jar Jar 的实用程序)以避免运行时中提供的类存在类兼容性问题。
至此以上问题就得到解决,此问题不是在所有的安卓9.0版本上发生,所以比较难发现。
原文地址:https://www.jianshu.com/p/d907aad8d973
相关推荐
-
浅谈tcp流的解析 Java基础
2020-6-15
-
在Spring boot 2 微服务中使用 GRPC Java基础
2019-9-7
-
从单例模式到HappensBefore Java基础
2019-5-6
-
如何开发自己的搜索帝国之ES图形化Kibana安装与使用,ElasticSearch 5学习(2)——Kibana+X-Pack介绍使用(全) Java基础
2019-3-26
-
MySQL数据库优化 Java基础
2019-4-1
-
为什么说java是只有值传递? Java基础
2019-8-17
-
为抖音而生的多闪,如何获取抖音的用户数据? Java基础
2019-6-26
-
LinkedHashMap如何保证顺序性 Java基础
2019-7-28
-
Go 每日一库之 viper Java基础
2020-6-16
-
使用ConcurrentHashMap一定线程安全? Java基础
2020-5-28