下載個例子,在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中。