首页 > 解决方案 > Android 找不到方法“implementation()”

问题描述

所以我在 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 如果重要的话

标签: androidgradle

解决方案


您的依赖项应该进入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项目的是类路径。


推荐阅读