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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24975745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:07:41  来源:igfitidea点击:

Gradle dependencies: compile project by relative path

javaandroidgradleandroid-studio

提问by sockeqwe

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:

是否可以在 Gradle(在 android studio 中)指定当前项目边界之外的另一个 gradle 项目的依赖项?例如,相对路径是这样的:

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

So what I trie is something like this:

所以我尝试的是这样的:

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

我有一个 Android 应用程序。结构如下所示:

  • MyApp (path is /Users/foo/workspace/MyApp)
    • app (path is /Users/foo/workspace/MyApp/app)
  • MyApp(路径是/Users/foo/workspace/MyApp)
    • 应用程序(路径是/Users/foo/workspace/MyApp/app)

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

我有一个包含 3 个子模块的 gradle android 库项目:

  • 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)
  • stdlib(路径为 /Users/foo/workspace/stdlib)
    • 匕首(路径是/Users/foo/workspace/stdlib/dagger)
    • utils(路径是/Users/foo/workspace/stdlib/utils)
    • http(路径为 /Users/foo/workspace/stdlib/http)

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

我想要的是将 dagger、utils、http 模块编译到 MyApp 项目中。

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.

stdlib 库模块正在大量开发中,并且会随着 MyApp 的增长而增长。因此,我不想每次做一点改动时都将它们推送到 Maven 存储库中。

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.

那么是否有可能以某种方式链接其他 gradle 项目?我正在寻找临时解决方案。源稳定后,我会将 std 库推送到 maven 存储库中。

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:

此外,作为解决方法,使用 sourceSet 的解决方案是可能的。我还考虑在 MyApp 中创建一个库文件夹,它是 stdlib 的符号链接,但我没有让它按预期工作:

  • MyApp (path is /Users/foo/workspace/MyApp)
    • app (path is /Users/foo/workspace/MyApp/app)
    • libraries (symlink to /Users/foo/workspace/stdlib)
  • MyApp(路径是/Users/foo/workspace/MyApp)
    • 应用程序(路径是/Users/foo/workspace/MyApp/app)
    • 库(符号链接到 /Users/foo/workspace/stdlib)
 dependencies {
      compile project(':libraries:dagger')
    }
 dependencies {
      compile project(':libraries:dagger')
    }

Any idea how to solve such a dependency in gradle?

知道如何解决 gradle 中的这种依赖性吗?

采纳答案by Martin Revert

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.

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

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

例如,在“MyApp”项目 settings.gradle 中,您可以尝试以下操作:

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:

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

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.

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