Android 错误的类文件魔术或版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24662801/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:34:01  来源:igfitidea点击:

Bad class file magic or version

androidintellij-ideaandroid-gradle-plugin

提问by Marco Acierno

I already know that question has been already asked very often and answers, but no one of the answers i found fixed my problem.

我已经知道这个问题已经经常被问到并得到了答案,但我找到的答案都没有解决我的问题。

It's the error:

这是错误:

Error:Gradle: Execution failed for task ':ffcommunity:preDexDebug'.
 com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    D:\Android SDK\sdk\build-tools.0.0\dx.bat --dex --output D:\Users\ReVo\Documents\IntelliJ IDEA\FFCommunity\ffcommunity\build\intermediates\pre-dexed\debug\bananaquery-2ee85432877a057e7414910b8127805535139d5d.jar D:\Users\ReVo\Documents\IntelliJ IDEA\FFCommunity\ffcommunity\libs\bananaquery.jar
Error Code:
    1
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
        at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
        at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
        at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
        at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
        at com.android.dx.command.dexer.Main.processClass(Main.java:665)
        at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
        at com.android.dx.command.dexer.Main.access0(Main.java:78)
        at com.android.dx.command.dexer.Main.processFileBytes(Main.java:572)
        at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
        at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
        at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
        at com.android.dx.command.dexer.Main.processOne(Main.java:596)
        at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
        at com.android.dx.command.dexer.Main.run(Main.java:230)
        at com.android.dx.command.dexer.Main.main(Main.java:199)
        at com.android.dx.command.Main.main(Main.java:103)
    ...while parsing com/comuf/revonline/bananaquery/BananaInsert.class
    1 error; aborting

The error is bad class file magic (cafebabe) or version (0034.0000).

错误是bad class file magic (cafebabe) or version (0034.0000)

I builded and execute the application without problems many times in the same day but now this fails everytime with this message.

我在同一天多次构建并执行该应用程序而没有出现问题,但现在每次都失败并显示此消息。



Project SDK is Android API 19 Platformand project level is 1.7.

项目 SDK 是Android API 19 Platform,项目级别是1.7.

CompileSDK is 19and buildToolsVersion is '20.0.0'.

CompileSDK 是,19而 buildToolsVersion 是'20.0.0'.

Compile options:

编译选项:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

and in dependenciesi have:

dependencies我有:

compile files('libs/bananaquery.jar')


The library is build with SDK Android API 19 Platformwith project level 1.7.

该库是Android API 19 Platform使用项目级别的SDK 构建的1.7

My .jarfile is inside libs/folder.

我的.jar文件在libs/文件夹内。

I use IntelliJ IDEA 14 Preview, it could be the reason? With IntellIJ IDEA 13 it remains stuck at "Gradle sync"

我使用 IntelliJ IDEA 14 预览版,这可能是原因吗?使用 IntellIJ IDEA 13,它仍然停留在“Gradle 同步”

compile files could be useless since i have compile fileTree(dir: 'libs', include: ['*.jar'])too

编译的文件可能是无用的,因为我有compile fileTree(dir: 'libs', include: ['*.jar'])太多

采纳答案by Marco Acierno

Ok, my bad.

好吧,我的错。

In the Project SDK section, when you add an Android SDKyou should provide the Java SDKand all my Android SDKs uses Java 8as SDK so it create the class files with the wrong version even if the Project level is 1.7(i don't know why, i supposed that everything was choosed by Project level).

在项目 SDK 部分,当您添加一个时,Android SDK您应该提供Java SDK并且我所有的 Android SDK 都使用Java 8作为 SDK,因此即使项目级别是1.7(我不知道为什么,我想)它也会创建具有错误版本的类文件一切都是由项目级别选择的)。

Now i changed the SDK (the java version "1.x.0"part.)

现在我更改了 SDK(java version "1.x.0"部分)。

enter image description here

在此处输入图片说明

and it seems to compile fine.

它似乎编译得很好。

The reason that worked before today was because my SDK was 1.8and not Android API x

在今天之前有效的原因是因为我的 SDK 是1.8而不是Android API x

回答by passsy

my JAVA_HOMEvariable changed to Java 1.8 and I got this error message when compiling a pure java module as a dependency of my android project.

我的JAVA_HOME变量更改为 Java 1.8,并且在将纯 Java 模块编译为我的 android 项目的依赖项时收到此错误消息。

build.gradle of the java module

java 模块的 build.gradle

apply plugin: 'java'

Solution #1: Quick an dirty

解决方案#1:快速变脏

