简体   繁体   中英

Could not resolve com.android.tools.build:gradle:2.2.3

I'm working on a mobile app project for work, but often when I attempt to run the application it gives me the following error:

Error:A problem occurred configuring root project 'projectName'. Could not resolve all dependencies for configuration ':classpath'. Could not resolve com.android.tools.build:gradle:2.2.3. Required by: :projectName:unspecified No cached version of com.android.tools.build:gradle:2.2.3 available for offline mode.

I have tried a number of different things including deselecting the Offline work checkbox in the settings and then closing and re-opening Android Studio, but nothing seems to fix the issue. It may build fine for a few times in a row, but that same error keeps popping up. Then I have to try variations of building and making the app, or exiting and re-opening Android Studios. Nothing consistently works though. Any insight into what may be causing this issue and how it can be fixed would be appreciated.

***Update: Upon request I have added below my app gradle content

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
useLibrary 'org.apache.http.legacy'

dexOptions {
    javaMaxHeapSize "8g"
}

defaultConfig {
    applicationId "com.example"
    minSdkVersion 15
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"

    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug{ 
        debuggable true
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.google.android.gms:play-services:9.4.0'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:support-v4:24.2.1'

compile files('libs/gson-2.2.2.jar')
compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'
compile 'com.facebook.stetho:stetho:1.3.1'
compile 'com.loopj.android:android-async-http:1.4.9'
}

It turns out the solution to my particular issue was relatively simple. The error message was a bit deceiving as this was not a problem with the offline mode switch, but rather confusion with the gradle file. Here are a few steps I took to fix the issue:

  1. For Android Studio 2.3 go to File>Settings>Gradle and select the "Use default gradle wrapper" option. Also make sure the checkbox next to "Offline work" is unchecked just in case.
  2. Click the "Apply" button
  3. Within the build.gradle file (the one named after your project) under the "Gradle Scripts" section of Android Studio make certain that your current build gradle class path matches the current version of Android Studio. An example for Android Studio 2.3:

     buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.0' } } 
  4. Also within the Gradle Scripts section make sure that your gradle-wrapper.properties file is referencing the correct gradle file in the distribution URL. For example for gradle 3.3:

     distributionUrl=https\\://services.gradle.org/distributions/gradle-3.3-all.zip 
  5. Navigate to C:\\Program Files\\Android\\Android Studio\\gradle and ensure there is a gradle folder with the same name as the gradle you are using. In my case I ensured there is a folder named gradle-3.3 with a zip file named gradle-3.3-all.zip. It is relatively easy to find this zip file online.

  6. Delete the .gradle folder in C:\\Users\\yourUser with yourUser being whichever user your Android Studio project is under.
  7. Close Android Studio and restart. It should begin the gradle sync process and rebuild the .gradle file.

For me when I did this it also complained about not finding the gradle-3.3-all.zip file in the .gradle folder. I was able to fix this by adding the gradle-3.3-all.zip file to C:\\Users\\yourUser.gradle\\wrapper\\dists\\gradle-3.3-all\\55gk2rcmfc6p2dg9u9ohc3hw9. I'm not sure if the folder has the same name for everyone, but whatever the name, that is the location I went to. If all this doesn't work I would try a variation of these steps, and hopefully it works for you.

The reason why it is failing is because it could not compile one of the libraries you included. Perhaps, try removing the one you suspect and try again. But most of the times, a rebuild of the project should resolve dependencies.

I've had the same pb, managed to resolve it adding in gradle.properties

systemProp.https.proxyHost=<my proxy hostname>
systemProp.https.proxyPort=<my proxy port>

I was missing HTTPS property, there was only HTTP setted so it was working sometimes, and not for the https links gradle used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM