I am using retrolambda and some tests got me quite confused.
My understanding is:
- with retrolambda I can use some java8 language features (e.g. lambdas), but not java 8 classes: e.g. without any additional libraries, I cannot use the java8 streams: HashSet<>.stream() is not available
- I write my code with java-8 features (lambdas). The source code is translated into java8-class files and then retrolambda kicks in and converts the java8-class files to java7-class files which can be executed on older Android version (e.g. Android 6)
So, I've built a small test-project to test my assumptions and there are some things I am not sure of. I hope someone can clarify. Heres a tag of my source code, that I will refer to.
- in a multimodule android application (one android module and a java-only module), we must apply the retrolambda gradle plugin to all modules (app and java) that want to use java8 features
- first the java module will be compiled by javac to java8-class files, retrolambda will convert them to java7 and a jar is built
- then the app module will be compiled the same way as the java module: but now we get the apk instead of a jar: everything is java7 and can happily be used on Android 6
- In my source code I have some unit tests, that deliberately use java8 code that should not work: I call
HashSet<>.stream()
- Android studio and the Emulator: All tests work
- I guess that Android Studio & Emulators do not even use retrolambda at all and just compile and run everything to java8
- Is there a way to change that?: e.g. it would be good if any of the tests already catch these problems
- Gradle command line build:
gradle clean app:test
: The unit tests workgradle clean java:test
: The unit tests fail
- I have no idea why the tests in the app module work and those in the java module fail: why is that?
- Android studio and the Emulator: All tests work
- On a real device with Android 6 everything crashes, as expected