问题描述
我的Eclipse工作区非常简单:
工作区
MyApp
MyApp-AndroidLibrary
当我在MyApp中添加build.gradle文件时,我想引用我的Android库项目:
apply plugin :'android'
dependencies {
编译fileTree(dir:'libs',include:'* .jar')
编译项目(':MyApp-AndroidLibrary')
}
运行gradle build时,出现错误Project with path:MyApp -AndroidLibrary无法在根项目中找到,我为此搜索了一下,发现我需要在工作区目录中设置一个settings.gradle文件,以添加
include:MyApp
include:MyApp-AndroidLibrary
这对我来说太糟糕了,为什么Gradle需要一个settings.gradle文件 ,为什么不提取我在依赖关系中定义的项目?
什么 include
真的意味着什么?如果我在工作区中安装了应用程序和其他共享库,那么结构可能如下所示:
工作区
App1
App2
Library1(由App1和App2使用)
Library2(仅用于App1)
Library3(仅用于App2)
因为只有一个settings.gradle文件,所以我必须将它们全部添加到settings.gradle中。这听起来并不好。
是的,我可以重新组织结构以使Library2成为App1的子目录,而Library3成为App2的子目录,但对于Library1呢?
对此有何评论?
你在问几个不同的问题。这里有一些提示:
-
':MyApp-AndroidLibrary'
是一个逻辑项目路径,根据settings.gradle
中提供的信息映射到物理路径。 - A多项目构建可以具有任意目录结构,该结构在
settings.gradle
中配置。除非需要,否则无需移动目录。 - Eclipse工作区和Gradle构建是单独的边界。您可以在同一个工作区中创建多个构建。
- 使用源代码构建的库时,可以将它们构建为同一个构建的一部分,也可以为它们单独构建。在后一种情况下,您需要确保在应用程序之前构建库,并通过Maven或Ivy存储库交换工件。这可以使用CI服务器和公司的Maven / Ivy存储库自动执行。或者,您可以在您的计算机上手动触发库的构建,并使用本地Maven / Ivy存储库。
有关更多信息,请参阅,特别是关于多项目构建的章节。
I am going to convert my Android projects from Ant to Gradle.
My Eclipse workspace is very simple:
Workspace
MyApp
MyApp-AndroidLibrary
When I add a build.gradle file in MyApp, I want to reference my Android library project:
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
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?
And what include
really means? What if I have anotoher app and some other shared libraries in workspace, the structure may look like this:
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.
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?
Any comment on this?
You are asking several different questions. Here are some hints:
':MyApp-AndroidLibrary'
is a logical project 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.
For more information, check out the Gradle User Guide, especially the chapter on multi-project builds.
这篇关于为什么Gradle需要一个settings.gradle文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!