13

I have a java module in my Android Studio project that is a dependency of an Android module. I am having problems on build with the following exception appearing.

Error:Execution failed for task ':myApplication:preDexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    /Applications/Android Studio.app/sdk/build-tools/android-4.4W/dx --dex --output ~/myapplication-app-android/dev/biketracks-app-android/bikeTracks/build/intermediates/pre-dexed/debug/coreUtilities-6ee7e0aafa5a6db72b2acb078f065e51c43124c2.jar ~/myapplication-app-android/dev/myapplication-app-android/libs/coreUtilities/build/libs/coreUtilities.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.access$600(Main.java:78)
        at com.android.dx.command.dexer.Main$1.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/corecoders/coreutilities/GPSUtils.class
    1 error; aborting

After some reading I can see that it is something to do with the Java compiler Android Studio is using. However I cannot see a way to change which compiler it uses.

The Java module I am trying to include in my Android module is one I created using Android Studio by going File > New Module > Java Module so I can't see any option I could make different?

Any ideas would be great.

StuStirling
  • 15,601
  • 23
  • 93
  • 150

2 Answers2

25

So I have found the solution at this blog post.

The trick is in your Java Library module's build.gradle file you need to include the following.

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

This will then work.

StuStirling
  • 15,601
  • 23
  • 93
  • 150
4

Seems things have changed on newer versions of Gradle / Android Studio, so the above solution about selecting source compatibility alone may not suffice. Particularly for complex projects which have a mix of modules which apply more than the simple android plugin ( I have seen following three plugins used on modules of same project: 'android' , 'java' and 'android-library')

You need to make sure that the following things are satisfied if source compatibility alone does not resolve your issue.

1) For you modules which apply plugin: 'android' select the source compatibility inside your build.gradle:

android {

      //... other sections.
compileOptions {

        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

2) Select Project Byte code version from: File -> Other Settings -> Default settings.

project bytecode version

3) Explicitly select the JDK environment: File -> Project Structure -> SDK location and set it to the JDK 7 folder.

explicit JDK selection

--Update: with the new Android Studio 1.2.x they changed the location where you can select the Java byteCode version to the following: File->Other Settings->Default Settings->Build , Executions Enviromnent-> Compiler.

enter image description here

Nilesh Pawar
  • 3,895
  • 3
  • 21
  • 19
  • I literally can't believe it. Even this did not solve the error for me. I am 6 hours deep and have tried countless fixes for this problem... – Shadoninja Jan 10 '16 at 06:33
  • @Shadoninja is your project a multi-module project? Can you try with a simple HelloWorld app to find if there is nothing strange with your developer environment? – Nilesh Pawar Jan 12 '16 at 16:27
  • I figured it out. An error my my end, but Android gave me some pretty rough error messages which led me down a very wrong path. Here is my question and answer. Thanks for replying. https://stackoverflow.com/questions/34703059/unable-to-fix-parseexception-bad-class-file-magic-cafebabe-or-version-0034?noredirect=1#comment57152088_34703059 – Shadoninja Jan 12 '16 at 21:48