Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Application + Java Module (Java 1.8)

Android Project (gradle) has main application module: app - android application (apply plugin: 'com.android.application') and java-module: network - (apply plugin: 'java')

java module network has source code compatibility 1.8 by default and I really want to have it because of lambda expressions.

But Android application 1.7 by default and I can not build the app. Of course I use retrolambda in my app - android module, but project doesn't compile because of:

Error:com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

Everything compiles good if I make my java module:

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

But in this case I can not use lambda expressions but I need it.

I see only next solutions:

  1. Make network module not java but android-module and use retrolambda (But I'm going to make .jar not .aar from this module so it has to be java)

  2. Don't use lambda expressions in my java module (.jar in future) (But I want to use it because of too match stupid code in rxJava without lambda)

The best solution for me would be something like retrolambda in my java module. I tried do something like that but retroambda dependencies (apply plugin: 'me.tatarka.retrolambda') was not affected errors during compilation. Or maybe use java 1.7 with something like retrolambda. But gradle file has no section android {...} to set compileOptions.

How to do this?

like image 650
Yura Buyaroff Avatar asked Oct 19 '22 17:10

Yura Buyaroff


1 Answers

Based on what I did for FunctionalIterables, your gradle file should look like that:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.2.3'
    }
}

repositories {
    jcenter()
}

apply plugin: 'java'
apply plugin: 'me.tatarka.retrolambda'

retrolambda {
    jdk System.getenv("JAVA8_HOME")
    oldJdk System.getenv("JAVA7_HOME")
    javaVersion JavaVersion.VERSION_1_6
}

(The rest of my gradle file is only related to publishing the library)

Retrolambda does everything needed to make your output jar target the right java version.

like image 125
njzk2 Avatar answered Oct 24 '22 03:10

njzk2