155

I am trying to open existing android project in android studio and it gradle cannot build the app without the error

Error android studio keeps on throwing

Error:(74, 1) A problem occurred evaluating project ':app'.
> Could not find method implementation() for arguments 
[com.android.support:appcompat-v7:26.0.0] on object of type 
org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

My Code in build.gradle Which can help to understand my issue My dependencies

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')

// google & support
implementation "com.android.support:appcompat-v7:$supportVersion"
implementation "com.android.support:cardview-v7:$supportVersion"
implementation "com.android.support:recyclerview-v7:$supportVersion"
implementation "com.android.support:design:$supportVersion"
implementation "com.android.support:palette-v7:$supportVersion"
implementation "com.android.support:customtabs:$supportVersion"
implementation "com.android.support:support-v4:$supportVersion"
implementation 'com.google.android.exoplayer:exoplayer:r2.0.4'

// utils
implementation 'com.github.bumptech.glide:glide:4.0.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'
implementation 'com.koushikdutta.ion:ion:2.1.7'
implementation 'com.github.Commit451:bypasses:1.0.4'
implementation 'com.jakewharton:butterknife:8.8.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.0'
implementation 'com.drewnoakes:metadata-extractor:2.9.1'
implementation "com.orhanobut:hawk:2.0.1"

}

Please help to solve the issue

cole
  • 3,147
  • 3
  • 15
  • 28

17 Answers17

174

Make sure you're adding these dependencies in android/app/build.gradle, not android/build.gradle.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Sunil Kumar
  • 6,112
  • 6
  • 36
  • 40
  • 18
    Confirmed. This is the correct answer. The error message as described above shows when adding dependencies to the wrong build.gradle file. – FrankKrumnow Mar 24 '20 at 10:58
  • @FrankKrumnow Cannot confirm because, there could be other reasons as well. – Sreekanth Karumanaghat Jun 26 '20 at 07:12
  • 1
    This was also my issue, after I'd spent 2 days trying to fix it. The dependencies used to live in android/build.gradle in previous versions of Cordova so this was very confusing for me. – SigmaSteve Sep 03 '20 at 23:04
  • 1
    this is the answer for me – beginners Nov 24 '22 at 06:57
  • 3
    Absolutely ridiculous that Google's own documentation doesn't mention that there are multiple build.gradle files in the project, or specify which one should be used. – Amoliski May 09 '23 at 19:49
  • I'm so glad someone decided to name so many different files with the same name ;) That's not going to be confusing. – steve May 23 '23 at 14:47
95

Replace compile with implementation.

compile was recently deprecated and replaced by implementation or api

Blue Jones
  • 385
  • 1
  • 9
Saurabh Thorat
  • 18,131
  • 5
  • 53
  • 70
  • 69
    The title says 'Could not find method implementation()' so using 'implementation' instead of 'compile' will not help (unless the error message is completely wrong) – Mario Klebsch Sep 17 '19 at 10:37
  • 11
    I am already using "implementation" for an ivy module which is present in our Artifactory repository. The answer does not help – GauravEdekar Feb 13 '20 at 11:13
  • 4
    The fact with deprication is correct but does NOT lead to the errormessage displayed in the header. Look up Sunil Kumar answer: you are editing the wrong file. If confirmed please downvote this answer as it is misleading. TY – FrankKrumnow Mar 24 '20 at 11:00
  • @GaVaRaVa any luck at your side? – Moeez Apr 27 '21 at 10:12
  • Although this is a correct statement it doesn't help for this problem. – John LaBarge Feb 04 '23 at 18:52
59

Make sure your Gradle version is 3.*.* or higher before using "implementation".

Open the project level Gradle file under dependencies:

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

Open the 'gradle-wrapper.properties' file and set the distributionUrl:

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

or latest version.

Sync the project. I Hope this solves your problem.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
krishnamurthy
  • 1,574
  • 14
  • 14
  • I'm using version below 3.*.* and using 'classpath' still results to the same error. I used 'compile' instead. Thanks for this answer though about the version. – birch3 Dec 07 '21 at 09:53
  • @birch3: add on top of build.gradle: `plugins { id 'java-library' }` – Sabrina Feb 16 '22 at 16:00
28

You need to use at least Gradle 3.4 or newer to be able to use implementation. It is not recommended to keep using the deprecated compile since this can result in slower build times. For more details see the official android developer guide:

