Gradle: how to include an existing, complete project as a subproject?

3.3k views Asked by At

I git clone a complete gradle project "CompleteGradleProjA" from github and include it into my local project as a submodule. By "complete gradle project" I mean that I can go into directory "CompleteGradleProjA" and issue command

cd CompleteGradleProjA && gradle build

to build it.

My directory structure looks like this,

MyProj
  |---CompleteGradleProjA
  |   |---build.gradle
  |
  |---build.gradle

My question is: How can I call "CompleteGradleProjA/build.gradle" without changing anything of it from my root "build.gradle"?

The following root "build.gradle" config does not help.

apply plugin: 'java'
dependencies {
  compile project(':CompleteGradleProjA')
}

I got error message

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':compileJava'.
> Could not determine the dependencies of task ':compileJava'.

"CompleteGradleProjA" is an android porject and "CompleteGradleProjA/build.gradle" looks like this

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}
2

There are 2 answers

0
Volodymyr On BEST ANSWER

CompleteGradleProjA/build.gradle

apply plugin: 'com.android.library'
// if your project isn't library then use this:
// apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion '21.1.2'

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 22
    }
}

dependencies {
    compile 'com.android.support:support-v4:22.2.0' // if needed
}

settings.gradle

include ':CompleteGradleProjA'

Use apply plugin: 'com.android.library' or apply plugin: 'com.android.application' instead of apply plugin: 'java'

0
Mapsy On

To include a custom project as part of your gradle build, first ensure the submodule is already included within the project folder and accessible via your settings.gradle. i.e.

include ':app', ':your_project_name'

You then register the project as a dependency of another by using in your project's build.gradle:

dependencies {
    compile project(path: ':your_project_name')
}

In newer versions compile has been deprecated. See here on what to use.