0

I try to correctly configure a multi project build with gradle.

The difficulty is that I have three projects and want to build an Android application as well as a normal Java8 application (runnable jar). Therefore, I have the Android project, the Java8 Application project and a plain java project with common code both applications share.

With the following setup, I can compile both applications.

  • the Android project applies the "com.android.application" plugin and has a dependency on the Common project
  • the Java Application project applies the "application" plugin and has a dependency on the Common project
  • the Common project applies the "java" plugin

In order to use lambda expressions, I'm using retrolambda in the Android project and Java 8 in the Java Application. Obviously, I cannot use full Java8 in the Common project, as this would break the Android application.

To be able to use lambda expressions in the Common project, I have to set the sourceCompatibility to Java 1.8. However this also makes it possible to use e.g. the Java8 Stream APIs in the Common project. When I then compile the Android project, I do not get a compile error. Obviously, the Android app will then crash at runtime.

So my question is: how can I configure the Common project to be able to use it in the standard Java8 application as well as in the Android application but get compile errors when I use stuff I cannot use on Android.

Hash
  • 4,647
  • 5
  • 21
  • 39
andy
  • 1,852
  • 2
  • 20
  • 31
  • So, you want Gradle to just "know" when you are using a feature not supported by Android? Are you using the Jack compiler, because that does support Java 8 compilation – OneCricketeer Aug 23 '16 at 20:54
  • Well the obvious solution is just not to use lambdas and streams in the common project. Then you have no hoops to jump through. Is it really that hard to write a for loop instead? – Gabe Sechan Aug 23 '16 at 20:54
  • Or replace Steams for RxJava Observable – OneCricketeer Aug 23 '16 at 20:55
  • I wonder if it is possible to configure the Common project to be compiled as Android library when it is included by the Android application and as a normal Java8 project when it is included by the Java8 application. And yes, I want to use lambdas. – andy Aug 23 '16 at 21:00

0 Answers0