Could not find method android() for arguments when

2019-01-16 18:51发布

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

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:

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

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?

2条回答
该账号已被封号
2楼-- · 2019-01-16 19:21

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.

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.

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
}

...

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-16 19:33

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

At the root gradle file you need to add the following

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

https://docs.gradle.org/current/userguide/multi_project_builds.html#sub:adding_specific_behavior

查看更多
登录 后发表回答