Java 找不到参数 org.gradle.api.Project 的方法 android()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40019584/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:38:43  来源:igfitidea点击:

Could not find method android() for arguments org.gradle.api.Project

javaandroidgradleandroid-gradle-plugin

提问by Omega

Getting a bug, when I try to compile my project in studio , i have search quite a bit with no real solution to it

遇到一个错误,当我尝试在 Studio 中编译我的项目时,我搜索了很多,但没有真正的解决方案

Error:(17, 0) Could not find method android() for arguments [build_a7zf1o8ge4ow4uolz6kqzw5ov$_run_closure2@19201053] on root project 'booksStudioDir' of type org.gradle.api.Project.

错误:(17, 0) 在 org.gradle.api.Project 类型的根项目“booksStudioDir”上找不到参数 [build_a7zf1o8ge4ow4uolz6kqzw5ov$_run_closure2@19201053] 的方法 android()。

This is a sample of my build/gradle file

这是我的构建/gradle 文件的示例

buildscript {
repositories {
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    classpath 'io.fabric.tools:gradle:1.+'
  }
 }
  apply plugin: 'com.android.application'
apply plugin: 'io.fabric'


android {
compileSdkVersion 21
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.peade.time"
    minSdkVersion 10
    targetSdkVersion 13
}
signingConfigs {
    release {
        storeFile file("/home/bestman/Desktop/mkey/key")
        storePassword "androidkeys"
        keyAlias "peade"
        keyPassword "yes1234"
    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),    'proguard-rules.txt'
        signingConfig signingConfigs.release
     }
  }
}
  repositories {
    maven { url 'https://maven.fabric.io/public' }
    mavenCentral()
 }
dependencies {
   compile 'com.edmodo:cropper:1.0.1'
   compile 'com.android.support:support-v4:21.0.3'
   compile 'com.itextpdf:itextpdf:5.5.6'
   //    compile files('libs/commons-lang3-3.3.2.jar')
   compile files('libs/dropbox-android-sdk-1.6.1.jar')
   //    compile files('libs/httpmime-4.0.3.jar')
   compile files('libs/json_simple-1.1.jar')
   compile files('libs/picasso-2.5.2.jar')
   compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
    transitive = true;
   }
   compile project(':owncloud')
 }

采纳答案by nshmura

There are two build.gradle files. One is in the top-level holder, and the other is in module holder.

有两个 build.gradle 文件。一个在顶层支架中,另一个在模块支架中。

Below is an example.

下面是一个例子。

Top-level build.gradle:
https://github.com/nickbutcher/plaid/blob/master/build.gradle

顶级 build.gradle:https:
//github.com/nickbutcher/plaid/blob/master/build.gradle

module's build.gradle:
https://github.com/nickbutcher/plaid/blob/master/app/build.gradle

模块的 build.gradle:
https://github.com/nickbutcher/plaid/blob/master/app/build.gradle

The androidblock should be in module's build.gradle. I guess you must have defined the androidblock in top-level build.gradle which is causing the error.

android应该在模块的 build.gradle 中。我猜您一定android在顶级 build.gradle 中定义了导致错误的块。

回答by Manish Ahire

buildscript {

    ext.versions = [
            'compileSdk'    : 27,
            'targetSdk'     : 27,
            'buildTools'    : '27.0.3',
            'butterknife'   : '8.8.1',
            'glide'         : '4.6.1',
            'kotlin'        : '1.2.21',
            'retrofit'      : '2.3.0',
            'supportLibrary': '27.0.2'
    ]

    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0-alpha01'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
    }

}

回答by MehrAmoon

I had same problem if your android block is in module's build.gradle and use google services, just add these 2 lines before android block:

如果您的 android 块在模块的 build.gradle 中并使用 google 服务,我会遇到同样的问题,只需在 android 块之前添加以下两行:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

回答by Emmanuel Opare

Invalidate Cache and restart

使缓存无效并重新启动

Go to : File/Invalidate Cache/Click on Invalidate and Restart This should solve your problem for you.

转到:文件/无效缓存/单击无效并重新启动这应该为您解决问题。