为model添加了一个第三方的jar包,同步build的之后报错。
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
大概的意思是 这个Jar包是用Java 8 打包的,但是Android Studio的默认用的JDK1.7,所以如果想使用这个jar包,
请到生成jar包的model的build.gradle中添加targetCompatibility = '1.7' sourceCompatibility = '1.7'。
一开始我理解错了,以为是在我的app的model build.gradle中添加targetCompatibility = '1.7' sourceCompatibility = '1.7'这两句,
结果加上去之后毫无效果,所以才认真理解这个error的提示内容,发现这是要求在lib生成的model中加的,然而这个第三方的jar没有源码,
所以我考虑把我的jdk设置为1.8,使用java8编译。
然而,我才发现我环境变量中配的就是java 8 ,为什么还会报这个错呢。我查了一下资料,才知道AndroidStudio要使用JAVA8必须要收到设置,
所以在build.gradle中增加了:
compileOptions{
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
然后点同步,报错:
Error:Jack is required to support java 8 language features. Either enable Jack or remove sourceCompatibility JavaVersion.VERSION_1_8.
在module的defaultConfig里添加
jackOptions {
enabled true
}
再同步一下,build通过。
整个model的build.gradle:
applyplugin:'com.android.application'
android {
compileSdkVersion26
buildToolsVersion"26.0.1"
defaultConfig {
applicationId"com.soft.jgui.connmysql"
minSdkVersion21
targetSdkVersion26
versionCode1
versionName"1.0"
testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
jackOptions {
enabledtrue
}
}
buildTypes {
release {
minifyEnabledfalse
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
compileOptions{
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
compile fileTree(dir:'libs',include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
excludegroup:'com.android.support',module:'support-annotations'
})
compile'com.android.support:appcompat-v7:26.+'
compile'com.android.support.constraint:constraint-layout:1.0.2'
testCompile'junit:junit:4.12'