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

Trying to build a react native project, and cannot compile 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")
}

UPDATE changing to implementation yields

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


Solution 1:[1]

enter image description hereUse implementation rather than compile. Compile is now depricated for gradle file.

implementation use Against compile.

testImplementation use Against testcompile.

runtimeOnly use Against runtime.

 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'

These line will not enter in build.gradle(Project:Projectname) These lines will be under 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'
}

Another one is build.gradle(Project:Projectname). Don't enter here.

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
}

Solution 2:[2]

The compile keyword in gradle has been deprecated, replace with implementation instead.

Solution 3:[3]

Top-level build.gradle file is the wrong place for dependencies. It's even stated in a comment in your code:

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

You should move these lines to another build.gradle file (in your module)

Solution 4:[4]

Try replacing compile with implementation:

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'

The compile method was deprecated: 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.

It is possible that they are now actually removed from the version of gradle you are using.

Solution 5:[5]

For anyone who has this problem with the third-party build.gradle, you can use patch-package to do a patch for the fix.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 MichaelStoddart
Solution 3 Sergey Glotov
Solution 4 Ben P.
Solution 5 Heaven