YosiFZ
YosiFZ

Reputation: 7900

Add OkHttp to my project

I am trying to add OkHttp to my project, i download both OkHttp and Okio library and add it to the libs directory.

than i add the compile methods:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'

        compile 'com.squareup.okhttp3:okhttp:3.4.1'

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

I try to Build the App and i get this error:

Error:(27, 1) A problem occurred evaluating root project 'APP'.
> Could not find method compile() for arguments [com.squareup.okhttp3:okhttp:3.4.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Any idea what can be the problem? i am using Android Studio om mac OS

Upvotes: 1

Views: 6190

Answers (2)

Tabish Hussain
Tabish Hussain

Reputation: 852

There are basically two build.gradle files in our project.

  1. Top level gradle (can be found in the project directory)
  2. Module level gradle (present inside the app folder of the project)

Here the problem is you are compiling the okHttp compile 'com.squareup.okhttp3:okhttp:3.4.1'in the Top level gradle file try to place it in your Module level gradle. Also if you are importing a library using gradle you don't have to add it in the lib folder explicitly

Google gradle documentation

Upvotes: 1

blkrt
blkrt

Reputation: 297

Delete this line of your root gradle :

compile 'com.squareup.okhttp3:okhttp:3.4.1'

And Add this to your dependencies's app's module build.gradle

Upvotes: 3

Related Questions