david.mihola
david.mihola

Reputation: 12992

`rootProject` in a Gradle module that's imported to another project

In my current setup I have two top-level Gradle projects:

A library project VideoPresenter with modules

where both videopresenter-exoplayer and videopresenter-visualon depend on videopresenter-common.

All three of the modules depend on OkHttp so I defined a version variable in my top-level build.gradle:

ext {
    okhttp = 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
}

which I use in the three other build.gradles:

dependencies {
    compile rootProject.ext.okhttp
}

So far, this is completely analogous to the way, for example, RxBinding is set up. And it seems to work as long as I compile the modules from within this project.

However, I also have an application project that uses one or more of these modules. Let's say the settings.gradle of that project includes the following:

include ':videopresenter-common'
include ':videopresenter-exoplayer'
project(':videopresenter-common').projectDir = new File('../VideoPresenterAndroid/videopresenter-common')
project(':videopresenter-exoplayer').projectDir = new File('../VideoPresenterAndroid/videopresenter-exoplayer') 

Now, when I try to compile the application project Gradle complains because it

Cannot get property 'okhttp' on extra properties extension as it does not exist

presumably because rootProject now points to the top-level build.gradle of my application project.

If I add the property there the project compiles. However, I don't want to have to "inject" the correct version number from the main project into the library project. Is there a way to centrally declare the property in the library project so that it also applies when the module is imported into another project?

Upvotes: 0

Views: 1495

Answers (1)

JBirdVegas
JBirdVegas

Reputation: 11403

If the goal is to get the value from the library then you could just get the library project's rootProject then reference the ext like before. Being specific about what project we are looking for should provide the expected result.

dependencies {
    compile project(':videopresenter-common').rootProject.ext.okhttp
}

As long as the project is in the settings.gradle you should be able to reference it's extension.

Upvotes: 1

Related Questions