最近使用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
如图:
安装配置请参考:http://jingyan.baidu.com/article/f96699bb8b38e0894e3c1bef.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包呢?请访问这个地址:
https://services.gradle.org/distributions/如下图:
我选择的是第二个,再重新编译就没有这个问题了,不过,我又出现新问题了:
……
Error:(17, 0) Gradle DSL method not found: 'android()'如图
我想了想,发现之前的项目顶级目录的build.gradle这个文件里面只有:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
allprojects {
repositories {
jcenter()
}
}
所以把这些之外,17行开始android{}的相关内容删除后,就可以了。