Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative project dependencies in Gradle?

Tags:

gradle

When specifying Gradle project dependencies, can I avoid using a full absolute project name and use a relative one instead? (i.e. in my example I don't want to explicitly specify :app-a when referencing :domain-a)

//Directory structure app-a/   domain-a/     build.gradle   webapp-a/     build.gradle 

WebApp-A build.gradle:

apply plugin: 'java'  //Build.gradle for webapp-a dependencies {    // Works   compile project(':app-a:domain-a')    //Doesn't work   compile project(projectDir.path + '/../domain-a/')    //Doesn't work   compile findProject('../domain-a')    //Doesn't work   compile project(':domain-a') } 
like image 320
vicsz Avatar asked Mar 16 '12 21:03

vicsz


People also ask

What is project dependency in Gradle?

A project dependency is a special form of an execution dependency. It causes the other project to be built first and adds the jar with the classes of the other project to the classpath. It also adds the dependencies of the other project to the classpath. You can trigger a gradle :api:compile .

What is subproject in Gradle?

subprojects { sourceCompatibility = 11 } It adds the sourceCompatibility property to all sub-projects. This property is then used by the Gradle build Java Plugin: Java version compatibility to use when compiling Java source. Default value: version of the current JVM in use JavaVersion.


1 Answers

For Android development under Android Studio and gradle, here's how to compile a library that isn't under your project's root but has a relative path:

  1. Include the library in settings.gradle and override the projectDir:

    include 'MyTestLibProject:MyTestLib' project(':MyTestLibProject:MyTestLib').projectDir = new File(settingsDir, '../MyTestLibProject/MyTestLib') 
  2. Edit the build.gradle for the library to use the android-library plugin instead of android, this causes the library outputs to be included into projects that reference it:

    apply plugin: 'android-library' 
  3. Specify that your project has a dependency on the lib by editing the build.gradle for the non-lib project:

    compile project(path: ':MyTestLibProject:MyTestLib') 
  4. If the lib includes any jars that your project also includes, the build will see them as a collision when it is generating the apk. Abstract jars into an additional lib that both the project and lib reference, or just exclude the colliding jar from the main project and let it pick up the jar from the dependency:

    dependencies {     compile fileTree(dir: 'libs', include: '*.jar', exclude: '**/android-support-v4.jar')     compile project(path: ':MyTestLibProject:MyTestLib') } 

DISCLAIMER: This isn't how gradle wants you to do this - you're intended to publish the lib using maven and then reference the dependency using the "POM" of the published lib (which includes the version number). This provides better dependency management - for example, maven simplifies the scenario of having many projects (with varying release cycles) all using various versions of a common lib. The relative path technique described here is a quick way to get up and running for small projects or as an interim step for projects being ported from ant.

Reference: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Creating-a-Library-Project

like image 78
J c Avatar answered Sep 20 '22 14:09

J c