在当前设置中,我有两个顶级Gradle项目:
带模块的库项目VideoPresenter
videopresenter-common
videopresenter-exoplayer
videopresenter-visualon
其中
videopresenter-exoplayer
和videopresenter-visualon
都取决于videopresenter-common
。这三个模块都依赖于OkHttp,因此我在顶层
build.gradle
中定义了一个版本变量:ext {
okhttp = 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
}
我在其他三个
build.gradle
中使用了它:dependencies {
compile rootProject.ext.okhttp
}
到目前为止,这完全类似于创建RxBinding的方式。只要我从该项目中编译模块,它就似乎可以正常工作。
但是,我也有一个使用这些模块中一个或多个的应用程序项目。假设该项目的
settings.gradle
包含以下内容:include ':videopresenter-common'
include ':videopresenter-exoplayer'
project(':videopresenter-common').projectDir = new File('../VideoPresenterAndroid/videopresenter-common')
project(':videopresenter-exoplayer').projectDir = new File('../VideoPresenterAndroid/videopresenter-exoplayer')
现在,当我尝试编译应用程序项目时,Gradle抱怨是因为
大概是因为
rootProject
现在指向我的应用程序项目的顶级build.gradle
。如果我在此处添加属性,则将编译该项目。但是,我不想从主项目中将正确的版本号“注入(inject)”到库项目中。有没有一种方法可以在库项目中集中声明该属性,以便将该模块导入另一个项目时也可以应用?
最佳答案
如果目标是从库中获取值,则可以只获取库项目的rootProject
,然后像以前一样引用ext
。具体说明我们正在寻找的项目应能提供预期的结果。
dependencies {
compile project(':videopresenter-common').rootProject.ext.okhttp
}
只要项目在
settings.gradle
中,您就应该能够引用它的扩展名。