【问题标题】:Gradle sync failed: Could not find method android() for arguments on root project on android studioGradle 同步失败:找不到方法 android() 用于 android studio 上根项目的参数
【发布时间】:2017-04-05 09:22:56
【问题描述】:

我是 android 编程新手,当我尝试构建我的第一个项目时,我收到以下错误 -

Gradle 同步失败:在 org.gradle.api.Project 类型的根项目“MyApplication”上找不到参数 [build_ed74sxwq3zd69j7x0p9x4y5fb$_run_closure3@3d54d64f] 的方法 android()。

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.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
}

android {
    compileSdkVersion 19
    buildToolsVersion '25.0.2'
    dexOptions {
        incremental true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
}
dependencies {compile files('app/libs/junit-4.12-JavaDoc.jar')
}
apply plugin: 'maven'

app/build/gradle 文件是 -

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.surabhi.myapplication"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.3.1'
    testCompile 'junit:junit:4.12'
    compile files('app/libs/junit-4.12-JavaDoc.jar') }
    apply plugin: mavin

【问题讨论】:

    标签: android android-studio android-gradle-plugin build.gradle


    【解决方案1】:

    您使用了错误 build.gradle 文件。

    您不能在顶级文件中定义android 块。

    你有删除这部分在你的

    android {
        compileSdkVersion 19
        buildToolsVersion '25.0.2'
        dexOptions {
            incremental true
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_6
            targetCompatibility JavaVersion.VERSION_1_6
        }
    }
    dependencies {compile files('app/libs/junit-4.12-JavaDoc.jar')
    }
    apply plugin: 'maven'
    

    【讨论】:

      【解决方案2】:

      我在 android studio 2.3.3 中遇到了同样的问题,我做错的是我的依赖项在错误的 build.gradle 中(这是错误的)

      我们需要的 build.gradle 是带有 android 选项的,如下 `apply plugin: 'com.android.application'

      机器人{ compileSdkVersion 23 buildToolsVersion '23.0.0'

      defaultConfig {
          applicationId "com.movas.mservice12"
          minSdkVersion 9
          targetSdkVersion 23
          versionCode 11
          versionName "2.0"
          multiDexEnabled true
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
      lintOptions {
          abortOnError false
      }
      dexOptions {
         // incremental = true
          preDexLibraries = false
          javaMaxHeapSize "4g" // 2g should be also OK
      }
      

      }` 确保添加 dexOptions 的位置在 android 花括号内。

      【讨论】:

        【解决方案3】:

        当我将它添加到项目级 gradle 文件时,我遇到了同样的错误。将此移动到应用程序级别的 gradle 文件中,您应该会很好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-03
          • 2014-04-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-22
          • 2014-09-10
          • 1970-01-01
          相关资源
          最近更新 更多