Search code examples
javaandroidandroid-studioandroid-gradle-pluginnew-project

android studio failed to create new project


I am new in android app development, and this a month that I am working with android studio. Before, I created lots of project without any problem. This is two days that I received this strange error when I create a new project:

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.

enter image description here

I also uninstall android studio and install it again but still doesn't work well, and also my previous projects don't work with this new android studio.

this is how my build.gradle looks like:

// 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 {
}

and this is the screen shot: enter image description here


Solution

  • I suppose that, you're trying to use already unsupported library

    HttpClient is not supported any more in sdk 23.

    You have to use URLConnection or downgrade to sdk 22 (compile 'com.android.support:appcompat-v7:22.2.0')

    If you need SDK version 23, add this to your build.gradle:

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

    You also may try to download and include HttpClient jar directly into your project or use OkHttp instead.

    If you need more information, please check also related topics:


    EDIT: According to How to exclude libraries from all dependencies in Gradle

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

    or

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

    Check this: android studio: gradle dependency error

    Hope it help

    Based on these explanations, the solution is to remove httpClients libraries from java exra libraries for me (on mac) form following dir: /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home/jre/lib/ext