2

I'm working an android project where the model is written in java alone and a dependency for the rest of the project (view, controller), written in "android-java".
I have recently switched from android studio 1.3 to 2.1 and begun to use java 8 in the model. It compiles fine insofar as rebuilding the project throws no errors.
(I occasionally get "Lambda expressions are not supported at this language level", but this can be resolved by clicking on the red light bulb which offers to set the language level to 8)
Now, when I run the app, I get ~40 errors that look like this one(with another class file each):

...while parsing /foo/ba/xy.class
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.

This question is very similar, however I can specify that the source of my errors contains no android specific code (and have thus no android-block, in my gradle file).
An obvious choice is now to add

targetCompatibility = '1.7'
sourceCompatibility = '1.7'

to that submodule's build.gradle file.
This beggs the first question: Why sourceCompatibility = '1.7'? I want to use java 8!
But regardless, I don't know how to add these 2 lines the the build.gradle file. The file in question looks like this:

apply plugin: 'java'

dependencies {
    testCompile 'junit:junit:4.7'
    testCompile 'org.apache.directory.studio:org.apache.commons.io:2.4'
    testCompile 'org.kie.modules:org-apache-commons-lang3:6.3.0.CR1'
}

Attempt 1: add both lines directly in line 2
get "Lambda expressions are not supported at this language level" again, click red bulb again, 1.7 is changed to 1.8 in both lines.
rebuild succeeds, try running.
get 107 errors as above suggesting I set both to 1.7.
Set targetCompatibility to 1.7 and sourceCompatibility to 1.8 because it seems logical: get

Error:Execution failed for task ':xxxxmodel:compileJava'.
> Compilation failed; see the compiler error output for details.

Gradle window says

javacTask: target release 1.7 conflicts with default source release 1.8

I can't find the compiler error output however.

Attempt 2: add

apply plugin: 'com.android.application'

android {
    targetCompatibility = '1.7'
    sourceCompatibility = '1.7'
}

Get Error:Cause: buildToolsVersion is not specified.

Community
  • 1
  • 1
peer
  • 4,171
  • 8
  • 42
  • 73
  • The Android SDK can't compile java 8 code. That simple – OneCricketeer May 02 '16 at 00:51
  • Please post your code. – Lips_coder May 02 '16 at 06:11
  • @cricket_007 I know that I can't use java 8 in android itself, but when using a library it shouldn't matter what java version it was written in, right? – peer May 02 '16 at 12:24
  • It does matter if that library was compiled in Java 8. – OneCricketeer May 02 '16 at 12:52
  • @cricket_007 that is sad to hear. I had assumed bytecode to be invariant of the version. – peer May 02 '16 at 14:24
  • @cricket_007, "The Android SDK can't compile java 8 code. That simple" - that's not true anymore. See https://developer.android.com/preview/j8-jack.html But I can't recommend jack for lambdas. Personally still use retrolambda for that – Deepscorn Jun 15 '16 at 14:42
  • @Deepscorn - I am aware that the Jack compiler in still in Preview stages – OneCricketeer Jun 15 '16 at 15:07
  • @cricket_007, I don't use jack. You wount find jackOptions { enabled true } in my configuration, but java version is set to JavaVersion.VERSION_1_8 Example: https://github.com/Deepscorn/libgdx-gradle-template/blob/master/android/build.gradle – Deepscorn Jun 15 '16 at 15:36

1 Answers1

0

I was facing the similar issue.

In build.gradle file where

apply plugin: 'java'

is written, just replaced the same with

allprojects {
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
Deepanshu
  • 209
  • 1
  • 2
  • 8