OkHttp添加到我的项目

问题描述:

我想补充OkHttp到我的项目,我同时下载OkHttpOkio库,并将其添加到libs目录。OkHttp添加到我的项目

比我添加编译方法:

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

删除此行根gradle这个的:

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

这对你的依赖的应用程序的模块添加的build.gradle

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

  1. 顶级gradle这个(可在项目目录中找到)
  2. 模块级gradle这个(目前该项目的应用程序文件夹中)

这里的问题是,你正在整理okHttp 尝试将顶层gradle文件中的compile 'com.squareup.okhttp3:okhttp:3.4.1'放入您的模块级gradle中。此外,如果您正在使用gradle导入库,则不必将其添加到lib文件夹中明确指定

Google gradle documentation