从 gradle 构建 Android 项目时找不到参数的方法 android() [英] Could not find method android() for arguments when building Android project from gradle

查看:20
本文介绍了从 gradle 构建 Android 项目时找不到参数的方法 android()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 gradle 的 Android 项目,其中包含 4 个子模块 - 两个库和两个应用程序.我试图通过将一些共享代码/配置移动到顶级 build.gradle 文件并使用 subprojects {} 使该代码可供每个子模块使用,从而简化每个子模块的 build.gradle 文件子模块.

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.

文件系统结构如下:

Root
 |- App1/
 |- App2/
 |- Lib1/
 |- Lib2/
 |- build.gradle
 |- settings.gradle

问题是,如果我将 android {} 部分添加到子项目中,那么 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"
  }
}

运行 gradle 会返回:

Running gradle returns this:

出了什么问题:评估根项目android"时出现问题.在根项目android"上找不到参数 [build_7dngpra6ldok366maq0on77r7e$_run_closure3_closure5@43d95624] 的方法 android().

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'.

我搜索了类似的帖子,有些人建议将 apply plugin: 'android' 行添加到每个子项目中,以公开 gradle 缺少的 android() 方法正在抱怨.但是,该解决方案对我不起作用,因为它会有效地将该行添加到库项目中,这需要 apply plugin: 'android-library' 代替.

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.

当您在同一个项目中有应用程序和库时,有没有办法在 subprojects {} 中使用 android {}?

Is there a way to use android {} inside of subprojects {} when you have apps and libraries in the same project?

推荐答案

这实际上是对 android-gradle 插件结构方式的限制,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-library 项目,所以您不能通过子项目闭包应用这些插件.

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.

建议的解决方案是:

...在根项目的 build.gradle 中:

...in the root project's build.gradle:

ext {
  compileSdkVersion = 19
  buildToolsVersion = "19.0.1"
}

在所有安卓模块中:

android {
  compileSdkVersion rootProject.ext.compileSdkVersion
  buildToolsVersion rootProject.ext.buildToolsVersion
}

...

我注意到的一个想法是,这不适用于旧版本的 gradle(我尝试使用 1.10 并遇到错误).不过,对于 Gradle 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.

这篇关于从 gradle 构建 Android 项目时找不到参数的方法 android()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