今天運(yùn)行./gradlew :PandaAndroidDemo:release
出現(xiàn)如下錯(cuò)誤:
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/shitianci/work/Lab/panda.android/PandaAndroidDemo/build.gradle' line: 1
* What went wrong:
A problem occurred evaluating project ':PandaAndroidDemo'.
> java.lang.UnsupportedClassVersionError: com/android/build/gradle/AppPlugin : Unsupported major.minor version 52.0
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
直接點(diǎn)擊 run按鈕 或者 Build→Generate Build APK 卻運(yùn)行正常像樊。
這里面有兩個(gè)問題:
- 為什么出現(xiàn)
Unsupported major.minor version 52.0
尤莺? - 為什么gradle命令和android studio按鈕運(yùn)行結(jié)果不一樣?
問題一:為什么出現(xiàn)Unsupported major.minor version 52.0
生棍?
在網(wǎng)上找了一圈颤霎,最后在stackoverflow找到了本質(zhì)原因
You get this error because a Java 7 VM tries to load a class compiled for Java 8
Java 8 has the class file version 52.0 but a Java 7 VM can only load class files up to version 51.0
In your case the Java 7 VM is your gradle build and the class is com.android.build.gradle.AppPlugin
簡(jiǎn)單來說,就是java的編譯環(huán)境版本太低涂滴,java 8 class file的版本是52友酱,Java 7虛擬機(jī)只能支持到51。所以需要升級(jí)到j(luò)ava 8 vm才行柔纵。
問題二:為什么gradle命令和android studio按鈕運(yùn)行結(jié)果不一樣缔杉?
從問題1來看,肯定Android Studio按鈕調(diào)用的是java 8 vm首量,所以查找一下系統(tǒng)配置壮吩,最終在Project Structure找到了如下設(shè)置:
Android Studio 2.2.2使用了自帶的JDK環(huán)境,其地址為
/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home
而gradle命令的執(zhí)行環(huán)境是在gradle.properties配置的加缘,其指向?yàn)椋?/p>
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/home
將其修改為:
org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home
問題解決鸭叙。
參考: