Java 为什么 Gradle 需要 settings.gradle 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20931337/
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
Why does Gradle need a settings.gradle file?
提问by virsir
I am going to convert my Android projects from Ant to Gradle.
我要将我的 Android 项目从 Ant 转换为 Gradle。
My Eclipse workspace is very simple:
我的 Eclipse 工作区非常简单:
Workspace
MyApp
MyApp-AndroidLibrary
When I add a build.gradle file in MyApp, I want to reference my Android library project:
当我在 MyApp 中添加 build.gradle 文件时,我想引用我的 Android 库项目:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':MyApp-AndroidLibrary')
}
When I run gradle build, there is an error "Project with path ':MyApp-AndroidLibrary' could not be found in root project", I googled for this, and found I need to setup a "settings.gradle" file in my workspace directory, to add
当我运行 gradle build 时,出现错误“在根项目中找不到路径为‘:MyApp-AndroidLibrary’的项目”,我用谷歌搜索了这个,发现我需要在我的工作区中设置一个“settings.gradle”文件目录,添加
include ":MyApp"
include ":MyApp-AndroidLibrary"
This looks too bad for me, why Gradle need a settings.gradle file, why not just extract the projects I defined in the dependencies?
这对我来说看起来太糟糕了,为什么 Gradle 需要一个 settings.gradle 文件,为什么不直接提取我在依赖项中定义的项目?
And what include
really means? What if I have anotoher app and some other shared libraries in workspace, the structure may look like this:
什么是include
真正的意思?如果我在工作区中有另一个应用程序和其他一些共享库,结构可能如下所示:
Workspace
App1
App2
Library1(Used by App1 & App2)
Library2(Used only by App1)
Library3(Used only by App2)
Because there is only ONE settings.gradle file, I had to add them all into settings.gradle. That does not smell good.
因为只有一个 settings.gradle 文件,所以我不得不将它们全部添加到 settings.gradle 中。那味道不好闻。
And yes, I can re-organize the strucuture to make Library2 into a child directory of App1, and Library3 to be a child directory of App2, but what about Library1?
是的,我可以重新组织结构,使 Library2 成为 App1 的子目录,而 Library3 成为 App2 的子目录,但是 Library1 呢?
Any comment on this?
对此有何评论?
回答by Peter Niederwieser
You are asking several different questions. Here are some hints:
你在问几个不同的问题。这里有一些提示:
':MyApp-AndroidLibrary'
is a logicalproject path, which gets mapped to a physical path based on information provided insettings.gradle
.- A multi-project build can have an arbitrary directory structure, which is configured in
settings.gradle
. No need to move directories around, unless you want to. - Eclipse workspace and Gradle build are separate boundaries. You can have multiple builds in the same workspace.
- When working with libraries built from source, you can either make them part of the same build, or have a separate build for them. In the latter case, you need to make sure to build the library before the app, and exchange artifacts via a Maven or Ivy repository. This could be automated using a CI server and a corporate Maven/Ivy repository. Alternatively, you could trigger the library's build manually on your machine and use a local Maven/Ivy repository.
':MyApp-AndroidLibrary'
是一个逻辑项目路径,它根据 中提供的信息映射到物理路径settings.gradle
。- 多项目构建可以具有任意目录结构,该结构在
settings.gradle
. 除非您愿意,否则无需移动目录。 - Eclipse 工作区和 Gradle 构建是独立的边界。您可以在同一工作区中进行多个构建。
- 使用从源代码构建的库时,您可以将它们作为同一构建的一部分,也可以为它们单独构建。在后一种情况下,您需要确保在应用程序之前构建库,并通过 Maven 或 Ivy 存储库交换工件。这可以使用 CI 服务器和企业 Maven/Ivy 存储库自动化。或者,您可以在您的机器上手动触发库的构建并使用本地 Maven/Ivy 存储库。
For more information, check out the Gradle User Guide, especially the chapter on multi-project builds.
有关更多信息,请查看Gradle 用户指南,尤其是关于多项目构建的章节。
回答by Joerg
Just in case people (like myself or gladman) still have this problem and couldn't find a solution. With the current version of Gradle you can do this:
以防万一人们(比如我自己或gladman)仍然有这个问题并且找不到解决方案。使用当前版本的 Gradle,您可以执行以下操作:
Move the file settings.gradle
to the directory My-App
and replace the include
by includeFlat 'MyApp-AndroidLibrary'
. This should fix the problem.
将文件移动settings.gradle
到该目录My-App
,并更换include
由includeFlat 'MyApp-AndroidLibrary'
。这应该可以解决问题。
Also compare this questionand see this sectionin the Gradle User Guide for further details.