Managing multiproject dependencies on Android with Gradle
Lately I have been needing to manage a number of dependencies within a multi-project Android application. I borrowed some project setup tips by perusing the Hibernate github repo to streamline dependencies that were shared across multiple sub-projects.
Example Project Structure
RootProject
-- build.gradle
-- project-one
-- build.gradle
-- project-two
-- build.gradle
From this project structure I typically have dependencies that belong in both project-one and project-two. I can put these dependecies in the RootProject and reference them from the sub-projects. This allows me to ensure I get the same version of dependencies across all projects.
In my root project build.gradle file I add the dependencies.
//root-project
ext {
daggerVersion = '1.2.0'
retrofitVersion = '1.2.2'
libs = [
dagger : ('com.squareup.dagger:dagger:' + daggerVersion),
daggerCompiler : ('com.squareup.dagger:dagger-compiler:' + daggerVersion),
retrofit : ('com.squareup.retrofit:retrofit:' + retrofitVersion)
]
}
We can then reference these dependencies in both sub-projects
//project-one
dependencies {
compile libs.dagger
compile libs.daggerCompiler
compile libs.retrofit
}
//project-two
dependencies {
compile libs.dagger
compile libs.daggerCompiler
compile libs.retrofit
}