找不到参数 [com.android.support:appcompat-v7:25.0.0] 的方法 compile()

Could not find method compile() for arguments [com.android.support:appcompat-v7:25.0.0]

正在尝试构建 React Native 项目,但无法编译 appcompat

Could not find method compile() for arguments [com.android.support:appcompat-v7:25.0.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

build.gradle

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

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 21
        compileSdkVersion = 28
        targetSdkVersion = 27
        supportLibVersion = "28.0.0"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile 'com.android.support:support-annotations:25.0.0'
        compile 'com.android.support:design:25.0.0'
    }
}

allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
            url "$rootDir/../node_modules/expo-camera/android/maven"
        }
    }
}


task wrapper(type: Wrapper) {
    gradleVersion = '4.7'
    distributionUrl = distributionUrl.replace("bin", "all")
}

更新 更改为实现收益

ERROR: Could not find method implementation() for arguments [com.android.support:appcompat-v7:25.0.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

gradle 中的 compile 关键字已弃用,请替换为 implementation

尝试用 implementation 替换 compile

implementation 'com.android.support:appcompat-v7:25.0.0'
implementation 'com.android.support:support-annotations:25.0.0'
implementation 'com.android.support:design:25.0.0'

compile 方法已弃用:https://docs.gradle.org/current/userguide/java_library_plugin.html

The compile, testCompile, runtime and testRuntime configurations inherited from the Java plugin are still available but are deprecated. You should avoid using them, as they are only kept for backwards compatibility.

现在可能实际上已从您正在使用的 gradle 版本中删除了它们。

顶级 build.gradle 文件是错误的依赖项位置。它甚至在您的代码的评论中说明:

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

您应该将这些行移动到另一个 build.gradle 文件(在您的模块中)

使用实现而不是编译。 gradle 文件的编译现已取消。

实现使用反编译。

testImplementation use Against testcompile.

runtimeOnly 使用 Against 运行时。

 implementation 'com.android.support:appcompat-v7:25.0.0'
    implementation 'com.android.support:support-annotations:25.0.0'
    implementation 'com.android.support:design:25.0.0'

这些行不会进入build.gradle(Project:Projectname) 这些行将在 build.gradle(Module:app)

   apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.waltonbd.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/raw'] } }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

另一个是 build.gradle(Project:Projectname)。不要在这里输入。

buildscript {
    repositories {
        google()
        jcenter()

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

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

    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

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

对于 third-party build.gradle 有此问题的任何人,您可以使用 patch-package 进行修复。