从 gradle 构建 Android 项目时,找不到参数的方法 android()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26243480/
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
Could not find method android() for arguments when building Android project from gradle
提问by Mike Laren
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.
我有一个基于 gradle 的 Android 项目,它有 4 个子模块——两个库和两个应用程序。我试图通过将一些共享代码/配置移动到顶级 build.gradle 文件并用于subprojects {}
使该代码可用于每个子模块来简化每个子模块的 build.gradle 文件。
The file system structure looks like this:
文件系统结构如下所示:
Root
|- App1/
|- App2/
|- Lib1/
|- Lib2/
|- build.gradle
|- settings.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:
问题是,如果我android {}
向子项目添加一个部分,那么 gradle 任务就会失败。例如,这是我的顶级 build.gradle 文件:
subprojects { project ->
android {
buildToolsVersion "20.0.0"
}
}
Running gradle returns this:
运行 gradle 返回这个:
What went wrong: A problem occurred evaluating root project 'android'. Could not find method android() for arguments [build_7dngpra6ldok366maq0on77r7e$_run_closure3_closure5@43d95624] on root project 'android'.
出了什么问题:评估根项目“android”时出现问题。在根项目“android”上找不到参数 [build_7dngpra6ldok366maq0on77r7e$_run_closure3_closure5@43d95624] 的方法 android()。
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.
我搜索了类似的帖子,有些人建议将这一行添加apply plugin: 'android'
到每个子项目中,以暴露android()
gradle 抱怨的缺失方法。但是,该解决方案对我不起作用,因为它会有效地将该行添加到库项目中,而后者需要apply plugin: 'android-library'
。
Is there a way to use android {}
inside of subprojects {}
when you have apps and libraries in the same project?
当您在同一个项目中拥有应用程序和库时,有没有办法在android {}
inside 中使用subprojects {}
?
回答by
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-gradle 插件结构方式的限制,并且在android 工具网站上记录了一个解决方法。
If you have a lot of Android modules, you may want to avoid manually setting the same values in all of them. Because you probably have a mix of android and android-library project you can't apply these plugins through a subprojects closure.
如果您有很多 Android 模块,您可能希望避免在所有模块中手动设置相同的值。因为您可能混合使用 android 和 android-library 项目,所以您无法通过子项目关闭来应用这些插件。
The proposed solution is:
建议的解决方案是:
...in the root project's build.gradle:
ext { compileSdkVersion = 19 buildToolsVersion = "19.0.1" }
in all the android modules:
android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion }
...
...在根项目的 build.gradle 中:
ext { compileSdkVersion = 19 buildToolsVersion = "19.0.1" }
在所有 android 模块中:
android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion }
...
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.
我注意到的一个想法是,这不适用于旧版本的 gradle(我尝试使用 1.10 并出现错误)。不过,在 Gradle 2.1 中,这似乎可以正常工作。
回答by Sonny
There's one minor issue with user4118620's answer... at least on Gradle 2.10 when I used it. In order to have the key visible to the android modules. We need to place the ext under the subproject scope
user4118620 的回答有一个小问题……至少在我使用 Gradle 2.10 时是这样。为了让密钥对 android 模块可见。我们需要将 ext 放在子项目范围内
At the root gradle file you need to add the following
在根 gradle 文件中,您需要添加以下内容
subprojects {
ext{
<android_compile_sdk_key> = <sdk_value>
<android_build_key> = <build_value>
}
}
At each of the submodules you then simply follow what he mentioned earlier
在每个子模块中,您只需按照他之前提到的内容进行操作
android {
compileSdkVersion <android_compile_sdk_key>
buildToolsVersion <android_build_key>
}
You can find the docs about the gradle multibuild on the following link
您可以在以下链接中找到有关 gradle multibuild 的文档
https://docs.gradle.org/current/userguide/multi_project_builds.html#sub:adding_specific_behavior
https://docs.gradle.org/current/userguide/multi_project_builds.html#sub:adding_specific_behavior
回答by Parth Gupta
You can use afterEvaluate
您可以使用 afterEvaluate
afterEvaluate {
subprojects { project ->
android {
buildToolsVersion "20.0.0"
}
}
}