我是android编程的新手,当我尝试构建我的第一个项目时,我收到以下错误 -
Gradle sync失败:无法在根项目' MyApplication'上找到参数[build_ed74sxwq3zd69j7x0p9x4y5fb $ _run_closure3 @ 3d54d64f]的方法android()。类型为org.gradle.api.Project。
// 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

答案 0 :(得分: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'
答案 1 :(得分:0)
我在android studio 2.3.3中遇到了同样的问题,我错误的是我的依赖项是错误的build.gradle(这是错误的)
我们需要的build.gradle是带有android选项的那个,如下面的`apply plugin:'com.android.application'
android { 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
}
}` 您可以在android花括号内添加dexOptions。
答案 2 :(得分:0)
当我将它添加到项目级别的 gradle 文件时,我遇到了同样的错误。将其移至应用级 gradle 文件,您应该会好起来。