Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App (2015) Networking Getting the OkHttp Library Using Gradle

Charles Harpke
Charles Harpke
33,986 Points

Getting the OKHttp library using Gradle:

I paid particular attention to simply copy and paste compile 'com.squareup.okhttp:okhttp:2.2.0' to the dependencies and got the following error:

Error:Failed to find: com.squareup.okhttp:okhttp:2.1.0 <a href="openFile">Open File</a><br><a href="open.dependency.in.project.structure">Open in Project Structure dialog</a>

I am running Andriod Studio version : 0.9.9 on a JVM 1.7.0_75, here is my build.grade file(ModuleApp)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "chuckharpke.com.stormy"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.squareup.okhttp:okhttp:2.1.0'
}
Charles Harpke
Charles Harpke
33,986 Points

The same error happens if I use: compile 'com.squareup.okhttp:okhttp:2.2.0'

11 Answers

Try going to Android Studio | Preferences | Gradle and untick Offline Work, select Auto-import and 'Create directories for empty ... '

Worked for me!

Did you Synch Project with Gradle Files? Button is on tool bar, fifth one in from the right end (the ? or help button)

Charles Harpke
Charles Harpke
33,986 Points

That's the error is got after I synched gradle....yes...I have also run this in Android Studio 1.0.0 and I got the same error.

Your Gradle file looks fine to me. I wonder if there's a connectivity/firewall issue. Have you used the Gradle dependencies section successfully before?

It could be worth adding compile 'com.jakewharton:butterknife:6.0.0' (also used for the Stormy project) above the OkHttp line and seeing if that fails also. If it does it might suggest a more general problem rather than a problem with the OkHttp line specifically. Could be worth trying a restart or the "Invalidate Caches / Restart" menu option just in case.

Charles Harpke
Charles Harpke
33,986 Points

I added dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.jakewharton:butterknife:6.0.0' compile 'com.squareup.okhttp:okhttp:2.2.0' }

I get the same error. I then did: 'Invalidate caches and restart', and I still get the same error.

Charles Harpke
Charles Harpke
33,986 Points

This appears to have fixed the issue:

File -> Invalidate caches / Restart Shutdown Android Studio Rename/remove .gradle folder in the user home directory Restart Android Studio let it download all the Gradle stuff it needs Gradle build success ! Rebuild project.... success !

Samuel Zachery
Samuel Zachery
Courses Plus Student 3,234 Points

for charles: could you give more detail on what you did? tried this and its still not working. neither the okayhttp or the butterknife are working.

for anyone: I am using the lastest versions of java and android studio is it possible that is the problem?

Glad to hear you solved the problem! It must have been frustrating.

Charles Harpke
Charles Harpke
33,986 Points

Thanks for your responses....I was Googling that one for a while!!

Charles Harpke
Charles Harpke
33,986 Points

This may not be a feasible workaround if you have project(s) in your version of Android Studio, however, that workaround worked in regard to gradle because I had only 1 project. In any case, I am running Android 1.0.1, with Java 1.7.0_75. I exited my Android Studio. Went into the 'Contents' folder in \Android\Contents\Gradle and renamed Gradle to Gradle.Old. I then restarted Android Studio, when starting it downloads gradle again with all the dependencies.

Charles Harpke
Charles Harpke
33,986 Points

Yes sir...mine prompted me to turn off offline after renaming....maube I employed too many steps.