76

Background

On Android Marshmallow, Google has completely removed the support of Apache HTTP client (link here) because it doesn't have good performance compared to the alternatives.

This might also be the cause for so many apps crashing on Android Marshmallow.

The problem

Google allows you to still use this API, just not as a built in one, by adding this line to the gradle file:

useLibrary 'org.apache.http.legacy'

So, this is what I did:

dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
}

And:

android {
    compileSdkVersion 'android-MNC'
    buildToolsVersion "23.0.0 rc3"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.example.user.androidmtest"
        minSdkVersion 'MNC'
        targetSdkVersion 'MNC'
        versionCode 1
        versionName "1.0"
    }

When I tried it, it compiled fine (no errors being shown, and I could run the proof-of-concept app, as it doesn't have any special code), but when I tried using some of the classes that I know that are part of the old API (like "HttpClient" class), I see that it doesn't allow me to do so.

I know it's not recommended to use this solution, but we must have the app ready to work there at least temporarily, till we work 100% on all of the things that should change for Android Marshmallow, and we don't want surprises in the form of crashes.

Here's a screenshot:

enter image description here

The question

Why does it occur? Did I use it correctly?


EDIT: reported about this issue here:

https://code.google.com/p/android/issues/detail?id=181474

4

12 回答 12

93

Android Studio 抱怨说org.apache.http

org.apache.http.NameValuePair
org.apache.http.client.utils.URLEncodedUtils

失踪了。

所以我添加org.apache.http.legacy.jarAndroid/Sdk/platforms/android-23/optional文件夹中的app/libs

我还将这一行添加到我的app.gradle文件中

compile files('libs/org.apache.http.legacy.jar')

但是如果你使用更多的库,你可以使用这种方式

compile fileTree(dir: 'libs', include: ['*.jar'])

这解决了我所有的错误,因为谷歌删除了对 Apache HTTP 客户端的支持。

于 2015-08-18T07:50:32.157 回答
10

通过运行简单的文件路径检查,完美的解决方案。通过运行

   android {
    compileSdkVersion 'android-MNC'
    buildToolsVersion "23.0.0 rc3"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.example.user.androidmtest"
        minSdkVersion 'MNC'
        targetSdkVersion 'MNC'
        versionCode 1
        versionName "1.0"

    }

        getBootClasspath().each{File file ->
           println file.absolutePath
        }
    }
}

你会得到类似下面的东西

/Users/“你的名字”/Development/android-sdk-macosx/platforms/android-MNC/android.jar /Users/“你的名字”/Development/android-sdk-macosx/platforms/android-MNC/optional/org.apache .http.legacy.jar

所以你去,罐子就在那里。由于某种原因,它没有被添加到项目中。但我猜你总是可以手动添加它。

于 2015-08-05T06:16:12.067 回答
10

useLibrary 'org.apache.http.legacy' 对我不起作用,直到我在我的 Android Studio 项目的主 bu​​ild.gradle 文件中升级了 Gradle 工具版本,如下所示:

dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
}
于 2015-08-24T19:22:26.283 回答
6

经过许多令人沮丧的小时后,以下工作:

1. 找到 apache jar。 它应该位于以下位置:

C:\Users\<yourname>\AppData\Local\Android\sdk\platforms\android-23\optional

2. 复制 org.apache.http.legacy.jar 到您的 libs 文件夹。

右键单击 libs -> paste ,或使用文件资源管理器导航到项目的 libs 文件夹并粘贴。

如果您像我一样没有 libs 文件夹,请创建一个新项目并将所有相关文件导入各自的位置。

3.点击ok 看这个

4.最重要的一步:右键单击apache文件夹并选择Add As Library 看到这个

希望这可以帮助某人继续他们的生活。

于 2016-05-13T20:04:15.527 回答
5

上面的答案只是帮助调试构建运行,并发布使用 gradle 的构建。

将此插入清单文件的应用程序标记中,在使用旧 apache 类的所有项目实例上:

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

这对那些在编译期间仍在使用 Eclipse 和 ant 脚本的人有所帮助。

于 2015-11-04T01:40:33.537 回答
2

旧版 Apache 库位于

[ANDROID_SDK]\platforms\android-23\optional\org.apache.http.legacy.jar 

所以你可以将它复制到你的项目中或者只是使用

compile files("${android.getSdkDirectory().getAbsolutePath()}" + File.separator + "platforms" + File.separator + "android-23" + File.separator + "optional" + File.separator + "org.apache.http.legacy.jar")

在你的/app/build.gradle

于 2015-09-17T09:25:03.830 回答
2

我知道这是愚蠢的原因,但在列表中尝试一下......

我最近遇到了这个问题,它是由路径长度限制引起的,我认为它最多 256 个字符。

重新定位您的项目,构建将成功。希望这对您有用。

于 2015-11-28T09:57:17.133 回答
2

首先你必须在你的 libs 文件夹中检查

确保在 Libs 文件夹中检查 apache 库

Then add into your gradle file like this 

    android {
        compileSdkVersion 23
        buildToolsVersion '23.0.2'

        defaultConfig {
            applicationId "info.tranetech.laundry"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    android {
        useLibrary 'org.apache.http.legacy'
    }
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.1
        compile 'com.android.support:design:23.0.1
        testCompile 'junit:junit:4.12'
        compile files('libs/android-async-http-1.4.4.jar')
        compile 'com.google.android.gms:play-services:8.4.0'
    }

这是我的 gradle 文件

还要检查外部库

于 2016-01-12T10:31:59.657 回答
1

在 sdk/platforms/android-23/optional/optional.json 中启用它

[
  {
    "name": "org.apache.http.legacy",
    "jar": "org.apache.http.legacy.jar",
    "manifest": false
  }
]
于 2016-02-26T07:17:41.223 回答
0

解决这个问题的一个简单方法是 C:\Users\username\AppData\Local\Android\sdk\platforms。在这里删除您的 android-23 并从 SDK 管理器再次更新您的 API 23。它将解决您的问题。

于 2015-12-15T10:21:07.653 回答
0

消除

useLibrary 'org.apache.http.legacy' 

来自 build.gradle,我还将这一行添加到我的 app.gradle 文件中

compile files('libs/org.apache.http.legacy.jar')

但是如果你使用更多的库,你可以使用这种方式

compile fileTree(dir: 'libs', include: ['*.jar'])

CoPLAS 回答解决了我的问题。

于 2016-03-15T02:17:36.103 回答
0

如何在 Android Marshmallow 上使用旧版 Apache HTTP 客户端?

要继续使用 API 23+ 的 Apache HTTP 类:

首先,一定要将gradle依赖添加到build.gradlef

buildscript {

    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
    }
}

然后在项目中添加引用build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"
    useLibrary 'org.apache.http.legacy'
    ...
}
于 2016-05-04T23:10:01.207 回答