最近在做一個 Android 項目,一次 Gradle Sync 報錯:
Gradle sync failed: Could not GET 'https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.3.20/kotlin-gradle-plugin-1.3.20.pom'. Received status code 405 from server: Method Not Allowed
Project 的 Gradle 腳本如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version ='1.3.20'
? ? repositories {
google()
jcenter()
}
dependencies {
classpath'com.android.tools.build:gradle:3.2.0-alpha01'
? ? ? ? classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
? ? ? ? // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
? ? }
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
deleterootProject.buildDir
}
由于報錯是 GET 請求失敗,初步懷疑是網絡問題,于是把?repositories 塊的 google() 注釋掉了。
網上搜了一下,說是國內 google 的庫訪問不了,?repositories 塊加了這一句:
maven { url"https://dl.google.com/dl/android/maven2/" }
再次 Sync ,又報錯:
Gradle sync failed: Could not GET 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-gradle-plugin/1.3.20/kotlin-gradle-plugin-1.3.20.pom'. Received status code 405 from server: Method Not Allowed
把 https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-gradle-plugin/1.3.20/kotlin-gradle-plugin-1.3.20.pom?用瀏覽器打開,看到以下信息:
{
? "errors" : [ {
? ? "status" : 404,
? ? "message" : "Could not find resource"
? } ]
}
看到 404 錯誤,于是把原來的鏈接刪了后面一部分,剩下“https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-gradle-plugin”,再打開,看到里面有很多版本號,看了一下,里面沒有?1.3.20 版本(估計是以前手殘把 ext.kotlin_version 設成了 1.3.20 ,本來是 1.2.20 的,導致了上面的 Sync 錯誤),于是把:
ext.kotlin_version ='1.3.20'
改成:
ext.kotlin_version ='1.2.20'
再 Sync 一下,錯誤沒了。
看來,細節決定成敗。于是以后寫代碼更加細心了。
總之,問題解決了,編程愉快!