【问题标题】:How to use apache http on Android P如何在 Android P 上使用 apache http
【发布时间】:2018-05-21 09:50:27
【问题描述】:

当我在 Android P 设备上运行我的应用程序时,我收到如下错误:

java.lang.RuntimeException: Unable to instantiate application com.le.android.client.LeApplication: java.lang.ClassNotFoundException: Didn't find class "com.le.android.client.LeApplication" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.le.android.client-uvQkO641-__8Z_p2oT0t7g==/base.apk"],nativeLibraryDirectories=[/data/app/com.le.android.client-uvQkO641-__8Z_p2oT0t7g==/lib/arm, /data/app/com.le.android.client-uvQkO641-__8Z_p2oT0t7g==/base.apk!/lib/armeabi, /system/lib, /vendor/lib]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:1009)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5836)
at android.app.ActivityThread.access$1000(ActivityThread.java:198)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1637)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6649)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.le.android.client.LeApplication" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.le.android.client-uvQkO641-__8Z_p2oT0t7g==/base.apk"],nativeLibraryDirectories=[/data/app/com.le.android.client-uvQkO641-__8Z_p2oT0t7g==/lib/arm, /data/app/com.le.android.client-uvQkO641-__8Z_p2oT0t7g==/base.apk!/lib/armeabi, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:50)
at android.app.Instrumentation.newApplication(Instrumentation.java:1120)
at android.app.LoadedApk.makeApplication(LoadedApk.java:1001)
... 9 more

我应该如何在Android P中使用库org.apache.http.legacy

【问题讨论】:

    标签: java android apache-httpcomponents


    【解决方案1】:

    我阅读了Android P behavior changes,收到了一些消息:

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

    要继续使用 Apache HTTP 客户端, 针对 Android P 及更高版本的应用必须在其 AndroidManifest.xml 中添加以下内容:

    注意:最低 SDK 为 23 或更低的应用需要 android:required="false" 属性,因为在 API 级别低于 24 的设备上,org.apache.http.legacy 库不可用. (在这些设备上,Apache HTTP 类在 bootclasspath 中可用。)

    【讨论】:

    • 即使添加了这个......我的应用程序仍然崩溃并出现同样的错误@scott
    • 谢谢,它对我有用,我想我只需要在 gradle 文件中添加 useLibrary 的东西。
    【解决方案2】:

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

    【讨论】:

    猜你喜欢
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多