最近在AndroidStudio命令行中使用gradle时遇到下面的错误:
java.lang.UnsupportedClassVersionError: com/android/build/gradle/AppPlugin : Unsupported major.minor version 52.0
网上找到的答案试过之后都不行,其根本原因是编译时的jdk版本跟运行时jdk版本不一致导致的,通过gradlew命令的–info选项可以看到以下信息:
Initialized native services in: C:\Users\linkaitao.gradle\native
Found daemon DaemonInfo{pid=4512, address=[596c6783-155f-4187-bb17-3351b218c6c3 port:65257, addresses:[/127.0.
0.1, /0:0:0:0:0:0:0:1]], state=Idle, lastBusy=1495703845901, context=DefaultDaemonContext[uid=9530595d-41b1-40
17-bdc5-0ba8a8482c12,javaHome=D:\Program Files\Java\jdk1.8.0_77,daemonRegistryDir=C:\Users\linkaitao.gradle\d
aemon,pid=4512,idleTimeout=10800000,daemonOpts=-Xmx1536M,-Dfile.encoding=GBK,-Duser.country=CN,-Duser.language
=zh,-Duser.variant]} however its context does not match the desired criteria.
Java home is different.
Wanted: DefaultDaemonContext[uid=null,javaHome=D:\Program Files\Java\jdk1.7.0_80,daemonRegistryDir=C:\Users\li
nkaitao.gradle\daemon,pid=6944,idleTimeout=null,daemonOpts=-Xmx1536M,-Dfile.encoding=GBK,-Duser.country=CN,-D
user.language=zh,-Duser.variant]
Actual: DefaultDaemonContext[uid=9530595d-41b1-4017-bdc5-0ba8a8482c12,javaHome=D:\Program Files\Java\jdk1.8.0_
77,daemonRegistryDir=C:\Users\linkaitao.gradle\daemon,pid=4512,idleTimeout=10800000,daemonOpts=-Xmx1536M,-Dfi
le.encoding=GBK,-Duser.country=CN,-Duser.language=zh,-Duser.variant]
很显然,是jdk版本不匹配导致的,但是更改了JAVA_HOME和IDE的jdk设置依然不行,最后将gradle.properties中加了下面一句才解决:
org.gradle.java.home=D:\Program Files\Java\jdk1.8.0_77