When your module configures an implementation dependency, it's letting Gradle know that the module does not want to leak the dependency to other modules at compile time. That is, the dependency is available to other modules only at runtime. Using this dependency configuration instead of api or compile can result in significant build time improvements because it reduces the amount of projects that the build system needs to recompile. For example, if an implementation dependency changes its API, Gradle recompiles only that dependency and the modules that directly depend on it. Most app and test modules should use this configuration.

https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations

Update: compile will be removed by end of 2018, so make sure that you use only implementation now:

Warning:Configuration 'compile' is obsolete and has been replaced with 'implementation'. It will be removed at the end of 2018

donfuxx
  • 11,277
  • 6
  • 44
  • 76
18

change apply plugin: 'java' to apply plugin: 'java-library'

java-library-plugin

Community
  • 1
  • 1
xiqing lau
  • 211
  • 2
  • 3
  • 1
    Although the accepted answer appears to be the most popular, it is not the only correct answer. In my case, it was the absence of the "apply plugin: 'java-library" which resulted in the exact same exception (different object, of course). So, thanks to @xiqing-lau for adding this possible solution. – pfurbacher Dec 17 '19 at 20:45
  • 1
    This is what helped me with Gradle 7.2 – Andriy Zhuk Dec 28 '21 at 03:49
17

For me I put my dependencies in the wrong spot.

buildscript {
  dependencies {
    //Don't put dependencies here.
  }
}

dependencies {
 //Put them here
}
Mr. Arbitrary
  • 932
  • 11
  • 25
12

Solved this by adding my dependancies in the app folder

Go to: app -> Gradle Scripts -> gradle.build(Module: app) and add the dependencies in that file and not the global build.gradle file

Add your dependency in app/build.gradle

אורי orihpt
  • 2,358
  • 2
  • 16
  • 41
DereckChamboko
  • 187
  • 2
  • 6
5

So ridiculous, but I still wanna share my experience in case of that someone falls into the situation like me.

Please check if you changed: compileSdkVersion --> implementationSdkVersion by mistake

Nguyen Tan Dat
  • 3,780
  • 1
  • 23
  • 24
  • 1
    "By mistake" meaning that we're *NOT* supposed to change it from compileSdk.. to implementationSdk.. right? That it is supposed to stay the compileSdkVersion? --- Anyone reading this comment the answer is - Yes, https://stackoverflow.com/a/49755251/4132182 – ZenVentzi Sep 09 '19 at 12:44
  • This is not a solution! **changing compile to implementation only applies to dependencies**. – Jorgesys May 05 '20 at 21:01
4

Your Code

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

Replace it By

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
Saurabh kumar
  • 51
  • 1
  • 1
  • 5
  • Thanks! This is the only suggestion that actually worked for me. I was using compile and the project actually compiled, but then the classes on the AAR were not available at runtime inside the app. This form of implementation solved both problems. – VictorEspina Jan 16 '21 at 19:32
4

I moved implementation to module-level build.gradle from root-level build.gradle. It solves the issue.

Chandler
  • 346
  • 3
  • 12
3

Replace your implementation with classpath. That should work.

2

As mentioned here, https://stackoverflow.com/a/50941562/2186220, use gradle plugin version 3 or higher while using 'implementation'.

Also, use the google() repository in buildscript.

buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
    }
}

These changes should solve the issue.

Bot
  • 622
  • 2
  • 10
  • 21
2

As suggested in official docs you need add these :

buildscript {
    repositories {
        // Gradle 4.1 and higher include support for Google's Maven repo using
        // the google() method. And you need to include this repo to download
        // Android Gradle plugin 3.0.0 or higher.
        google()
        ...
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.0'
    }
}

adding these remove my error. Also use implementation instead of compile.

Ali Yar Khan
  • 1,231
  • 2
  • 11
  • 33
2

For me the problem was that I wrote:

android {
    compileSdk 31
…

Instead of:

android {
    compileSdkVersion 31

So make sure your build.gradle is correct.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Ramasie
  • 21
  • 1
1

In my case it was because I used Implementation instead of implementation.

Bouh
  • 1,303
  • 2
  • 10
  • 24
0

If implementation is not defined, you are writing on a wrong file. On Unity 2019+ the correct file is main template grandle and not some of the others.

George T
  • 169
  • 1
  • 1
  • 9
0

in my case, i was just missing apply plugin: 'java'

in general, make sure your gradle version is >7 for using implementation and that you are in the correct project