Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

android studio - Unable to fix "ParseException: bad class file magic (cafebabe) or version (0034.0000)"

I recently imported a jar from my AWS application that contains all the objects I am going to be referencing on my Android application. As soon as that is in my Android Studio project and hooked up via Gradle, the error shows up and doesn't go away until I clear out my imported jar and rebuild everything.

What I read:

This error can be legitimate. This can happen when an Android application surpasses 65k method names. My application may be big, but I would be amazed if all the libraries I imported totaled over that.... I won't rule it out, but that is a lot of methods. From what I saw, nine times out of ten, this was a configuration bug. The first couple articles I read said to enable Proguard to remove all unused methods, but that didn't help.

My configuration:

I have everything on my Android Studio environment set to run Java 7:

  • Project bytecode version: 1.7
  • JDK location: C:Program FilesJavajdk1.7.0_45

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.maveric.helloworld"
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:4.2.42'
    compile 'com.android.support:appcompat-v7:20.0.0'
    compile 'org.apache.commons:commons-io:1.3.2'
    compile 'com.cedarsoftware:json-io:4.3.0'
}

I have spent the last 5 hours trying every single solution on SO, and haven't had a single bit of luck with any of them.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After over 6 hours of trying solutions, the one Oleg linked me in the comments finally led me to the real cause of the problem.

The brief explanation of the issue can be found HERE, but I will try to explain how everything looked on my system:

This error first showed up for me when I imported a jar file that was part of my main Java project, which was hosted in Eclipse.

The way this error first pokes up to the user is through the Messages tab where Gradle walks through each of its tasks. The task that it crashes on is

:app:transformClassesWithDexForDebug

The problem is the Gradle Messages window just says the task failed and then gives a non-descriptive error message saying

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:Program FilesJavajdk1.7.0_45injava.exe'' finished with non-zero exit value 1

In order to get a better view, you need to go look at the Gradle Console view. The text in the terminal view tells you this:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:Program FilesJavajdk1.7.0_45injava.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Scrolling up a little bit shows that the stacktrace came through just fine, even though this initial message hints that there should be additional flags added for debugging. The top of the stacktrace says this:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)

But even that doesn't help very much. What it is trying to tell you (very poorly I have to add) is that the class it is looking at was compiled with a Java version that is not supported. In my case, version (0034) was the hex representation of 52 which means Java 8. Android doesn't support Java 8, so it terminates the build.

The solution was to go back to my Eclipse project and export the jar file with the compiler set to version 1.7 and now everything is up and running again. I hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...