react-native run-android报错: Could not resolve com.android.tools.build:gradle:3.1.4
错误输出
* What went wrong:
A problem occurred configuring root project 'LearnRN'.
> Could not resolve all files for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:3.1.4.
Required by:
project :
> Could not resolve com.android.tools.build:gradle:3.1.4.
> Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.4/gradle-3.1.4.pom'.
> Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.4/gradle-3.1.4.pom'.
> Connect to dl.google.com:443 [dl.google.com/172.217.25.78] failed: Connection timed out: connect
解决
找了很久, 比较多的是修改build.gradle文件中的maven配置, 但是一直没有效果. 怀疑是网络原因,无法访问到墙外网址.
- 修改Gradle的Maven仓库地址为国内仓库,例如阿里云
buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 16
compileSdkVersion = 28
targetSdkVersion = 26
supportLibVersion = "27.1.1"
}
repositories {
// google()
// jcenter()
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
- 修改buildscript属性下仓库地址
allprojects {
repositories {
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
url "$rootDir/../node_modules/react-native/android"
}
}
}
这边有个坑的地方, Android文档上说,每个maven仓库链接都应该有自己单独的maven标签。所以上面这种写法是不正确的
需要改为:
allprojects {
repositories {
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android" //打包react native程序时,这个仓库地址是一定要保留的,切记
}
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
}
}
- Could not find com.android.tools.build:gradle:3.1.4
Could not resolve all files for configuration ':classpath'.
> Could not find com.android.tools.build:gradle:3.1.4.
Searched in the following locations:
http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/3.1.4/gradle-3.1.4.pom
http://maven.aliyun.com/nexus/content/groups/public/com/android/tools/build/gradle/3.1.4/gradle-3.1.4.jar
https://repo.maven.apache.org/maven2/com/android/tools/build/gradle/3.1.4/gradle-3.1.4.pom
https://repo.maven.apache.org/maven2/com/android/tools/build/gradle/3.1.4/gradle-3.1.4.jar
找到SDK目录中build-tools下自己的版本号,修改android/build:gradle配置文件中的buildToolsVersion属性为自己的版本号,注意compileSdkVersion属性也需要修改,例如版本号为28.0.3 那么compileSdkVersion属性为28