Gradle and multi-project structure

I'm trying to figure out how I can approach the following project setup:

β”ŒTop Android Project β”‚ β”œβ”€β”€ Project 1 - (Pure Java Modules) β”‚ β”‚ β”‚ β”œβ”€β”€ Module A1 β”‚ β”œβ”€β”€ Module B1 β”‚ : β”‚ └── Module Z1 β”‚ β”œβ”€β”€ Project 2 - (Android Libraries Modules) β”‚ β”‚ β”‚ β”œβ”€β”€ Module A2 β”‚ β”œβ”€β”€ Module B2 β”‚ : β”‚ └── Module Z2 β”‚ └── Module - Actual Android Project 

In the current installation, I have build.gradle in each of the modules, what I really hate about this setting is that all the build.gradle content is duplicated between the modules.

The fact is that in most of them I need the same logic: " Clean Java modules " are all infra modules that I would like to use to output JavaDoc and sources, as well as deploying to some remote repository ( * default).

On the other hand, some modules of β€œ Clean Java modules ” I would like, for example, to have a second plan, in addition to the built-in default * assembly, I would like to deploy a jar of dependencies for a specific project or something like that.

When creating the Actual Android project, I would like the modules to compile in the default assembly * and finally set the default build.gradle for all my Android projects, which are quite a few, and I would not want to duplicate this file.

==================================================== ==============

I suppose I'm looking for something like a parent pom Maven, but since I don’t quite understand how Gradle works, I bring this to you guys to share my thoughts ...

Taking into account that duplication of the same assembly file (I suppose) is unacceptable, because I can change something in the whole assembly logic of all modules

What would be the best approach to handle this kind of settings?

+55
android android-studio build.gradle gradle
Jul 08 '13 at 21:55
source share
2 answers

In most cases, this is normal for the http://www.gradle.org/docs/current/userguide/multi_project_builds.html page. However you will need to add

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

so that gradle evaluates project1 and project2 before the module. In all projects containing code, you will need an empty build.gradle file. It will also allow you to customize the project if necessary.

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

Add build.gradle to the root of your projects. So you need 4 in which there is useful information.

 /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") } 
+55
Jul 09 '13 at 0:33
source share
β€” -

If you have folders matching this structure, you can apply the same build logic to multiple projects.

If your .gradle settings contain

 include ':project2:moduleA2' 

then ': project2' is also a project, and it can have its own build.gradle, in which you can write:

 subprojects { project -> apply plugin 'android-library' // more configuration } 

If you do not apply any plug-in to "project2" itself, then this project will simply not output anything (which you probably want), but in this way you can easily configure its entire subproject.

Then you can also use your entire submodule to determine the logic specific to them.

You can also technically do this in project2 / build.gradle if you want to save everything in one file. You can read http://www.gradle.org/docs/current/userguide/multi_project_builds.html to learn how to configure subprojects from the parent build.gradle file, accessing all subprojects or specific ones, or using filtering.

+8
Jul 08 '13 at 23:38
source share



All Articles