将OkHttp添加到我的项目中

时间:2016-09-21 07:41:48

标签: java android gradle

我正在尝试将OkHttp添加到我的项目中,我同时下载OkHttpOkio库并将其添加到libs目录。

比我添加编译方法:

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
}

我尝试构建应用程序并收到此错误:

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.

知道可能是什么问题吗?我正在使用Android Studio om mac OS

2 个答案:

答案 0 :(得分:2)

删除根gradle的这一行:

  directionsArray += [(locationArray[i].textField?.text,
                             locationArray[i+1].textField?.text, routes[i])]

并将此添加到依赖项的应用程序模块build.gradle

答案 1 :(得分:1)

我们的项目中基本上有两个build.gradle文件。

  1. 顶级gradle(可在项目目录中找到)
  2. 模块级gradle(存在于项目的app文件夹中)
  3. 这里的问题是你正在编译okHttp 顶级gradle文件中的compile 'com.squareup.okhttp3:okhttp:3.4.1'尝试将其放在Module level gradle中。此外,如果您使用gradle导入库,则不必将其显式添加到lib文件夹中

    Google gradle documentation