Getting around "Bad Class File Magic" with only JDK 1.7 installed

  • Published: 25 Sep 18:53

I had to use a pure Java library for an Android project I'm working on. I built the library from source with ANT and referenced it from my project. Once I launched the app I got the infamous:

Unfortunately, App has stopped

I decided to run the gradle build from command line and look if there is anything in the log. And no surprises, I got the following error per each class in the library:

[dx] trouble processing:
[dx] bad class file magic (cafebabe) or version (0033.0000)
[dx] ...while parsing XXXXXXX.class

After searching the net, I realized that it might have something to do with Java versions. Android uses JDK 1.6 and I got JDK 1.7 installed, which means that the library was built with JDK that isn't compatible with Android.

I decided to drop the ANT build.xml file and create a Maven build which uses the maven-compiler-plugin to set the compiled classes to Java 1.6.

‚Äč


    4.0.0
    com.example
    library
    1.0.0
    jar
    Pure Java lib

    
        UTF-8
    

    
        
            
                maven-compiler-plugin
                3.1
                true
                
                    1.6
                    1.6
                
            
        
    

After running `mvn package` I pushed the jar file to my app and now everything is working again! :)