Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 955
StuStirling Avatar asked Jul 14 '14 17:07

StuStirling


People also ask

Where is compiler error output Android Studio?

Go to File > Settings > Build, Execution, Deployment > Compiler.

How Java code is compiled in Android?

Java byte-code file (TestClass. class) ends up in JVM (Java Virtual Machine). JVM understands byte-code and converts it into machine code, using JIT (Just-In-Time) compiler. The machine code is then fed to the memory and executed by computer's central processing unit.

What is IDE max heap size Android Studio?

By default, Android Studio has a maximum heap size of 1280MB.

Where to add dependencies in Android?

Go to File > Project Structure in Android Studio. Select the app module in the Modules list on the left. Select the Dependencies tab. Click the + button on the lower left to add the dependency.


2 Answers

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.

like image 113
StuStirling Avatar answered Oct 22 '22 07:10

StuStirling


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

like image 41
Nilesh Pawar Avatar answered Oct 22 '22 07:10

Nilesh Pawar