7

I use Java 1.8 to create my jar.

I can use it in a Java project, but in an Android project, I have the following error:

Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:1 error; aborting
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Android-tool\JAVA\jdk1.8.0_77\bin\java.exe'' finished with non-zero exit value 1
Information:BUILD FAILED
Information:Total time: 6.468 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console
IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
jevely
  • 172
  • 3
  • 10
  • what version of Java is used there? Have you read the error message and tried the solutions it suggests? – Stultuske Apr 06 '16 at 08:14
  • Probably, your question is a duplicate to http://stackoverflow.com/questions/23318109/is-it-possible-to-use-java-8-for-android-development – GhostCat Apr 06 '16 at 08:17

4 Answers4

0

Add these things to the module's build.gradle:

android {
    buildToolsVersion "24rc4"

    defaultConfig {
        jackOptions {
            enabled true
        }
    }
    dexOptions {
        incremental true
    }

    compileOptions {
        sourceCompatibility = org.gradle.api.JavaVersion.VERSION_1_8
        targetCompatibility = org.gradle.api.JavaVersion.VERSION_1_8
    }
}
toddmo
  • 20,682
  • 14
  • 97
  • 107
0

Fell into the same situation. Here's what I did. 1. Download Jdk 1.7 from this link Oracle Java 1.7

  1. Now, after downloading and installing it go to system properties (My computer) and then to advanced system properties.
  2. Go to environment variables and change the path home and java home to your newly installed jdk 1.7.

It should work fine now. If not then follow the below steps:

  1. Switch to Android studio and then right click on your module. Go to module settings/project structure.
  2. now go to modules section and specify the source and target compatibility to 1.7

Works like a charm for me. Ps. - Sorry for such a late answer

Han
  • 575
  • 8
  • 21
0

In case of a Java library in a multi-app project, use code mentioned bellow in the library's build.gardle file.

apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
compileJava.options.bootClasspath = "D:/android/sdk/platforms/android-23/android.jar"
...
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
...
}

You will need to change compileJava.options.bootClasspath with your local path to android.jar file. This works for me in AS 2.2 on Win10. for more references please visit here

Aaditya Dengle
  • 136
  • 1
  • 8
-1

If self explain in the error:

This is caused by library dependencies that have been compiled using Java 8 or above. If you are using the 'java' gradle plugin in a library submodule add targetCompatibility = '1.7' sourceCompatibility = '1.7' to that submodule's build.gradle file.

Miguel Benitez
  • 2,322
  • 10
  • 22