Android Studio / Kotlin:Gradle错误 – 找不到参数的compile()方法

我正在使用Android Studio 3.0 Canary 6.我一直在build.gradle (Project)文件有一些麻烦。

build.gradle文件如下所示:

 // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.1.3-2' ext.support_version = '26.0.1' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha6' classpath "org.jetbrains.kotlin:kotlin-gradle- plugin:$kotlin_version" compile "com.android.support:appcompat-v7:$support_version" compile "com.android.support:recyclerview-v7:$support_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir } 

我得到的错误是:

错误:错误:行(13)无法找到类型为org.gradle.api.internal.artifacts.dsl.dependencies的对象上的参数[com.android.support:appcompat-v7:26.0.1]的方法compile()。 DefaultDependencyHandler。 请从Android SDK管理器安装Android支持信息库。 打开Android SDK管理器。

请帮我解决这个错误。

从顶层build.gradle移除compile依赖关系,并将其放在应用程序级别的build.gradle dependencies ,这可以在项目视图的app/src下找到。

 dependencies { compile "com.android.support:appcompat-v7:26.0.1" compile "com.android.support:recyclerview-v7:26.0.1" } 

应用程序级别依赖关系(主要以编译开始)应该放在应用程序级别build.gradle文件下,而不是放在顶级build.gradle

实际上, build.gradle文件是使用Groovy语言compilecompile是一个方法调用。 在Groovy中,我们可以为顶级表达式留下括号 。 这就是为什么你得到一个错误,如:

找不到方法complie()

不能顶级文件中使用这些行

  compile "com.android.support:appcompat-v7:$support_version" compile "com.android.support:recyclerview-v7:$support_version" 

删除它们并在模块文件中使用它们。