7

Let's say I am making an Android library (to be distributed as a .aar) and I want to use Java 8 language features in my internal library code. Basic stuff like lambdas, etc.

In my experience, if I then assemble and publish this library any developer that relies up on it will get errors in build unless they add this to their build.gradle:

android {
  // ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Is there any way to compile my library so that consumers don't have to do this? Assume that the public API surface of the library does not contain any Java 8 specific features.

Sam Stern
  • 24,624
  • 13
  • 93
  • 124
  • 1
    in short: no this is not possible. – Timothy Truckle Mar 27 '18 at 20:33
  • @TimothyTruckle do you have any explanation or a link to where I could learn more about this? I was surprised to see this hit me, for instance when I upgraded to version `3.2.0` of the Twitter SDK (non-major upgrade) I suddenly also needed Java 8. – Sam Stern Mar 27 '18 at 20:38
  • 1
    Think about what is the AAR file which are class files not DEX files. Referring to: https://developer.android.com/studio/write/java8-support.html the 'desugaring' process occurs before dex-ing and I suspect that not modifying what the AAR carries will allow for improvements/modifications on the toolchain easier. – Morrison Chang Mar 27 '18 at 21:08
  • 2
    You favorite search engine should have come up with this link: http://www.oracle.com/technetwork/java/javase/8-compatibility-guide-2156366.html *"The class file version for Java SE 8 is 52.0 as per the JVM Specification. Version 52.0 class files produced by a Java SE 8 compiler cannot be used in earlier releases of Java SE."* – Timothy Truckle Mar 27 '18 at 22:44
  • Thanks @MorrisonChang and TimothyTruckle for the explanation, I wasn't thinking about where the dexing occurs in the chain. – Sam Stern Mar 27 '18 at 23:00
  • See also [this answer](https://stackoverflow.com/a/23757061/1063673) – Steffen Harbich Mar 29 '18 at 08:30

0 Answers0