I’m trying to develop a mental model around Java source compatibility, which you can set in your And...
k
I’m trying to develop a mental model around Java source compatibility, which you can set in your Android’s Gradle build file. Relates to `sourceCompatibility`/`targetCompatibility`/`jvmTarget` . 🧵
I understand what each option sets for JVM application, but can’t tell why it’s counterintuitive for Android. For example: in JVM, a library sets targetComp = 17. A project consuming that library, using sourceComp = 11, will fail to run, due to a difference in binary format (different version compiled vs. lower version trying to use it). That’s understandable. But doing a similar experiment for Android works just fine. I created an app and set the sourceComp = 11. I created a module targetingComp = 17 and added it to the app as a dependency (aar). I expected this to fail, because .aar contains .java classes compiled against Java 17, while running the project with Java 11, but it’s running just fine. Am I missing something here?
k
Yeah, I’ve read it before. My question boils down to this: Why my app (toolchain set to 11) can use a library built with toolchain 17 and not fail?
m
D8 desugaring the Java17 bytecode to dex?
l
If I understand you correctly, and I am not entirely sure what's the status on android, but we know that google is backporting java features through desugaring and recently also shipping features to older android releases through google play (I forget the name now)
k
D8 desugaring the Java17 bytecode to dex?
That’s the one! I just tried d8 from build tools 34 and it compiled the Java 17 class. Using v30 throws an error
👍 1
If I understand you correctly, and I am not entirely sure what’s the status on android, but we know that google is backporting java features through desugaring and recently also shipping features to older android releases through google play (I forget the name now)
I think you mean project Mainline
👌 1
g
Maybe the recently added official documentation on Java versions can help with some context too https://developer.android.com/build/jdks
k
It did shed some light into it, for sure, but the final piece of the puzzle was D8 desugaring