【问题标题】:Android cannot find the method "implementation()"Android 找不到方法“implementation()”
【发布时间】:2021-11-09 17:54:20
【问题描述】:

所以我在 Gradle 文件中实现了一些依赖项,当我尝试同步它时,我得到了

Build file '/home/qwirrr/AndroidStudioProjects/ClockIn/build.gradle' line: 9

A problem occurred evaluating root project 'ClockIn'.
> Could not find method implementation() for arguments [androidx.fragment:fragment:1.3.6] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Gradle 文件如下所示:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        implementation 'androidx.fragment:fragment:1.3.6'
        implementation 'com.google.android.material:material:1.0.0'
        classpath "com.android.tools.build:gradle:7.0.3"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我尝试将implementation 更改为compile,甚至更改类路径行中的Gradle 版本,但没有成功。

我该如何解决这个问题?

注意:我使用的是 Pop_OS! 20.04 LTS 如果重要的话

【问题讨论】:

  • 您正在将依赖项添加到根构建 gradle,将它们添加到您应用的 build.gradle
  • implementation 配置为added by the Java plugin。所以你应该将plugins { id 'java' } 添加到你的build.gradle 的顶部。在buildscript 块中,您只能使用classpath

标签: android gradle


【解决方案1】:

您的依赖项应该进入模块/应用程序的build.gradle,而不是项目的build.gradle

您应用的build.gradle 应如下所示:

plugins {
    id 'com.android.application'
    ...
}

android {
    ...
}

dependencies {
    //all your dependencies goes here
    implementation 'androidx.fragment:fragment:1.3.6'
    implementation 'com.google.android.material:material:1.0.0'
}

唯一应该进入项目的build.gradle 的是类路径。

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 1970-01-01
    • 2019-06-16
    • 2018-05-24
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 2021-01-08
    • 2019-02-16
    相关资源
    最近更新 更多