下载个例子,在Android Studio中打开,错误N多,这个错误如下:
Gradle DSL method not found: ‘android()’
在stackoverflow上有很好的回答:
解决方法:
在项目顶层(根目录)的build.gradle中,有下面的代码,其实是不行的:
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
}
这些代码其实是在打开模块属性对话框,设置了“Compile Sdk Version”和“Build Tools Version”之后,自动在顶层build.gradle中生成的,坑人。其实,如果这个对话框中左侧的模块中,如果有app模块,那么修改的属性应该就插入到app/build.gradle中了。
把这些删掉,重新刷新就可以了。
此外,在顶层的build.gradle中,下面这些依赖的代码也是不可以有的,否则会出现错误:Gradle Dsl method not found compile()
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
}
这些都只能在项目中模块(如app)的build.gradle中。