Android Studio/Kotlin : Gradle error - cannot find compile() method for arguments [com. android.support:recyclerview-v7:$support_version]

标签 android android-gradle-plugin kotlin

我使用的是 Android Studio 3.0 Canary 6。我在处理 build.gradle(项目)文件时遇到了一些问题。

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
    } 

我得到的错误是:

Error:Error:line (13)Could not find method compile() for arguments [com.android.support:appcompat-v7:26.0.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. Please install the Android Support Repository from the Android SDK Manager. Open Android SDK Manager.

请帮我解决这个错误。

最佳答案

从顶级 build.gradle 中删除 compile 依赖项,并将其放入您的应用程序级别 build.gradle dependencies,在项目 View 的 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语言编写的,compile是一个方法调用。在 Groovy 中,我们可以 leave the parenthesis for top-level expressions .这就是为什么您会收到如下错误:

Could not find method complie()

关于Android Studio/Kotlin : Gradle error - cannot find compile() method for arguments [com. android.support:recyclerview-v7:$support_version],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45783601/

相关文章:

java - FirebaseRecyclerAdapter getItemCount = 0

android - 将收件箱和发送短信与所有短信区分开来

java - 在 Android 1.5 中删除短信

Android Studio - 找不到 native 方法 - 在构建中包含 .so 文件

kotlin - 为什么使用@singleton而不是仅使用对象

android - findViewById 在调用 setContentView 后返回 null

android - 升级到Android Studio 2.0后获取java.lang.VerifyError

Android Studio gradle 从服务器收到状态代码 400 : Bad Request

kotlin - Gradle 。 block 插件中的自定义函数{}

java - Jackson 的 @JsonAppend 具有默认值