318

Looked around for this solution for much too long now, and I'm not sure if I missed it or just misstyped something, but my Gradle script will not compile. I am migrating to Gradle, and am very new with it. I am very used to using Maven for dependency management, but Gradle seems best me for now. From running this snippet of code:

dependencies {
  compile group: 'org.bukkit', name: 'bukkit', version: '1.7.9-R0.1-SNAPSHOT'
  compile('io.ibj:MattLib:1.1-SNAPSHOT') {
    exclude group: 'de.bananaco'
    exclude 'net.milkbowl:vault:1.2.27'
  }
  compile group: 'net.citizensnpcs', name: 'citizens', version: '2.0.12'
  compile group: 'com.sk89q', name: 'worldedit', version: '5.6.1'
  compile group: 'com.sk89q', name: 'worldguard', version: '5.9'
  compile group: 'net.milkbowl', name: 'vault', version: '1.2.12'
  compile fileTree(dir: 'libs', includes: ['*.jar'])
}

NOTE: I do have the java, maven, nexus, shadow, and rebel plugins applied.

When I run my Gradle task, I encounter this error:

Could not find method compile() for arguments [[io.ibj:MattLib:1.1-SNAPSHOT], build_1b5iofu9r9krp7o8mme0dqo9l$_run_closure2_closure8@66fb45e5] on root project 'project'

If I remove the MattLib dependency from my project and reinsert it as

compile 'io.ibj:MattLib:1.1-SNAPSHOT'

The script completes, but I have dependency issues. I read up here:

dependencies {
  compile("org.gradle.test.excludes:api:1.0") {
    exclude module: 'shared'
  }
}

(From Chapter 50 From the Gradle Manual)

that what I have SHOULD work, but I am confused why it doesn't.

gradle --version output:

Groovy:       1.8.6
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
Ivy:          2.2.0
JVM:          1.8.0_05 (Oracle Corporation 25.5-b02)
OS:           Windows 7 6.1 amd64
2
  • 3
    Did you ever figure this out? I have the exact same issue. Someone on the discussion forum recommended putting exclude inside a compile block, but whenever I try to do so, I get this exact error.
    – Hakanai
    Jun 21, 2016 at 2:04
  • Hi, Sometimes we need to check gradle-wrapper.properties - distributionUrl whixh I used 7 and reverted back to 6.1 and working fine.
    – Shankar
    Feb 22 at 8:44

9 Answers 9

792

Note that the compile, runtime, testCompile, and testRuntime configurations introduced by the Java plugin have been deprecated since Gradle 4.10 (Aug 27, 2018), and were finally removed in Gradle 7.0 (Apr 9, 2021).

The aforementioned configurations should be replaced by implementation, runtimeOnly, testImplementation, and testRuntimeOnly, respectively.

9
  • 5
    i wish there's some RSS feeds that we can subscribe to stay updated with such updates Nov 24, 2021 at 17:54
  • 1
    @JustKhaithang absolutely not. This is pull not a push problem. Better docs or a better language would be the choices. I'd largely prefer 1.
    – Meshugah
    Dec 8, 2021 at 6:01
  • 1
    @Meshugah , what do you mean by pull or push ? Dec 10, 2021 at 21:07
  • 2
    ggs on just changing the commands without a fallback or documentation entry
    – clockw0rk
    May 5, 2022 at 16:10
  • 1
    i know nobody but the most jnr person works on open source, but this is sososo dumb
    – bharal
    Apr 14 at 16:51
185

Make sure that you are editing the correct build.gradle file. I received this error when editing android/build.gradle rather than android/app/build.gradle.

5
  • 8
    I had it in the wrong place too. Thanks for helping me out!
    – igorrmotta
    Sep 5, 2016 at 3:40
  • 4
    wat do u mean by wrong place?which gradle file am supposed to edit?
    – guru_001
    Nov 25, 2016 at 16:12
  • @guru_001 You are supposed to edit android/app/build.gradle Sep 25, 2017 at 10:08
  • 10
    Why the F.. they named it the same?? May 27, 2019 at 18:13
  • 1
    thank you, felt like a real noob trying to solve this
    – adot
    Feb 15, 2022 at 10:13
124

