找不到Gradle DSL方法:'implementation()'

Moh*_*ani 21 android android-gradle-plugin

我有这个错误

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>
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

build .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'
Run Code Online (Sandbox Code Playgroud)

小智 25

或者如果您不想更新到最新的Gradle,请返回使用DSL方法compile()而不是implementation()

  • compile() 预计 2018 年底被废弃,所以我不会改变它,专注于修复错误 (2认同)

Gab*_*tti 24

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

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

然后在你的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'
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的 gradle-wrapper.properties

distributionUrl=\
  https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip
Run Code Online (Sandbox Code Playgroud)

更详细的信息在这里.


Fl_*_*i4n 20

As a beginner, I discovered that there's 2 build.gradle files

It's worth checking that you didn't add the line in the wrong file. I added mine in the project file whereas I should have added it in the module one.

你可以在这里看到 android 视图中的 2 构建


iyr*_*rin 7

我有一个很简单的原因无法执行此操作,因此我必须发布我的答案以防止其他人对此进行检查。

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

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
Run Code Online (Sandbox Code Playgroud)

凡将注释放在此处的人,都很好。而是将您的存储库和依赖项放在类似命名的模块文件中build.gradle (Module: app)

应该已经有一个dependencies功能。您可能需要添加存储库功能。就我而言,是:

repositories {
    maven { url "https://jitpack.io" }
}
Run Code Online (Sandbox Code Playgroud)

这应该使您同步和识别implementation

  • 我希望这能阻止我经历同样的事情 - 我今天大部分时间都在试图找出问题所在,包括重新安装 Android Studio、摆弄我的设置、安装新的 SDK 等。对我来说,问题是你提到的评论是在“buildscript”大括号内,所以我认为将依赖项放在大括号外可能是可以的。重申一下:请确保将内容放入 *app* 或 *module* 的 `build.gradle` 中 - _而不是_ 项目。 (3认同)