Gradle DSL方法未找到:'implementation()'。

36

我遇到了这个错误

Error:(45, 0) Gradle DSL method not found: 'implementation()'
Possible causes:<ul><li>The project 'LaTaxi2' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
<a href="fixGradleElements">Upgrade plugin to version 2.3.3 and sync project</a></li><li>The project 'LaTaxi2' may be using a version of Gradle that does not contain the method.
<a href="open.wrapper.file">Open Gradle wrapper file</a></li><li>The build file may be missing a Gradle plugin.
<a href="apply.gradle.plugin">Apply Gradle plugin</a></li>

build.gradle 内容

// Top-level build file where you can add configuration options common to all sub-projects/modules.


buildscript {
    repositories {
        maven {
            url 'https://maven.google.com'
        }
//        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        /* CHANGE to classpath 'com.android.tools.build:gradle:2.3.3'  for STABLE BUILD TOOL VERSION*/
//        classpath 'com.android.tools.build:gradle:3.0.0-alpha7'
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.google.gms:google-services:3.1.0'
        // We recommend changing it to the latest version from our changelog:
        // https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin

        classpath 'io.fabric.tools:gradle:1+'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}


allprojects {
    repositories {
        maven {
            url 'https://maven.google.com'
        }
//        google()
        jcenter()
    }
}

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

构建 .gradle 模块应用程序

apply plugin: 'com.android.application'

apply plugin: 'io.fabric'

repositories {
    maven {
        url 'https://maven.google.com'
    }
//    google()
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.0'
    defaultConfig {
        applicationId "in.techware.lataxi"
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 5
        versionName "1.0.4"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    /* Remove This to remove Crashlytics and Fabric */

    compile('com.crashlytics.sdk.android:crashlytics:2.6.7@aar') {
        transitive = true;
    }
/*    compile('com.digits.sdk.android:digits:2.0.6@aar') {
        transitive = true;
    }*/
    implementation 'com.android.support:appcompat-v7:25.4.0'
    implementation 'com.android.support:design:25.4.0'
    implementation 'com.android.support:recyclerview-v7:25.4.0'
    implementation 'com.squareup.okhttp3:okhttp:3.8.1'
    implementation 'com.android.support:cardview-v7:25.4.0'
    implementation 'com.github.bumptech.glide:glide:3.8.0'
    implementation 'com.google.android.gms:play-services-maps:11.0.2'
    implementation 'com.google.android.gms:play-services-location:11.0.2'
    implementation 'com.google.android.gms:play-services-places:11.0.2'
    implementation 'com.google.firebase:firebase-auth:11.0.2'
    implementation 'com.google.firebase:firebase-messaging:11.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta1'
    implementation 'com.google.code.gson:gson:2.8.0'
    testCompile 'junit:junit:4.12'
}







apply plugin: 'com.google.gms.google-services'

1
对我来说,问题在于我把实现放在了项目的 build.gradle 中,而不是模块应用程序的 build.gradle 中。一旦我改变了它,就可以正常工作了。 - Abhi
9个回答

34

我对于这个问题的失败原因很简单,必须发布我的答案以防止其他人遇到同样的问题。

不要将存储库和依赖项放在名为build.gradle (Project: YourProjectName)的文件中!该文件中有一个注释:

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

谁在里面放了那张便条,做得好。相反,将您的存储库和依赖项放在同名的模块文件build.gradle(Module:app)中。

应该已经有一个dependencies函数。您可能需要添加repositories函数。在我的情况下,它是:

repositories {
    maven { url "https://jitpack.io" }
}

这应该让你同步并识别实现


3
我希望这个问题能够避免我再次犯同样的错误——今天我大部分时间都在尝试找出问题所在,包括重新安装Android Studio、调整设置、安装新的SDK等。对于我来说,问题在于你提到的注释位于buildscript括号内部,因此我认为将依赖项放在括号外面可能是可以的。再次强调:确保将东西放在你的应用程序模块build.gradle中,而不是项目中。 - M_M
这正是我经历过的。值得注意的是,SDK会建议您可能需要更新,即使在我们的情况下这并不是问题所在。 - iyrin
好的。我从Visual Studio转到Android Studio,AS使用“Project”就像VS使用“Solution”一样;而AS使用“Module”就像VS使用“Project”一样...至少这是我所看到的。 - pdschuller
这真的帮了我大忙,我一直忽略了被注释掉的代码行,直到找到了你的答案。谢谢你。 - Eric

29

要使用DSL implementation(),你必须:

  • 使用更新的Android 3.0.0 gradle插件
  • 使用3.4或更高版本的gradle

然后在你的build.gradle中使用:

buildscript {
    repositories {
        ...
        // You need to add the following repository to download the
        // new plugin.
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta1'
    }
}

在你的gradle-wrapper.properties文件中

distributionUrl=\
  https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip

更详细的信息在此处


@MohammadAminMoordani 这是一个独立的问题。它与你的问题无关。你的 build.gradle 文件有问题。你可能需要考虑提出一个单独的问题。 - Gabriele Mariotti

24

如果您不想更新到最新的Gradle版本,可以使用DSL方法compile()代替implementation()


非常感谢,您的解决方案快速且完美地运行。 - Mr Redstoner
2
compile() 预计将在2018年底被弃用,因此我不会更改它,而是专注于修复错误。 - brkeyal

20
作为一个初学者,我发现有两个build.gradle文件。
值得注意的是检查您是否将该行添加到了错误的文件中。我将我的添加到了项目文件中,而应该将其添加到模块文件中。
您可以在Android视图中看到这两个build文件,如下图所示: You can see here the 2 build in the android view

这对我来说是答案。 - Belphegor

4
您正在使用土耳其的工作区,下面的更改可以解决问题。
testImplementation -> testİmplementation,
androidTestImplementation -> androidTestİmplementation
androidTestImplementation -> androidTestİmplementation

3

implementation()已经取代了compile()配置,compile()将在2018年底之前被弃用。

为了修复错误并使用它,您必须升级到Android Gradle Plugin 3.0.0(或更高版本)。由于此更新的影响,您还需要将Gradle更新到Gradle 4.1(或更高版本)。您还需要使用Build Tools 26.0.2(或更高版本),意味着您需要在您的build.gradle中更新您的buildToolsVersion,或者直接从那里完全删除它(因为自3.0.0以来,默认使用最低要求的版本)。您还需要升级到Android Studio 3(或更高版本)才能使用这些高级版本。

您可以在这里阅读有关android gradle插件3.0.0的更改日志、改进的编译时间等内容: https://developer.android.com/studio/releases/gradle-plugin#3-0-0

那么如何更新Android Gradle插件和Gradle?

选项1:手动更新

在您的build.gradle中找到Gradle classpath并将版本更新为Gradle 3.0。此外,应添加google()的maven存储库:

buildscript {
    repositories {
        jcenter()
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
    }
}

在 gradle-wrapper.properties 文件中找到 distributionUrl 并更新为:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

选项2:通过项目属性(但请注意,手动配置将覆盖它)

前往 文件→项目结构→项目

enter image description here

最后还有一件事,您还可以选择是否将compile()替换为implementation()或api()。默认情况下,我建议只使用implementation()。您可以在此阅读更多有关差异的信息: https://developer.android.com/studio/build/dependencies


2
在我的情况下,这只是一个笔误。在其中一行后面多了一个"/"符号。最初的回答。

@TaslimOseni 这没关系。 - Neuron
1
对不起,我以为这是一个模糊的回答,没有仔细阅读它。 - Taslim Oseni

0

在我的情况下,我添加了这些行并解决了问题。

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

0

我遇到了类似的错误。当我尝试使用以下代码从我的build.gradle(Module: app)中包含recyclerview依赖项时。

//other build.gradle(Module: App) code above
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    testCompile 'junit:junit:4.12'
    implementation 'com.android.support:recyclerview-v7:25.3.1'
}


所以我解决这个问题的简单方法是通过更改代码,将实现更改为编译,同时保留其他所有代码及其版本号。
//other build.gradle(Module: App) code above
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:recyclerview-v7:25.3.1'
}

希望这能帮助到遇到类似问题的人。 PS:实际错误是由于implementation 'com.android.support:recyclerview-v7:25.3.1'引起的,将代码更改为compile 'com.android.support:recyclerview-v7:25.3.1'即可解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接