找不到参数 [DefaultProjectDependency{dependencyProject='project ':app'', configuration='default'}] 的方法实现()

Could not find method implementation() for arguments [DefaultProjectDependency{dependencyProject='project ':app'', configuration='default'}]

我正在尝试将 Tensorflow 与 Android Studio 集成。我创建了模块和所有库依赖项,但我不断收到此错误:

构建文件“/home/User/AndroidStudioProjects/MyApp/tensorflow-android-1.13.1/build.gradle”行:4 评估项目 ':tensorflow-android-1.13.1'.

时出现问题

Could not find method implementation() for arguments [DefaultProjectDependency{dependencyProject='project ':app'', configuration='default'}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

同时,如果我尝试使用 compile 而不是 implementation(),它已经被弃用了。我的 build.gradle 项目文件是:

// 顶级构建文件,您可以在其中添加所有通用的配置选项 sub-projects/modules。

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

buildscript {

    repositories {
        google()
        jcenter()
        maven { url "https://chaquo.com/maven" }

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.2'
        implementation'org.tensorflow:tensorflow-android:+'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath "com.chaquo.python:gradle:7.0.2"
    }
}

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

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

通常,这表示缺少 javajava-library 插件,或 kotlin 等效插件。您的 gradle 文件中是否有类似的内容?

如果没有,请尝试在依赖项上方添加以下块,根据您的开发语言选择 java 或 kotlin 行。

plugins {
    id 'java'
    kotlin("jvm") version "1.3.70"
}

此外,根据您提供的最新信息,我认为问题来自以下行:

implementation'org.tensorflow:tensorflow-android:+'

据我所知,buildscript 依赖项仅对 gradle 插件有用,对代码依赖项没有用。此外,在 buildscript 下的 dependencies 块中,您只能将插件添加到类路径,然后您将使用 apply.

应用它

但是,回到手头的问题,尝试对文件进行以下更改:


buildscript {

    repositories {
        google()
        jcenter()
        maven { url "https://chaquo.com/maven" }

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath "com.chaquo.python:gradle:7.0.2"
    }
}

plugins {
    id 'java'
    kotlin("jvm") version "1.3.70"
}
allprojects {
    apply plugin: 'java'
    apply plugin: 'kotlin'

    repositories {
        google()
        jcenter()
       // maven{
       //     url 'https://maven.google.com/'
       //     name 'Google'
       // }
    }
    dependencies {
        implementation'org.tensorflow:tensorflow-android:+'
    }

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

Let me know if it helps. You might have to chose between `java` and `kotlin` based on your needs.