且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Gradle 依赖:通过相对路径编译项目

更新时间:2023-10-05 22:06:46

您可以使用主项目中的settings.gradle"文件包含外部根项目模块.它也必须是一个 gradle 项目,并且在特定的 Android 构建情况下,您必须将每个模块配置为android-library"插件项目.

例如,在 'MyApp' 项目 settings.gradle 中你可以试试这个:

包含应用程序"包括匕首"project(':dagger').projectDir = new File('/Users/foo/workspace/stdlib/dagger')

您的MyApp" build.gradle 必须以相对路径 Gradle 方式反映dagger"模块的需求:

依赖项{编译项目(':dagger')}

就是这样.对您需要的每个外部模块重复此步骤,您将拥有正确的 Gradle 多项目配置.

is it possible to specify a dependency in Gradle (in android studio) to another gradle project outside of the current project boundaries? For example with a relative path something like this:

dependencies {
  compile project('../../stdlib/dagger')
}

So what I trie is something like this:

I have an Android Application. The structure looks like this:

  • MyApp (path is /Users/foo/workspace/MyApp)
    • app (path is /Users/foo/workspace/MyApp/app)

And I have a gradle android library project containing 3 submodules:

  • stdlib (path is /Users/foo/workspace/stdlib)
    • dagger (path is /Users/foo/workspace/stdlib/dagger)
    • utils (path is /Users/foo/workspace/stdlib/utils)
    • http (path is /Users/foo/workspace/stdlib/http)

What I want is to compile the dagger, utils, http module into MyApp project.

The stdlib libraries modules are under heavy development and will grow as MyApp grow. Hence I do not want to push them into a maven repository everytime I make a little change.

So is there a possibility to link other gradle projects somehow? Im looking for a temporarly solution. I will push the std library into maven repository once the source is stable.

Also, as workaround, a solution with sourceSet would be possible. I have also considered to make a libraries folder in MyApp who is a symlink to stdlib, but I didnt get it to work as expected:

  • MyApp (path is /Users/foo/workspace/MyApp)
    • app (path is /Users/foo/workspace/MyApp/app)
    • libraries (symlink to /Users/foo/workspace/stdlib)
 dependencies {
      compile project(':libraries:dagger')
    }

Any idea how to solve such a dependency in gradle?

You can include an outside root project module using 'settings.gradle' file from your main project. It must to be a gradle project too and in the specific Android building situation, you must configure every module as an "android-library" plugin project.

For example, in 'MyApp' project settings.gradle you can try this:

include 'app'
include 'dagger'
project(':dagger').projectDir = new File('/Users/foo/workspace/stdlib/dagger')

Your 'MyApp' build.gradle must reflect the need of the 'dagger' module in a relative path Gradle way:

dependencies {
  compile project(':dagger')
}

And that's it. Repeat this step with every external module you need and you'll have a proper Gradle multi-project configuration.