问题描述
我有4子模块的摇篮 - 基于Android项目 - 两个图书馆和两个应用程序。我试图用的共享code移动的一些/配置到顶级build.gradle文件,以方便各个子模块的build.gradle文件,并使用的子项目{}
作出这样的code可用于每个子模块。
I have an gradle-based Android project with 4 submodules - two libraries and two applications. I was trying to simplify the build.gradle files of each submodule by moving some of of the shared code/configurations to the top level build.gradle file and use subprojects {}
to make that code available to each submodule.
的文件系统结构是这样的:
The file system structure looks like this:
Root
|- App1/
|- App2/
|- Lib1/
|- Lib2/
|- build.gradle
|- settings.gradle
现在的问题是,如果我添加一个安卓{}
部分子项目,然后摇篮任务失败。例如,这是我的顶层build.gradle文件:
The problem is that if I add an android {}
section to the subprojects then gradle tasks fail. For example, this is my top-level build.gradle file:
subprojects { project ->
android {
buildToolsVersion "20.0.0"
}
}
运行摇篮回报这样的:
Running gradle returns this:
出了什么问题: 发生问题的评估根项目机器人。 找不到方法的Android()对根项目'机器人'参数[build_7dngpra6ldok366maq0on77r7e $ _run_closure3_closure5 @ 43d95624。
我搜索了类似的帖子,有些人建议增加行应用插件:'机器人'
每个子项目,以揭露失踪的安卓()
方法摇篮抱怨。然而,这种解决方案并不适合我,因为这将有效地补充该行库项目,需要应用插件:机器人库
而不是
I searched for similar posts and some people suggest adding the line apply plugin: 'android'
to each subproject in order to expose the missing android()
method that gradle is complaining about. However, that solution doesn't work for me because it would effectively add that line to library project, which require apply plugin: 'android-library'
instead.
有没有办法使用安卓{}
里面的子项目{}
,当你在应用程序和库同一个项目?
Is there a way to use android {}
inside of subprojects {}
when you have apps and libraries in the same project?
推荐答案
这是实际的方式限制了Android的摇篮插件的结构并没有记录在the Android的工具网站。
This is actually a limitation on the way that the android-gradle plugin is structured and there is a workaround documented at the android tools website.
如果你有大量的Android的模块,您可能希望避免在所有这些手动设置相同的值。因为你可能有Android和Android的库项目的组合,你无法通过一个子项目封闭应用这些插件。
提出的解决方案是:
...在根项目的build.gradle:
ext {
compileSdkVersion = 19
buildToolsVersion = "19.0.1"
}
在所有的Android模块:
in all the android modules:
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
...
...
一想,我注意到的是,这并不工作在旧版本的摇篮(我试图与1.10,得到了一个错误)。随着摇篮2.1这似乎正常工作,但。
One think I noticed was that this doesn't work on older versions of gradle (I was trying with 1.10 and got an error). With Gradle 2.1 this seems to work correctly though.
这篇关于找不到方法的Android()的参数构建从摇篮Android项目时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!