2

我是 android 应用程序开发的新手,这一个月我正在使用 android studio。之前,我创建了很多项目没有任何问题。这是我在创建新项目时收到这个奇怪错误的两天:

Error:Unable to find method 'org.apache.http.impl.client.DefaultHttpClient.setRedirectStrategy(Lorg/apache/http/client/RedirectStrategy;)V'.
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
<a href="syncProject">Re-download dependencies and sync project (requires network)</a></li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

<a href="stopGradleDaemons">Stop Gradle build processes (requires restart)</a></li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

在此处输入图像描述

我也卸载了 android studio 并再次安装,但仍然无法正常工作,而且我以前的项目也不适用于这个新的 android studio。

这就是我的 build.gradle 的样子:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.5.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}
allprojects {
repositories {
    jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

android {
compileSdkVersion 22
buildToolsVersion '19.1.0'
defaultConfig {
    minSdkVersion 21
    targetSdkVersion 21
}
productFlavors {
}
}
dependencies {
}

这是屏幕截图: 在此处输入图像描述

4

1 回答 1

2

我想,您正在尝试使用已经不受支持的库

HttpClient在 sdk 23 中不再支持。

您必须使用URLConnection或降级到 sdk 22 ( compile 'com.android.support:appcompat-v7:22.2.0')

如果您需要 SDK 版本 23,请将其添加到您的build.gradle

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

您也可以尝试下载HttpClient jar并将其直接包含到您的项目中,或者改用OkHttp

如果您需要更多信息,请查看相关主题:


编辑:根据如何从 Gradle 中的所有依赖项中排除库

configurations {
 compile.exclude group:'ch.qos.logback'
}

或者

 configurations {
    // to avoid double inclusion of support libraries
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

检查这个:android studio:gradle依赖错误

希望有帮助

基于这些解释,解决方案是为我(在 Mac 上)从 java exra 库中删除 httpClients 库,如下目录:/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home/jre/lib/ext

于 2016-01-05T22:47:16.730 回答