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

Hep*_*tus 93 java android maven android-studio

我刚刚切换到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.
Run Code Online (Sandbox Code Playgroud)

我已经更新了主项目的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
    }
}
Run Code Online (Sandbox Code Playgroud)

我还更新了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
}
}
Run Code Online (Sandbox Code Playgroud)

子模块是用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>
Run Code Online (Sandbox Code Playgroud)

我的问题:1)这是Android Studio 2.1问题吗?有别人看过吗?2)假设这是我的错误,并且由于错误消息没有帮助找到坏模块,是否有任何关于查找V52代码的建议?我不能在不破坏大量代码的情况下省略库.可以检查一个.jar文件来查找代码修订版吗?提前致谢.-Hephaestus

小智 87

只需使用带有Android Studio 3.0+的java 1.8并为我设置以下作品:它似乎需要最新的构建工具

classpath 'com.android.tools.build:gradle:3.0.0'
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 显然不再需要dexOptions.incremental,因为它默认为true,请参阅/sf/ask/2626586791/ (7认同)
  • 我编译的代码与Java 8而针对Android的N,但没有问题,跑在Android上16个应用程序.你可以自己测试一下 (3认同)
  • 请注意"不推荐使用``android.dexOptions.incremental`属性,它对构建过程没有影响." (3认同)

nex*_*Dev 16

如果您的模块具有非特定于Android的Java库,则应该可以:apply plugin:'java'

将它放在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
    }
Run Code Online (Sandbox Code Playgroud)

  • 错误:已应用'java'插件,但它与Android插件不兼容. (16认同)
  • 如果您的模块具有非特定于Android的Java库,那么这是正确的解决方案. (2认同)

0wl*_*0wl 8

如果你使用org.jetbrains:annotation:15和retrolambda插件然后删除compile org.jetbrains:annotations:15.0你的行,build.gradle错误将消失.这个对我有用.


fob*_*o66 7

可能,您的一些依赖项是使用Java 8编译的,而不是针对Android编译的.尝试将该依赖项切换为旧版本.我不确切地知道您应该降级哪个库,因为您没有附加主模块的依赖项列表.

例如:我遇到了同样的问题.经过几个小时的搜索,我发现该库org.codehaus.httpcache4j.uribuilder:2.0.0需要Java 8,从github开始.因此,当我切换到时1.1.0,项目已成功构建和部署.


vit*_*ent 7

尝试在allprojects部分中添加到main build.gradle

tasks.withType(JavaCompile) {
    sourceCompatibility = "1.7"
    targetCompatibility = "1.7"
}
Run Code Online (Sandbox Code Playgroud)

或者在依赖项中添加它

    sourceCompatibility = 1.7
    targetCompatibility = 1.7
Run Code Online (Sandbox Code Playgroud)

所有模块中手动


小智 7

我能够通过添加以下行来解决此问题:

jackOptions {
    enabled true
}
Run Code Online (Sandbox Code Playgroud)

defaultConfigbuild.gradle文件中.

您可以在链接上遵循Java 8指南 - https://developer.android.com/guide/platform/j8-jack.html


小智 5

我对greendao-generator依赖有同样的问题.我错误地将依赖添加到我的build.gradle(compile 'org.greenrobot:greendao-generator:3.1.0')和AndroidStudio中,向我显示了相同的错误消息.

可能是因为该模块已使用Java 8编译.

所以我从build.gradle中删除了这个依赖项,所有编译都愉快地:)