使用子项目构建 Android Gradle [英] Android Gradle build with sub projects
问题描述
我目前正在将我们的一个项目从 maven 转换为 Gradle.文件夹结构如下:
I am currently in the process of converting one of our projects to Gradle from maven. The folder structure is as follows:
gitRoot
settings.gradle
build.gradle
ProjectA
build.gradle
src/main/java
Libraries
SomeLib (git submodule)
ProjectBRoot (git submodule)
settings.gradle
build.gradle
ProjectB
build.gradle
src/main/java
Libraries
FacebookSDK/facebook
build.gradle
src
所以它看起来已经很复杂了.但这个想法是 ProjectB 是一个库项目,它应该能够单独构建和打包,这就是为什么它有自己的 settings.gradle 并且据我所知它似乎工作正常,我有它构建并且它发现 facebook 就好了.
So already it looks complicated. But the idea is that ProjectB is a library project and it should be able to be built and packaged separately, which is why it has its own settings.gradle and as far as i can tell it seems to be working ok, i have it building and its finding facebook just fine.
ProjectB/build.gradle 包含这一行
The ProjectB/build.gradle contains this line
compile project(':libraries:facebook-android-sdk:facebook')
ProjectBRoot/settings.gradle 包含这一行
The ProjectBRoot/settings.gradle contains this line
include ':ProjectB', ':libraries:facebook-android-sdk:facebook'
gitRoot/settings.gradle 包含这一行
The gitRoot/settings.gradle contains this line
include ':ProjectA', ':Libraries:ProjectBRoot:ProjectB'
ProjectA/build.gradle 包含这一行
The ProjectA/build.gradle contains this line
compile project(':Libraries:ProjectBRoot:ProjectB')
当我运行构建时出现此错误
When I run the build i get this error
The TaskContainer.add() method has been deprecated and is scheduled to be removed in Gradle 2.0. Please use the create() method instead.
FAILURE: Build failed with an exception.
* Where:
Build file '/gitRoot/Libraries/ProjectBRoot/ProjectB/build.gradle' line: 17
* What went wrong:
A problem occurred evaluating project ':Libraries:ProjectBRoot:ProjectB'.
> Project with path ':libraries:facebook-android-sdk:facebook' could not be found in project ':Libraries:ProjectBRoot:ProjectB'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 4.652 secs
所以我猜想出了什么问题是 facebook 不在 ProjectB 的直接子文件夹中……但这在 ProjectBRoot 中构建时并不重要.这可能是因为我直接引用 ProjectB 而不是通过 ProjectBRoot/gradle.build 引用,但我尝试过,但它也不起作用.有人可以帮我一下吗,我已经浏览了文档,它没有谈论多个具有自己的 settings.gradle 文件的项目,我认为这就是让我感到困惑的事情.
So my guess as to whats wrong is that facebook is not in a direct subfolder from ProjectB...but that doesn't matter when building within ProjectBRoot. This is probably due to the face that I am referencing ProjectB directly and not through the ProjectBRoot/gradle.build but I tried that and it also did not work. Can someone please help me out, I have looked through the documentation and it doesn't talk about multiple projects that have their own settings.gradle files and I think thats the thing that is messing me up.
更新:
所以我遵循了 Xav 的回答,现在我可以使用命令行进行构建,但是我无法使用 android studio 导入/构建.我知道问题仍然出在 facebook 项目上.我得到的错误是它无法配置 ProjectB.
So I followed Xav's answer and I am now able to build with the command line however i can't import/build with android studio. I know the problem is still with the facebook project. The error i get is that it could not configure ProjectB.
Gradle: A problem occurred configuring project ':ProjectA'.
> Failed to notify project evaluation listener.
> A problem occurred configuring project ':Libraries:ProjectBRoot:ProjectB'.
> Failed to notify project evaluation listener.
> Configuration with name 'default' not found.
错误是由线路引起的
compile project(':facebook-sdk')
在 ProjectB/build.gradle 内
inside the ProjectB/build.gradle
推荐答案
settings.gradle 必须定义所有模块.它不会加载树中找到的其他 settings.gradle 以加载更多模块.
settings.gradle must define all the modules. It won't load other settings.gradle found in the tree to load more module.
你必须要么
在顶层 settings.gradle 中为 facebook SDK 定义一个模块.是的,其他 settings.gradle 是多余的.
define a module for the facebook SDK in the top level settings.gradle. Yes it's redundant with the other settings.gradle.
以某种方式发布项目 B(以及它的依赖项,在本例中为 facebook SDK 库),在某处(例如公司工件存储库)并从项目 A 访问它.
publish Project B somehow (as well as its dependencies so in this case the facebook SDK library), somewhere (a corporate artifact repository for instance) and access it from Project A.
虽然#1 更好,但它使 ProjectB -> Facebook 依赖变得棘手,因为路径会根据所使用的 settings.gradle 而有所不同.解决此问题的一种方法是将模块名称/路径与其在磁盘上的实际位置分开.这是在 settings.gradle 文件中完成的.
While #1 is better, it makes the ProjectB -> Facebook dependency tricky as the path will be different depending on the settings.gradle used. One way to fix this is to split the module name/path from its actual location on disk. this is done inside the settings.gradle file.
在你的顶级 settings.gradle 文件中,做
In your top level settings.gradle file, do
include 'facebook-sdk'
project(':facebook-sdk').projectDir = new File('Libraries/ProjectBRoot/Libraries/FacebookSDK/facebook')
在你的项目 B 中的 setting.gradle 文件中,用不同的相对路径做同样的事情:
In the setting.gradle file inside your Project B, do the same with the different relative path:
include 'facebook-sdk'
project(':facebook-sdk').projectDir = new File('Libraries/FacebookSDK/facebook')
这使得两个项目设置都定义了位于磁盘上相同绝对位置的相同facebook-sdk"模块.
This makes both project setup define the same 'facebook-sdk' module located at the same absolute location on disk.
依赖这个模块的所有项目都应该将依赖声明为
All projects depending on this module should just declare the dependency as
compile project(':facebook-sdk')
这篇关于使用子项目构建 Android Gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!