此博客可能適用於本來能正常編譯的老項目,一段時間後無法編譯通過,並提示這個錯誤。假如是新建項目碰到的,可能的確是build.gradle裏classpath沒寫好版本。
今天打車還遲到了,很自閉,然後打開android studio。項目自動編譯,突然爆了個大紅色,更自閉了。以下是錯誤流程:
說實話,有點懵,我第一反應是不是Android Studio把我安卓編譯版本升級了,我的項目下的build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: 'config/global.gradle'
buildscript {
ext.kotlin_version = '1.2.10'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath "com.tencent.bugly:tinker-support:latest.release"
classpath 'com.meituan.android.walle:plugin:1.1.5'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
mavenCentral()
jcenter()
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/releases/'
}
maven {
url "https://jitpack.io"
}
maven {
url "https://dl.bintray.com/thelasterstar/maven/"
}
maven {
url "https://maven.google.com"
}
maven {
url "https://dl.google.com/dl/android/maven2/"
}
flatDir{
dir 'libs'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我的gradle-wrapper.properties:
#Tue May 23 09:23:46 CST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
發現沒有任何變化,git也沒提示有文件被修改
先是照着提示,操作了一遍,發現沒有任何作用:
Add Google Maven repository and sync project
這個時候項目下的build.gradle的repositories增加了谷歌倉庫
repositories { jcenter() maven { url 'https://maven.google.com/' name 'Google' } }
然後編譯失敗,提示:
說是gradle版本最低爲4.6,雖然我不想升級,但是我還是點擊了Fix Gradle wrapper and re-import project
這個時候gradle-wrapper.properties的gradle版本號已經變成了4.6,接下來可能是我Android Studio版本太低,是無法編譯通過的,提示:Error:Cause: com/android/builder/Version
假如是比較高級的Android Studio可能這個時候就會提示什麼compile應該替換成implementation之類的,就可以改了,我沒升級Android Studio上面只是猜測。
從最開始提示找不到com.android.tools.build:gradle:3.2.1可以猜測,是否是某些東西需要了3.2.1進行編譯,而我們沒有改任何配置的情況下,最可疑的就是那些版本號爲latest.release的依賴了,那麼第一個懷疑對象就是它的同級:騰訊的bugly,先把代碼回滾,然後把它版本號寫死,寫成固定的一個版本,重新編譯,編譯成功!
假如是突然出現問題是沒使用過的編譯版本,那麼很可能是你的其他依賴自動更新後使用了該編譯工具,回退上一個版本,就能解決問題。
有點囉嗦,但是這個問題,的確搞了好久,希望能幫到大家。