157

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

6
  • 6
    Are you using Android Studio 3.0? Aug 10, 2017 at 13:45
  • 1
    I am new to android. But Yes, I updated to 3.0 as previous error was complaining about it.
    – cole
    Aug 10, 2017 at 13:46
  • 3
    Nope android studio 2.3.3
    – cole
    Aug 10, 2017 at 13:47
  • 11
    Try replacing implementation with compile Aug 10, 2017 at 13:50
  • 1
    What gradle version do you use? Although using compile will still work for now, keep in mind that it is deprecated and should no longer be used.
    – donfuxx
    Feb 6, 2018 at 3:57

17 Answers 17

176

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

6
  • 18
    Confirmed. This is the correct answer. The error message as described above shows when adding dependencies to the wrong build.gradle file. Mar 24, 2020 at 10:58
  • @FrankKrumnow Cannot confirm because, there could be other reasons as well. Jun 26, 2020 at 7: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 3, 2020 at 23:04
  • 1
    this is the answer for me
    – beginners
    Nov 24, 2022 at 6: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 9 at 19:49
96

Replace compile with implementation.

compile was recently deprecated and replaced by implementation or api

5
  • 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) Sep 17, 2019 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 Feb 13, 2020 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 Mar 24, 2020 at 11:00
  • @GaVaRaVa any luck at your side?
    – Moeez
    Apr 27, 2021 at 10:12
  • Although this is a correct statement it doesn't help for this problem. Feb 4 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.

2
  • 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 7, 2021 at 9:53
  • @birch3: add on top of build.gradle: plugins { id 'java-library' }
    – Sabrina
    Feb 16, 2022 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

9
  • 2
    I'm already on Gradle 4.4, still having this issue suddently. Feb 16, 2018 at 19:00
  • 1
    And what is your version of the gradle plugin? @InfiniteLoops
    – donfuxx
    Feb 23, 2018 at 2:05
  • the plugins version is 171.4249.39. tools.build:Gradle version is 3.0.1 Feb 23, 2018 at 7:05
  • In Android Studio, if you create a new project from scratch: Does its gradle file use implementation or compile? @InfiniteLoops
    – donfuxx
    Mar 3, 2018 at 3:58
  • 1
    For me, the difference was milliseconds. Why mess with something when it's not broken? Apr 7, 2018 at 22:48
18

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

java-library-plugin

2
  • 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, 2019 at 20:45
  • 1
    This is what helped me with Gradle 7.2 Dec 28, 2021 at 3:49
17

For me I put my dependencies in the wrong spot.

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

dependencies {
 //Put them here
}
13

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

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

2
  • 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, stackoverflow.com/a/49755251/4132182
    – ZenVentzi
    Sep 9, 2019 at 12:44
  • This is not a solution! changing compile to implementation only applies to dependencies.
    – Jorgesys
    May 5, 2020 at 21:01
4

Your Code

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

Replace it By

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
1
  • 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. Jan 16, 2021 at 19:32
4

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

1
  • Yep, this solved it for me on Android Studio 4.2.2 Aug 8, 2021 at 0:36
3

Replace your implementation with classpath. That should work.

2
  • Hi Nander, I don't think my answer is similar to the accepted one. Besides, I did not work for me, which is why I posted what worked and could help other developers too. Mar 29, 2020 at 9:27
  • Hi Edward, sorry, I misread. Your answer is indeed different. Mar 30, 2020 at 12:26
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.

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.

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.

1

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

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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.