I fixed it by setting my JAVA_HOMEback to 1.7:

我通过将JAVA_HOME返回设置为 1.7 来修复它:

export JAVA_HOME=`/usr/libexec/java_home -v 1.7`

Solution #2: change compiler version:

解决方案#2:更改编译器版本:

change back to 1.7 for this specific module in its build.gradle

将此特定模块的 build.gradle 改回 1.7

apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7

回答by Chris Lacy

In case people find @Marco Acierno's answer to be a bit unclear, the solution is to ensure you're building with Java 7 and not a higher version.

如果人们发现@Marco Acierno 的回答有点不清楚,解决方案是确保您使用 Java 7 而不是更高版本进行构建。

For Android Studio, change File -> Project Structure -> SDK Location -> JDK Locationto jdk1.7.x. For the command line, ensure java -versionoutputs java version "1.7.x".

对于 Android Studio,更改File -> Project Structure -> SDK Location -> JDK Locationjdk1.7.x. 对于命令行,确保java -version输出java version "1.7.x"

回答by Vishal Singh

Setting JAVA_HOME back to 1.7 worked for me.

将 JAVA_HOME 设置回 1.7 对我有用。

回答by baiiu

change all your module java version to java 1.7 in every build.grade file.

在每个 build.grade 文件中将所有模块 java 版本更改为 java 1.7。

in plugin which is library and application

在插件库和应用程序中

compileOptions{
    sourceCompatibility=JavaVersion.VERSION_1_7
    targetCompatibility=JavaVersion.VERSION_1_7
}

and in java

在Java中

sourceCompatibility= 1.7
targetCompatibility= 1.7

回答by Bugs Happen

This problem occurs when you use a .jarfile that is not using any feature of Java 6 or higher but was built using Java 6 or higher.

当您使用的.jar文件不使用 Java 6 或更高版本的任何功能但使用 Java 6 或更高版本构建时,会出现此问题。

If you built that .jarfile then you don't need to change anything in Gradleor ProGuardor Compiler Version. The solution is very simple, just build that .jarfile again but using Java 5 or less.

如果您构建了该.jar文件,那么您无需更改GradleorProGuard或 中的任何内容Compiler Version。解决方案非常简单,只需再次构建该.jar文件,但使用 Java 5 或更低版本

More details.

更多详情

回答by RickPat

had a similar issue when i tried to add a self made library from netbeans to android studio. setting the source- and target compatibility in android studio and source/binary format in netbeans (both!) to java 1.7 solved the problem.

当我尝试将自制库从 netbeans 添加到 android studio 时遇到了类似的问题。将android studio中的源和目标兼容性以及netbeans中的源/二进制格式(两者!)设置为java 1.7解决了这个问题。

in android studio:

在安卓工作室:

Project Structure -> Modules/App -> Propreties -> Source- and Target to 1.7

项目结构 -> 模块/应用程序 -> 属性 -> 源和目标到 1.7

in netbeans:

在网豆中:

File -> Project Properties -> Sources -> Source/Binary Format to 1.7

File -> Project Properties -> Sources -> Source/Binary Format to 1.7

then clean and build your netbeans projekt and copy the .jar from "NBProj/dist" to "app/libs"

然后清理并构建您的 netbeans 项目并将 .jar 从“NBProj/dist”复制到“app/libs”

回答by spaceMonkey

I had a similar issue, i solved it by upgrading my proguard. get your proguard version by this command

我有一个类似的问题,我通过升级我的 proguard 解决了它。通过此命令获取您的 proguard 版本

java -jar ~/android-sdks/tools/proguard/lib/proguard.jar 

get the latest progaurd.jar file from here (http://proguard.sourceforge.net)

从这里获取最新的 progaurd.jar 文件(http://proguard.sourceforge.net

replace the existing android-sdks/tools/proguard/lib/proguard.jar with new .jar file.

用新的 .jar 文件替换现有的 android-sdks/tools/proguard/lib/proguard.jar。

Hopefully this should help you. If u using java 8 then you should upgrade to proguard 5.x coz proguard 4.x does not support java 8.

希望这应该对您有所帮助。如果你使用 java 8,那么你应该升级到 proguard 5.x 因为 proguard 4.x 不支持 java 8。

回答by suiwenfeng

share the solution of the case if only Java8 installed, just set Java compiler level to 1.7 and then rebuild the project should be OK.

分享一下如果只安装了Java8的情况下的解决方法,只要将Java编译器级别设置为1.7,然后重新构建项目应该就可以了。