1

I have a Gradle project which depends on another Gradle project. My project structure is like this:

  • project1
    • build.gradle
    • settings.gradle
    • src/
  • project2
    • build.gradle
    • settings.gradle
    • src/

in project1/build.gradle I want to add project2 as a dependency:

version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

sourceSets {
    main {
        java {
            srcDirs = [ 'src' ]
        }
    }
}

include ':project2'
project(':project2').projectDir = new File("../project2")

dependencies {
    compile project(':project2')
}

Unfortunately, I'm always getting this error message:

Error:(21, 0) Could not find method include() for arguments [:project2] on root project 'project1' of type org.gradle.api.Project.

I'm using Gradle 3.5 and I'm getting this error both on the command line (gradle build) and in IntelliJ. I found a few StackOverflow threads about the same issue (this and this), but they were not helpful.

The Gradle multi-project documentation also doesn't mention any specific requirements which I may be missing that can cause the error.

When I leave the include call out, I get the message that the project path could not be found in the root project.

I also tried moving the dependency to a subdirectory of project1, but without success.

I wonder what I'm doing wrong and why apparently not many other people are having the same problem. I'd be grateful for hints.

Note: this is not an Android project.

Janek Bevendorff
  • 578
  • 1
  • 5
  • 16
  • `include` should be invoked in a separate file named: `settings.gradle` - please give it a try. – Opal May 06 '17 at 14:02
  • Thanks! I suppose that was what I was missing. However, now I'm getting the message that the read-only property projectDir cannot be set. I worked around it by specifying compile project(':..:project2') as the dependency and removing the projectDir assigment. – Janek Bevendorff May 06 '17 at 14:09
  • You can't do that with gradle. Either both projects must be sub-projects of a multi-project build (https://docs.gradle.org/current/userguide/multi_project_builds.html), or you must use a composite build (https://docs.gradle.org/current/userguide/composite_builds.html). – JB Nizet May 06 '17 at 14:25

1 Answers1

9

As pointed out in the first comment, include actually needs to go into settings.gradle and not into build.gradle. The same applies to changing the projectDir property.

Comment 3 gave also me another idea. The project can be included in settings.gradle as follows:

includeBuild '../project2'

and in project1/build.gradle I specify the dependency as

dependencies {
    compile 'group:project2:version'
}

I generally like this better, since it's less code and looks cleaner. The downside, however, is that recursive composite builds aren't possible. So if project2 itself is also a composite build, this won't work.

Janek Bevendorff
  • 578
  • 1
  • 5
  • 16
  • Adding to @Janek's solution, if you don't have group and version then simply add compile ':project2:' – User Dec 09 '19 at 10:00