compile is a configuration that is usually introduced by a plugin (most likely the java plugin) Have a look at the gradle userguide for details about configurations. For now adding the java plugin on top of your build script should do the trick:

apply plugin:'java'
3
  • the order in which this definition goes seems to have an impact on the build; in my case, i faced the issue with the jar() tag and Rene's note helped me resolve the issue. Jul 9, 2016 at 16:29
  • while working with kotlin, had to apply kotlin plugin apply plugin: "kotlin"
    – prayagupa
    Mar 15, 2018 at 3:31
  • My syntax sugar was plugins { id 'java' } Jun 12, 2020 at 11:29
12

It should be exclude module: 'net.milkbowl:vault:1.2.27'(add module:) as explained in documentation for DependencyHandler linked from here because ModuleDependency.exclude(java.util.Map) method is used.

1
  • 1
    Looking back all this time, this is the most likely problem I was having. I have since obviously far moved on, but after getting notifications that this question was still open I think its only proper to indicate this as the 'correct' answer, as it appears to be the closest.
    – Ichbinjoe
    Mar 28, 2018 at 4:10
9

In my case the problem was mismatch in the gradle version. I have installed gradle on mac using

brew install gradle

and got the latest gradle which was 7.0

However when I cloned by project repo and executed the gradle taks it failed with below error

* What went wrong:
A problem occurred evaluating root project 'digital-engineering-course'.
> Could not find method compile() for arguments [org.springframework.boot:spring-boot-starter-web, build_bzpgd6h32w4m8umtmgs76ewog$_run_closure3$_closure8@b55ca3] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

build.gradle file looked pretty normal to me as it has regular dependencies

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    } 
    compile("org.springframework.boot:spring-boot-starter-data-mongodb")    

It took me a while to understand the problem is mismatch of version. Gradle is not able to find the method compile() because I was using gradle 7.0 in my bash.

And the project was supposed to be ran with gradle 4.8 (Actually gradle wrapper was to be used, but that was breaking for another interesting issue Could not find or load main class org.gradle.wrapper.GradleWrapperMain (If interested please follow this for details)

The reason for failure is compile is that the compile, runtime, testCompile, and testRuntime configurations introduced by the Java plugin have been deprecated since Gradle 4.10, and were finally removed in Gradle 7.0.

So, to solve the problem I had to install the lower version of gradle. If you want to manage multiple version of gradle use sdkman (earlier known as gvm)

Installation on macOs / linux is as simple as executing below

curl -s "https://get.sdkman.io" | bash

Once done use

sdk list gradle

It will list out all the available versions of the gradle. As per your need install and use. for e.g

sdk install gradle 4.8 (this will choose the 4.8 by default in current shell) sdk use gradle 4.8 (if already installed, this is suffice to switch between gradle version)

And now the build.gradle was able to compile and execute the task.

8

In my case, all the compile statements has somehow arranged in a single line. separating them in individual lines has fixed the issue.

3
  • yes if we modify gradle properties from module settings dialog it happen Jun 3, 2017 at 20:16
  • True. Damn gradle.build file generator removes newline separators in dependencies block after some manipulations. Jul 17, 2017 at 6:50
  • oh really? come on google what a bug, it removed all my new lines when i woke up and hit the run button! this is really a shit man.. Nov 27, 2017 at 4:34
6

Add the dependency to your project-level build.gradle:

classpath 'com.google.gms:google-services:3.0.0'

Add the plugin to your app-level build.gradle:

apply plugin: 'com.google.gms.google-services'

app-level build.gradle:

dependencies {
        compile 'com.google.android.gms:play-services-auth:9.8.0'
}
1
  • 4
    What does this have to do with the question?
    – aarnaut
    Oct 16, 2017 at 13:20
0

In my case I had to remove some files that were created by gradle at some point in my study to make things work. So, cleaning up after messing up and then it ran fine ...

If you experienced this issue in a git project, do git status and remove the unrevisioned files. (For me elasticsearch had a problem with plugins/analysis-icu).

Gradle Version : 5.1.1

-1

Just for the record: I accidentally enabled Offline work under Preferences -> Build,Execution,Deployment -> Gradle -> uncheck Offline Work, but the error message was misleading

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