Gradle and Multi-Project structure

Most of this is pretty normal to the http://www.gradle.org/docs/current/userguide/multi_project_builds.html page. However you are going to need to add

evaluationDependsOn(':project1')
evaluationDependsOn(':project2')

so that gradle will evaluate project1 and project2 before module. In all the projects that contain code you will need to have an empty build.gradle file. This will also allow you to customize a project if needed.

Example: https://github.com/ethankhall/AndroidComplexBuild

Add a build.gradle at the root of your projects. So you need 4 that have useful information in it.

/build.gradle
/settings.gradle
/project1/build.gradle
/project2/build.gradle
/module/build.gradle

in /build.gradle put

dependencies {
    project(":module")
}

in /settings.gradle put

include ':module'
include ':project1', ':project1:A1', ':project1:B1', ':project1:Z1'
include ':project2', ':project2:A2', ':project2:B2', ':project2:Z2'

in /project1/build.gradle put

apply plugin: 'java'

subprojects {
    apply plugin: 'java'

    sourceCompatibility = JavaVersion.VERSION_1_6
    targetCompatibility = JavaVersion.VERSION_1_6

    repositories{
        mavenCentral()
    }   

    //Anything else you would need here that would be shared across all subprojects
}

/project2/build.gradle

buildscript {
    repositories {
        mavenCentral()
    }   

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

subprojects {
    apply plugin: 'android-library'

    android {
        compileSdkVersion 17
        buildToolsVersion "17.0"
    }   

    sourceCompatibility = JavaVersion.VERSION_1_6
    targetCompatibility = JavaVersion.VERSION_1_6

    repositories{
        mavenCentral()
    }   

    //Anything else you would need here that would be shared across all subprojects
}

in /module/build.gradle put

buildscript {
    repositories {
        mavenCentral()
    }   

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

evaluationDependsOn(':project1')
evaluationDependsOn(':project2')

apply plugin: 'android'

android {
    compileSdkVersion 17
    buildToolsVersion "17.0"
}

dependencies {
    compile project(":project1:A1")
    compile project(":project1:B1")
    compile project(":project1:Z1")

    compile project(":project2:A2")
    compile project(":project2:B2")
    compile project(":project2:Z2")
}

Leave a Comment