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') }
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 .
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.
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:
Include the library in settings.gradle and override the projectDir:
include 'MyTestLibProject:MyTestLib' project(':MyTestLibProject:MyTestLib').projectDir = new File(settingsDir, '../MyTestLibProject/MyTestLib')
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'
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')
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With