简体   繁体   中英

Wrong Java Compiler When Including a Java Module as Dependency in Android Studio

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.

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.

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.

项目字节码版本

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

显式JDK选择

--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.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM