android studio在編輯時出現如Failed to sync Gradle project類似的問題時的解決方案
最近使用android studio發現環境壞了,提示如下:
Failedto sync Gradle project’XXXXX’
Error:Unable to load class'asLocalRepo0_1r7i3nvnipez3xbls4oy6h797'.
Possible causes for this unexpected errorinclude:<ul><li>You are using JDK version 'java version"1.7.0_67"'. Some versions of JDK 1.7 (e.g. 1.7.0_10) may cause classloading errors in Gradle.
Please update to a newer version (e.g.1.7.0_67).
<a href="open.project.structure">OpenJDK Settings</a></li><li>Gradle's dependency cache may becorrupt (this sometimes occurs after a network connection timeout.)
<ahref="syncProject">Re-download dependencies and sync project(requires network)</a></li><li>The state of a Gradle buildprocess (daemon) may be corrupt. Stopping all Gradle daemons may solve thisproblem.
<ahref="stopGradleDaemons">Stop Gradle build processes (requiresrestart)</a></li><li>Your project may be using a third-partyplugin which is not compatible with the other plugins in the project or theversion of Gradle requested by the project.</li></ul>In the case ofcorrupt Gradle processes, you can also try closing the IDE and then killing allJava processes.
按上面的意思,可能是jdk出問題了,所以查看了一下自己的jdk版本:1.7.0.67,已經是了呀,後來我讀這段英語才明白,它的意思是jdk1.7(無論是1.7.0_10還是1.7.0.67,還是別的1.7.XXX)型別的可能都會導致使用Gradle工具類載入錯誤,所以就去下載個最新版的吧,我寫的現在最新版是jdk 1.8,下載地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
如圖:
對於下載安裝配置完jdk1.8,我在專案目錄\app\build.gradle裡面寫的還是1.7:
sourceCompatibilityJavaVersion.VERSION_1_7
targetCompatibilityJavaVersion.VERSION_1_7
一開始是我寫成了1.8的如:
sourceCompatibilityJavaVersion.VERSION_1_8
targetCompatibilityJavaVersion.VERSION_1_8
報了很多莫名其妙的錯如:
UNEXPECTEDTOP-LEVEL EXCEPTION:
Error:com.android.dx.cf.iface.ParseException:bad class file magic (cafebabe) or version (0034.0000)
atcom.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
atcom.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
atcom.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
atcom.android.dx.command.dexer.Main.parseClass(Main.java:764)
at com.android.dx.command.dexer.Main.access$1500(Main.java:85)
atcom.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1684)
atcom.android.dx.command.dexer.Main.processClass(Main.java:749)
... 19 more
出現這種錯,可能是jdk1.8雖然比jdk1.7更相容,但他們使用的某些庫和函式差別比較,當然有兄弟知道的更多的,也請告訴我一聲,謝謝了。
然後再重新載入時就少了這個錯,
<a href="open.project.structure">OpenJDK Settings</a></li><li>Gradle's dependency cache may becorrupt (this sometimes occurs after a network connection timeout.)
<ahref="syncProject">Re-download dependencies and sync project(requires network)</a></li><li>The state of a Gradle buildprocess (daemon) may be corrupt. Stopping all Gradle daemons may solve thisproblem.
<ahref="stopGradleDaemons">Stop Gradle build processes (requiresrestart)</a></li><li>Your project may be using a third-partyplugin which is not compatible with the other plugins in the project or theversion of Gradle requested by the project.</li></ul>In the case ofcorrupt Gradle processes, you can also try closing the IDE and then killing allJava processes.
對於這個問題,觀察和猜想,關鍵應該是這句話:Your project may be using a third-party plugin which is notcompatible with the other plugins in the project or the version of Gradlerequested by the project,我的理解就是外掛不相容,再看看了官網,然後把咱們的專案下的build.gradle下面的改成最新的,我的是改成這樣:
再設定一下:專案目錄\gradle\wrapper\gradle-wrapper.properties這個檔案的distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip,如果不知道設定成什麼的話,就設定比較新吧,那麼怎麼知道現在有哪些新的gradle包呢?請訪問這個地址:
我選擇的是第二個,再重新編譯就沒有這個問題了,不過,我又出現新問題了:
……
我想了想,發現之前的專案頂級目錄的build.gradle這個檔案裡面只有:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
allprojects {
repositories {
jcenter()
}
}