如何将Apache HTTP API(旧版)作为编译时依赖添加到Android M的build.grade?

Vir*_*ngh 94 apache android httpclient build.gradle android-6.0-marshmallow

如前所述这里,Android的M将不支持的Apache HTTP API.文档声明:

请改用HttpURLConnection类.

要么

要继续使用Apache HTTP API,必须首先在build.gradle文件中声明以下编译时依赖项:

android {useLibrary'org.apache.http.legacy'}

我已经将我项目的大部分HttpClient用法转换为HttpURLConnection,但是,我仍然需要在一些领域使用HttpClient.因此,我试图将'org.apache.http.legacy'声明为编译时依赖项,但在build.gradle中出现错误:

找不到Gradle DSL方法:'useLibrary()'

我的问题是:如何在项目中将'org.apache.http.legacy'声明为编译时依赖项?

任何帮助深表感谢.谢谢

hid*_*dro 172

对于API 23:

顶级build.gradle - /build.gradle

buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}
...
Run Code Online (Sandbox Code Playgroud)

特定于模块的build.gradle - /app/build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"
    useLibrary 'org.apache.http.legacy'
    ...
}
Run Code Online (Sandbox Code Playgroud)

官方文档(虽然预览):http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

最新的android gradle插件更新日志:http://tools.android.com/tech-docs/new-build-system

  • 我更新gradle构建版本然后为apache声明useLibrary但是我也会得到错误:错误:(204,13)错误:找不到符号类DefaultHttpClient错误:(204,48)错误:找不到符号类DefaultHttpClient错误: (205,13)错误:找不到符号类HttpPost错误:(205,37)错误:找不到符号类HttpPost错误:(207,13)错误:找不到符号类HttpResponse错误:(208,13)错误:不能查找符号类HttpEntity错误:(209,19)错误:找不到符号变量EntityUtils (2认同)

nex*_*Dev 28

另一种选择是只添加jbundle依赖.这对Android Studio更加友好,因为Android Studio没有给出消息"无法解析符号......"

 dependencies {
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
 }
Run Code Online (Sandbox Code Playgroud)


小智 13

在build.gradle文件中,按照> notes 添加useLibrary'org.apache.http.legacy'.Android 6.0 ChangesApache HTTP Client Removal

android {
    ...
    useLibrary 'org.apache.http.legacy'
    ...
}
Run Code Online (Sandbox Code Playgroud)

为了避免丢失链接错误,请添加依赖项

dependencies {
    provided 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}
Run Code Online (Sandbox Code Playgroud)

使用'提供'的依赖项将不包含在apk中


Gun*_*ein 12

Android 9(Pie)注意事项。

另外,useLibrary 'org.apache.http.legacy'您还必须添加AndroidManifest.xml:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>
Run Code Online (Sandbox Code Playgroud)

来源:https : //developer.android.com/about/versions/pie/android-9.0-changes-28


小智 11

刚刚复制文件:org.apache.http.legacy.jarAndroid/Sdk/platforms/android-23/optional文件夹到项目文件夹app/libs.

工作就像23.1.1的魅力.