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.