Android Studio新手求助:Gradle同步失败(找不到gradle:3.1.1)
Hey there! I remember hitting this exact issue when I was first starting out with Android Studio—Gradle sync errors can feel super frustrating, but let’s break down how to fix this step by step.
Step 1: Verify Your Project-Level build.gradle Repository Config
First, open your project’s root build.gradle file (the one in the main project folder, not the app module). Make sure you have the correct repositories listed in both the buildscript and allprojects blocks. Google’s Maven repository is essential for accessing Android Gradle plugin versions, and Maven Central has replaced JCenter for most dependencies now.
Here’s what the file should look like:
buildscript { repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:3.1.1" } } allprojects { repositories { google() mavenCentral() } }
Double-check that you haven’t missed either repository—this is the most common reason Gradle can’t find the plugin.
Step 2: Use the Default Gradle Wrapper
Android Studio’s Gradle wrapper handles downloading the correct Gradle version automatically. To make sure this is enabled:
- Go to
File > Settings > Build, Execution, Deployment > Gradle(on Mac, it’sAndroid Studio > Preferences > Build, Execution, Deployment > Gradle) - Ensure the option "Use default Gradle wrapper (recommended)" is selected.
- Click "Apply" then "OK".
Step 3: Clear Caches and Restart Android Studio
Sometimes old cached files can cause issues. Let’s wipe those and start fresh:
- Go to
File > Invalidate Caches... - Check the box next to "Invalidate and Restart"
- Click "Invalidate" to restart Android Studio. This will clear old cache files and force Gradle to re-download the plugin from the repositories.
Optional: Upgrade to a Newer Gradle Plugin Version
The 3.1.1 version of the Android Gradle plugin is pretty outdated (it’s from 2018) and might not play nicely with newer versions of Android Studio. If the above steps don’t work, or if you want a more stable setup, consider upgrading:
- In your root
build.gradle, change the classpath to a newer, supported version. For example:classpath "com.android.tools.build:gradle:7.0.4" - Open
gradle/wrapper/gradle-wrapper.propertiesand update thedistributionUrlto match the required Gradle version for that plugin (7.0.4 uses Gradle 7.0.2):distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip - Sync your project again—Android Studio will guide you through any additional setup if needed.
Give these steps a try, and your Gradle sync should work smoothly once you’ve got the right repositories and cache cleared.
内容的提问来源于stack exchange,提问作者Mnsrh

