首次安装Android Studio遇Gradle同步失败:找不到android()方法
Hey there, let's get this Gradle sync issue sorted out for your ICS (Android 4.0, API 15) project on Windows. That error pops up because there's a syntax mix-up in your Module: app build.gradle file—here's exactly what to do:
Why This Happens
The error is telling you Gradle can't find an android() method to call. This almost always means one of two things:
- You used parentheses
android(...)instead of curly bracesandroid {}for your Android module configuration (Gradle expects a code block here, not a method call). - You accidentally copied root-project level configuration (like
buildscriptsections) into your module'sbuild.gradlefile.
Step-by-Step Fixes
1. Correct the Android Configuration Syntax
Open your build.gradle(Module: app) file and fix the core syntax issue:
- Replace any lines starting with
android()with the properandroid {}block. For an ICS-targeted app, your config should look something like this:plugins { id 'com.android.application' } android { compileSdkVersion 15 // Match ICS's API level buildToolsVersion "30.0.3" // Use a build tools version compatible with API 15 (check your installed versions in SDK Manager) defaultConfig { applicationId "com.your.package.name" minSdkVersion 15 targetSdkVersion 15 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { // Add your app dependencies here, e.g., // implementation 'androidx.appcompat:appcompat:1.3.0' }
2. Separate Root vs. Module Gradle Configs
Double-check that your root project's build.gradle and module's build.gradle have distinct roles:
- Root
build.gradleshould handle global repository and plugin setup (noandroidblock here):buildscript { repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:7.0.4" // Use a Gradle plugin version compatible with your Android Studio } } allprojects { repositories { google() mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir } - Module
build.gradleshould only contain module-specific Android settings and dependencies (nobuildscriptsection here).
3. Verify SDK Setup
Since you're targeting ICS (API 15), open Android Studio's SDK Manager and make sure:
- The Android 4.0.3 (API 15) SDK platform is installed.
- A compatible Build Tools version (e.g., 30.0.3 or lower, since newer versions drop support for older APIs) is installed.
4. Sync Gradle
Once you've made these changes, click the "Sync Now" button in the top-right corner of Android Studio to apply the fixes.
内容的提问来源于stack exchange,提问作者Sandeep Ranjan

