首页 > 解决方案 > 如何处理“在org.gradle.api.Project类型的项目':app'上找不到方法android()”的错误

问题描述

我收到以下错误ERROR: Could not find method android() for arguments [build_9fawur68cjcg5sfkvki95w8sr$_run_closure1@30cec325] on project ':app' of type org.gradle.api.Project.

我不确定为什么会出现此错误,并且我尝试了许多其他方法以查看是否可以使其正常工作。虽然它从来没有。

应用程序级别的 gradle 已多次修改,但都没有解决问题。项目级别的 gradle 也无法正常工作。

应用级分级

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "net.troop59.LoginFirebase"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-core:16.0.6'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:28.0.0'

    //add this line
    compile 'com.google.firebase:firebase-auth:16.1.0'
}
apply plugin: "com.google.gms.google-services"

项目级gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
        buildscript {
            repositories {
                google()
                jcenter()

            }
            dependencies {
                classpath 'com.google.gms:google-services:4.2.0'

                // NOTE: Do not place your application dependencies here; they belong
                // in the individual module build.gradle files
            }
        }
allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

标签: androidandroid-gradle-plugin

解决方案


你错过了这条线

apply plugin: 'com.android.application'

在您的应用程序级build.gradle文件的开头。

更多信息

github上的一个例子

您还应该在项目级别具有此依赖项build.gradle

dependencies {
    ...
    classpath 'com.android.tools.build:gradle:3.3.0'
    ...
}

更新#1

您必须添加 google 存储库:

allprojects {
    repositories {
        google()
        jcenter()
    }
}

推荐阅读