Android: Dex 无法解析版本52字节代码

我刚刚切换到 Android Studio 2.1,在尝试编译一个以前可以工作的应用程序时,出现了这个错误:

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.

我已经更新了主项目的 gradle.build 文件,以强制 Java 1.7代码生成:

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
}

我还对 gradle.build 模块进行了如下更新,以设置 Java 版本:

android {
compileSdkVersion 19
buildToolsVersion "23.0.2"


defaultConfig {
applicationId "com.abc.def"
minSdkVersion 19
targetSdkVersion 19
}


buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}

使用 Maven 构建的子模块。
我知道我正在使用一个组装工件,它包含了从属模块,但是我没有改变任何从属模块和结果。模块的 jar 文件在上次编译时运行良好。

    <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId> <!-- maven-compiler-plugin -->
<version>2.6</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

我的问题是: 1)这是 Android Studio 2.1的问题吗? 其他人看到过吗? 2)假设这是我的错误,因为错误消息没有帮助找到坏的模块,是否有任何建议找到 V52代码?我不能在不破坏大量代码的情况下省略这些库。能不能检查一下。找到代码修订的 jar 文件? 先谢谢你。 赫菲斯托斯

88153 次浏览

Possibly, some of your dependencies was compiled with Java 8, not for Android especially. Try to switch that dependencies to older version. I don't exactly know which library you should downgrade, because you haven't attached a list of dependencies of your main module.

For example: I had the same problem. After hours of searching, I've found that library org.codehaus.httpcache4j.uribuilder:2.0.0 requires Java 8, as of github. So, when i've switched to 1.1.0, project has been successfully builded and deployed.

just use java 1.8 with Android Studio 3.0+ and set following works for me: it seems need the latest build tools

classpath 'com.android.tools.build:gradle:3.0.0'

and

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"


defaultConfig {
...
//jackOptions { // DEPRECATED
//enabled true
//}
}
dexOptions {
incremental true
}


compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

If you have a module with a java library that is not Android-specific, this should work: apply plugin:'java'

Put it at the top of the build.gradle file, then rebuild.

    apply plugin: 'java'
apply plugin: 'jacoco'


dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.11'


sourceCompatibility = 1.7
targetCompatibility = 1.7
}

If you use org.jetbrains:annotation:15 and retrolambda plugin then remove line compile org.jetbrains:annotations:15.0 from your build.gradle and the error will disappear. It works for me.

I had the same problem with the greendao-generator dependency. I mistakenly added that dependency into my build.gradle (compile 'org.greenrobot:greendao-generator:3.1.0') and AndroidStudio showed me the same error message.

Probably it's because that module has been compiled with Java 8.

So I removed that dependency from my build.gradle and all compiled happily :)

I solved this problem as belowed :

apply plugin: 'java'


sourceCompatibility = 1.7
targetCompatibility = 1.7


dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

Switching off Instant Run in Android Studio 2.2 with Gradle plugin 2.2.2 fixed it for me. Switching back to an older version of the Gradle plugin (such as 2.2.0) also fixed it, but that's less desirable imho.

Try to add to main build.gradle in section allprojects

tasks.withType(JavaCompile) {
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
}

or add this in dependencies

    sourceCompatibility = 1.7
targetCompatibility = 1.7

in all modules manually

I was able to solve this issue by adding the following lines:

jackOptions {
enabled true
}

to defaultConfig in build.gradle file.

You can follow the guidelines for Java 8 on the link - https://developer.android.com/guide/platform/j8-jack.html

This happened to me with Android Studio 2.3.3. The solution I found was to delete the build folder and then Rebuild Project. It was as simple as that.

I have come across this issue when trying to upgrade to auto-value v 1.5 in Android Studio v 2.3.3. Auto-value 1.5 will presumably be compatible with AS 3 (It requires an updated java compiler)

For now auto-value 1.4.1 works.

I also faced same error in Android 2.3.3, after adding few JAR depencies. Problem was due to the depenecy io.netty:netty-all:4.1.16.Final. This 4.1.16 version JAR was compiled with Java 1.8 and all others was generated with Java 1.7.

This get resolved, after including older version of netty(which was generated with Java 1.7) in my build.gradle file.

compile 'io.netty:netty-all:4.1.5.Final'

I face the same issue with Reactive Location APIs Library for Android and RxJava 2.Just update build.gradle to 3.0.1 and reduce the Reactive Location APIs Library for Android and RxJava 2 library version from 1.0.4 to 1.0.3 It works fine in my case.

I have come across this issue when trying to import a jar compiled by jdk 1.8 in Android Studio 3.0. I tried all above solution, but none work. so, I asked the developer of that jar to re-compile it with jdk 1.7, and then it work well, not come across this issue again.

If possible for you:

  1. Upgrade android tools to: classpath 'com.android.tools.build:gradle:3.0.0'
  2. buildToolsVersion "26.0.2"

This should take care of the issue.

Reference: https://developer.android.com/studio/write/java8-support