Java Android:Dex 无法解析版本 52 字节代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37020413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Dex cannot parse version 52 byte code
提问by Hephaestus
I just switched to Android Studio 2.1 and this error showed up when trying to compile an app the was previously working:
我刚刚切换到 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.
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.
I had already updated the main project's gradle.build file to force Java 1.7 code generation:
我已经更新了主项目的 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
}
}
I had also updated the module gradle.build as follows to set the java version:
我还更新了模块 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
}
}
The submodule being built with Maven. In the pom.xml file I have also tried to force 1.7 code generation.
I understand that I am using an assembly artifact, which incorporates subordinate modules, but i have not changed any of the subordinate modules and the resulting .jar file for the module ran fine last time I compiled.
使用 Maven 构建的子模块。在 pom.xml 文件中,我还尝试强制生成 1.7 代码。
我知道我正在使用一个包含从属模块的程序集工件,但我没有更改任何从属模块,并且在我上次编译时生成的模块的 .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>
My question: 1) Is this an Android Studio 2.1 problem? Have others seen it? 2) Assuming this is my error and since the error message gives no help in finding the bad module, are there any recommendations on finding the V52 code? I can't just omit the libraries without breaking large amount of code. Can one inspect a .jar file to find the code revision? Thanks in advance. -Hephaestus
我的问题:1) 这是 Android Studio 2.1 的问题吗?其他人看过吗?2) 假设这是我的错误,并且由于错误消息对查找坏模块没有帮助,是否有任何关于查找 V52 代码的建议?我不能在不破坏大量代码的情况下省略库。可以检查 .jar 文件以查找代码修订版吗?提前致谢。-赫菲斯托斯
采纳答案by foolcage
just use java 1.8 with Android Studio 3.0+ and set following works for me: it seems need the latest build tools
只需在 Android Studio 3.0+ 中使用 java 1.8 并为我设置以下工作:似乎需要最新的构建工具
classpath 'com.android.tools.build:gradle:3.0.0'
and
和
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
...
//HymanOptions { // DEPRECATED
//enabled true
//}
}
dexOptions {
incremental true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
回答by fobo66
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.
可能您的某些依赖项是使用 Java 8 编译的,尤其不是针对 Android。尝试将该依赖项切换到旧版本。我不完全知道您应该降级哪个库,因为您没有附上主模块的依赖项列表。
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.
例如:我遇到了同样的问题。经过数小时的搜索,我发现库org.codehaus.httpcache4j.uribuilder:2.0.0
需要 Java 8,从github 开始。因此,当我切换到 时1.1.0
,项目已成功构建和部署。
回答by nexDev
If you have a module with a java library that is not Android-specific, this should work: apply plugin:'java'
如果您的模块的 java 库不是 Android-specific,则应该可以:apply plugin:'java'
Put it at the top of the build.gradle file, then rebuild.
把它放在 build.gradle 文件的顶部,然后重建。
apply plugin: 'java'
apply plugin: 'jacoco'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.11'
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
回答by 0wl
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.
如果您使用org.jetbrains:annotation:15
和retrolambda 插件,然后compile org.jetbrains:annotations:15.0
从您的行中删除build.gradle
,错误就会消失。这个对我有用。
回答by Riccardo Leschiutta
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.
我对 greendao-generator 依赖项有同样的问题。我错误地将该依赖项添加到我的 build.gradle ( compile 'org.greenrobot:greendao-generator:3.1.0'
) 中,AndroidStudio 向我显示了相同的错误消息。
Probably it's because that module has been compiled with Java 8.
可能是因为该模块是用 Java 8 编译的。
So I removed that dependency from my build.gradle and all compiled happily :)
所以我从我的 build.gradle 中删除了那个依赖项,并且全部编译愉快:)
回答by u6270136
I solved this problem as belowed :
我解决了这个问题如下:
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
回答by StephanBezoen
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.
使用 Gradle 插件 2.2.2 在 Android Studio 2.2 中关闭 Instant Run 为我修复了它。切换回旧版本的 Gradle 插件(例如 2.2.0)也修复了它,但恕我直言,这不太理想。
回答by vitalinvent
Try to add to main build.gradle in section allprojects
尝试在 allprojects 部分添加到 main build.gradle
tasks.withType(JavaCompile) {
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
}
or add this in dependencies
或者在依赖项中添加这个
sourceCompatibility = 1.7
targetCompatibility = 1.7
in all modulesmanually
在所有模块中手动
回答by Faisal Ali
I was able to solve this issue by adding the following lines:
我能够通过添加以下几行来解决这个问题:
HymanOptions {
enabled true
}
to defaultConfig
in build.gradle
file.
要defaultConfig
在build.gradle
文件中。
You can follow the guidelines for Java 8 on the link - https://developer.android.com/guide/platform/j8-Hyman.html
您可以在链接上遵循 Java 8 指南 - https://developer.android.com/guide/platform/j8-Hyman.html
回答by mp501
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.
这发生在我的 Android Studio 2.3.3 上。我找到的解决方案是删除 build 文件夹,然后删除Rebuild Project。就这么简单。