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

时间:2015-06-15 23:52:10

标签: android apache httpclient build.gradle android-6.0-marshmallow

如上所述here,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'声明为编译时依赖项?

非常感谢任何帮助。感谢

10 个答案:

答案 0 :(得分:170)

对于API 23:

顶级build.gradle - /build.gradle

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

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

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"
    useLibrary 'org.apache.http.legacy'
    ...
}

官方文档(虽然预览):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

答案 1 :(得分: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'
 }

答案 2 :(得分:11)

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

像23.1.1的魅力一样工作。

答案 3 :(得分:10)

在build.gradle文件中,根据Android 6.0 Changes>添加useLibrary 'org.apache.http.legacy'Apache HTTP Client Removal注意到。

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

为避免遗漏链接错误,请添加依赖项

dependencies {
    provided 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}

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

答案 4 :(得分:5)

适用于Android 9(Pie)的注释。

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

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

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

答案 5 :(得分:3)

由于答案有点陈旧,我会提出我的解决方案(对我有用),对其他人有帮助......我从Apache的official documentation获取了我的解决方案,没有工作 - 周围。

1 /在gradle中:

dependencies {
...
// This is the maintained version from apache.
compile group: 'cz.msebera.android', name: 'httpclient', version: '4.4.1.1'
}

2 /在应用程序的其余部分中,将org.apache.http替换为cz.msebera.android.httpclient,并修复所有导入(依赖项)。你可以在整个项目中用ctrl + shift + R替换它。

答案 6 :(得分:2)

我这样解决了这个问题:

1。)将顶级构建文件中的classpath设置为GUG提及:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0-beta2'
    }
    allprojects {
        repositories {
           jcenter()
        }
    }
}

2.)在特定模块的构建文件中:

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

答案 7 :(得分:2)

FWIW前一段时间预示着删除Apache库。我们的好朋友杰西威尔逊在2011年给了我们一个线索: http://android-developers.blogspot.com/2011/09/androids-http-clients.html

谷歌刚刚停止使用ApacheHTTPClient,所以任何仍然依赖它的库都应该被放到已弃用的库列表中,除非维护者更新他们的代码。

<rant> 我不能告诉你我坚持坚持使用Apache HTTP客户端的人有多少技术论据。有一些主要的应用程序将要破解,因为我不知名的前任雇主的管理层没有听他们的高级工程师或知道当他们忽略警告时他们在说什么......但是,水下这座桥。

我赢了。

</rant>

答案 8 :(得分:0)

要解决这些问题,请确保使用构建工具版本“23.0.0 rc2”,并使用以下工具构建gradle依赖项:

cont1.visible = true;
cont2.visible = false;
cont3.visible = false;

答案 9 :(得分:0)

它应该有所帮助:

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

为避免遗漏链接错误,请添加依赖项

dependencies {
    provided 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}

dependencies {
    compileOnly 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}

,因为

Warning: Configuration 'provided' is obsolete and has been replaced with 'compileOnly'.