React Native升级后执行run-android报错:无法解析:classpath依赖
Alright, let's break down why this error is popping up and fix it step by step. The core issue here is that the Android Gradle Plugin version 3.1.2 isn't hosted on JCenter anymore—it lives in Google's official Maven repository, which your project isn't checking right now. Here's how to resolve this:
Step 1: Update the Project-Level build.gradle File
Navigate to your project's android/build.gradle file (this is the root build.gradle for the Android project, not the one inside the app folder). Modify the repositories section under buildscript to include the google() repository:
buildscript { repositories { google() // Add this line first jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' // Keep your other classpath dependencies (like the React Native Gradle plugin) here } } allprojects { repositories { mavenLocal() google() // Add this here too if it's missing jcenter() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url "$rootDir/../node_modules/react-native/android" } } }
Step 2: Match Gradle Wrapper Version to Android Plugin
Android Gradle Plugin 3.1.2 requires Gradle version 4.4 or higher. Check your gradle/wrapper/gradle-wrapper.properties file and update the distributionUrl to match:
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Step 3: Clean and Rebuild
Once you've made these changes, clean the Android build cache to avoid any leftover issues:
cd android && ./gradlew clean
Then go back to your project root and run the app again:
react-native run-android
Quick Context on Why This Happened
When you ran react-native upgrade, it reset your Android project's configuration files to the default template for your React Native version. The default template might not have included the google() repository (which became mandatory for newer Android Gradle Plugins) or had an outdated Gradle wrapper version that doesn't align with the plugin you're using.
内容的提问来源于stack exchange,提问作者Ketan Malhotra

