Could not find method android() for arguments
E

5

105

I've been trying to import a project to Android Studio and this is where I am stuck, there is a similar question on Stack Overflow but it did not provide a solution to my particular error.

This is my error log:

C:\<some location>\build.gradle

Error:(24, 1) A problem occurred evaluating root project '<project name>'.
> Could not find method android() for arguments [build_4fli1jm76ubcnxesnhqnhie47$_run_closure3@6e71db85] on root project '<project name>'.
Information:BUILD FAILED

The Gradle sync messages are:

Error:(24, 0) Gradle DSL method not found: 'android()' Possible causes:

  • The project 'PoojaPeople' may be using a version of Gradle that does not contain the method. Gradle settings
  • The build file may be missing a Gradle plugin. Apply Gradle plugin
  • I'm not too sure where exactly this method android() is located. If it is the one located in Application's build.gradle file, I still don't exactly know where to go from here. Any help is appreciated.

    My build.gradle is

    buildscript {
        repositories {
            maven { url "http://dl.bintray.com/populov/maven" }
            mavenCentral()
        }
         dependencies {
            classpath 'com.android.tools.build:gradle:2.1.0'
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    allprojects {
        repositories {
            maven { url "http://dl.bintray.com/populov/maven" }
            mavenCentral()
    
        }
    }
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    
    android {
        compileSdkVersion 17
        buildToolsVersion '23.0.0'
    }
    dependencies {
        compile files('app/libs/junit-4.12-JavaDoc.jar')
    }
    apply plugin: 'maven'
    
    Evie answered 16/5, 2016 at 9:20 Comment(3)
    where is your build.gradle ? post itTatianna
    Does this answer your question? Could not find method compile() for arguments GradleClubhouse
    try it classpath "com.android.tools.build:gradle:4.0.1"Marven
    U
    212

    You are using the wrong build.gradle file.

    In your top-level file you can't define an android block.

    Just move this part inside the module/build.gradle file.

    android {
        compileSdkVersion 17
        buildToolsVersion '23.0.0'
    }
    dependencies {
        compile files('app/libs/junit-4.12-JavaDoc.jar')
    }
    apply plugin: 'maven'
    
    Underproof answered 16/5, 2016 at 10:48 Comment(8)
    I had the same problem, and your answer fixed it for me. I had installed a fresh version of Android Studio on a new machine, and was trying to install a sample from this repo: github.com/googlesamples/android-vision I am not sure why, but at some point in the import process Android Studio itself added the android() block in the top level build.gradle file which was not in the original source.Tandi
    I have an old Eclipse project automatically imported into AS 2.2.2. It's reporting that same error but in the build.graddle file inside the app folder, where Gabriele suggests moving that android { } section. So, it's something else.Mcnully
    I really wish there were more meaningful messages in studio. I hate that Eclipse has been stopped; Studio is such a pain in the neck. Takes more time to actually compile the code than to actually write it. 10x more time!Kristoferkristoffer
    Happened the same with me, Android Studio added this block after I changed the project settings using the Project Structure / Modules / Properties form.Rosemarie
    Most of the error messages produced by the Android Studio / Gradle are like this; incomprehensible and misguiding. Sorry, but its a laugh.Apparel
    This happened to me too when I downloaded a project, which used sdk version 24, while I had version 26 installed. I changed version in project settings, but Andriod Studio modified top-level build.gradle file. Removing the generated android() block and manually fixing app/build.gradle file fixed the issue.Awhirl
    Seems that IntelliJ must've put that block in when I edited it in Project Structure. Seems like a bug to me!Tingly
    With agp 8 and new settings plugin you can have global setting in settings.gradle for all projects. But I do not know how. I tried to use android block directly in settings.gradle file but that doesn't work. Idk why there is no good documentation for it. link: developer.android.com/build/releases/…Westley
    K
    17

    My issue was inside of my app.gradle. I ran into this issue when I moved

    apply plugin: "com.android.application"
    

    from the top line to below a line with

    apply from:
    

    I switched the plugin back to the top and violá

    My exact error was

    Could not find method android() for arguments [dotenv_wke4apph61tdae6bfodqe7sj$_run_closure1@5d9d91a5] on project ':app' of type org.gradle.api.Project.
    

    The top of my app.gradle now looks like this

    project.ext.envConfigFiles = [
            debug: ".env",
            release: ".env",
            anothercustombuild: ".env",
    ]
    
    
    apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
    apply plugin: "com.android.application"
    
    Khufu answered 27/10, 2018 at 15:49 Comment(2)
    Good catch! Thanks!Oyster
    apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle" all the way to the bottom of the file did the trick for me.Puri
    B
    3

    guys. I had the same problem before when I'm trying import a .aar package into my project, and unfortunately before make the .aar package as a module-dependence of my project, I had two modules (one about ROS-ANDROID-CV-BRIDGE, one is OPENCV-FOR-ANDROID) already. So, I got this error as you guys meet:

    Error:Could not find method android() for arguments [org.ros.gradle_plugins.RosAndroidPlugin$_apply_closure2_closure4@7e550e0e] on project ‘:xxx’ of type org.gradle.api.Project.
    

    So, it's the painful gradle-structure caused this problem when you have several modules in your project, and worse, they're imported in different way or have different types (.jar/.aar packages or just a project of Java library). And it's really a headache matter to make the configuration like compile-version, library dependencies etc. in each subproject compatible with the main-project.

    I solved my problem just follow this steps:

    ① Copy .aar package in app/libs.

    ② Add this in app/build.gradle file:

    repositories {
        flatDir {
            dirs 'libs' //this way we can find the .aar file in libs folder
        }
    }
    

    ③ Add this in your add build.gradle file of the module which you want to apply the .aar dependence (in my situation, just add this in my app/build.gradle file):

    dependencies {
        compile(name:'package_name', ext:'aar')
    }
    

    So, if it's possible, just try export your module-dependence as a .aar package, and then follow this way import it to your main-project. Anyway, I hope this can be a good suggestion and would solve your problem if you have the same situation with me.

    Brownfield answered 20/4, 2017 at 6:26 Comment(0)
    A
    1

    This error appear because the compiler could not found "my-upload-key.keystore" file in your project

    After you have generated the file you need to paste it into project's andorid/app folder

    this worked for me!

    Avowal answered 5/5, 2020 at 10:13 Comment(0)
    C
    1

    In my case, I generate the project by create-react-native and react-native run-android, and there are two build.gradle files, which in ./android/build.gradle and ./android/app/build.gradle.

    You need to set android to ./android/app/build.gradle, which has the line apply plugin: "com.android.application"

    Cooke answered 15/2 at 4:3 Comment(0)

    © 2022 - 2024 — McMap. All rights reserved.