前言
Android Studio安裝完成割按,新創(chuàng)建或?qū)牍こ烫崾疽韵洛e(cuò)誤信息:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'MyApplication'.
> Could not resolve all artifacts for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:xxx.
Required by:
project :
> Could not resolve com.android.tools.build:gradle:xxx.
> Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/xxx/gradle-xxx.pom'.
> Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/xxx/gradle-xxx.pom'.
> Connect to dl.google.com:443 [dl.google.com/172.217.26.14, dl.google.com/2404:6800:4004:809:0:0:0:200e] failed: No route to host (connect failed)
意思是說(shuō)無(wú)法訪問(wèn)dl.google.com這個(gè)域名两波,用命令ping這個(gè)域名的確是無(wú)法訪問(wèn)刁赖。實(shí)際上Android 工程的構(gòu)建過(guò)程很多步驟需要訪問(wèn)這個(gè)域名。
解決方法
方式一
修改項(xiàng)目根目錄下的build.gradle文件泞歉,使用國(guó)內(nèi)地址構(gòu)建,如下:
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
}
}
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
}
}
問(wèn)題是解決了匿辩,但對(duì)代碼有侵入且不利于代碼維護(hù)腰耙。
方式二
-
查詢(xún)dl.google.com在國(guó)內(nèi)的可用的鏡像IP地址(http://ip.tool.chinaz.com/)
image.png 修改電腦hosts文件,鏡像國(guó)內(nèi)域名訪問(wèn)铲球,配置如下:
Mac下:Finder->前往->前往文件夾(輸入etc/hosts):文件拷貝出來(lái)挺庞,打開(kāi)后末尾添加:
203.208.43.71 dl.google.com修改完成后替換hosts文件即可。
Windows訪問(wèn)系統(tǒng)hosts文件稼病,修改方式相同选侨。