Android app + Java module (Java 1.8)

Android Project (gradle) has the main application module: application - application for Android (use the plugin: "com.android.application") and java module: network - (use the plugin: 'java')

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

But the Android 1.7 application is by default, and I cannot create the application. Of course, I use retrolambda in my application - the android module, but the project does not compile due to:

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 if I create my java module:

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

But in this case, I cannot use lambda expressions, but I need it.

I see only the following solutions:

  • Create a network module, not java, but android-module and use retrolambda (But I'm going to make .jar not .aar from this module, so it should be Java)

  • Do not use lambda expressions in my Java module (.jar in the future) (but I want to use it because of too matching stupid code in rxJava without lambda)

The best solution for me would be something like retrolambda in my Java module. I tried to do something similar, but the dependencies (depending on the plug-in: "me.tatarka.retrolambda") that were retroamba dependent were not affected by errors during compilation. Or maybe use java 1.7 with something like retrolambda. But the gradle file does not have an android {...} section to set compileOptions.

How to do it?

+5
source share
1 answer

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

 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 what it takes to get your output jar aimed at the right version of java.

+3
source

Source: https://habr.com/ru/post/1241434/


All Articles