我刚切换到 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 构建的子模块。在 pom.xml 文件中,我还尝试强制生成 1.7 代码。
我知道我正在使用一个包含从属模块的程序集 Artifact ,但我没有更改任何从属模块,并且上次编译时该模块的 .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 文件以找到代码修订版吗? 提前致谢。 -赫菲斯托斯
最佳答案
只需使用带有 Android Studio 3.0+ 的 java 1.8 并为我设置以下工作: 似乎需要最新的构建工具
classpath 'com.android.tools.build:gradle:3.0.0'
和
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
}
}
关于java - Android:Dex 无法解析版本 52 字节码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37020413/