7

Android Studio 2.3.3; Kotlin Plugin: 1.1.4; Kotlin Compiler: Kotlin to JVM: 1.6.

Android Studio > New Project > Configure Kotlin in Project action adds reference for kotlin-stdlib-jre7 to gradle file. I would expect kotlin-stdlib as we Kotlin target JVM is 1.6.

Have not found good explanation. E.g. this answer states that

The kotlin-stdlib-jre7 artifact is not supposed to work on Android, which currently supports only Java 6 platform. When targeting Android, you should use kotlin-stdlib, which is built for JRE 6

user2051551
  • 167
  • 3
  • 10

1 Answers1

6

Android supports Java 7 for some years already. In fact, partial support for Java 8 is on the way, at the beginning by means of Jack toolchain and currently by means of desugaring in the default toolchain.

So, kotlin-stdlib-jre7 is perfectly fine.

As you can see in this answer, kotlin-stdlib got two additional extension libraries kotlin-stdlib-jre7 and kotlin-stdlib-jre8 in 1.1. This libraries add specific features for Java 7 and Java 8, and include kotlin-stdlib as a transitive dependency. See also the Kotlin 1.1 release notes:

Java 8 standard library support

There are now separate versions of the standard library supporting the new JDK APIs added in Java 7 and 8. If you need access to the new APIs, use kotlin-stdlib-jre7 and kotlin-stdlib-jre8 maven artifacts instead of the standard kotlin-stdlib. These artifacts are tiny extensions on top of kotlin-stdlib and they bring it to your project as a transitive dependency.

Kotlin plugin in current versions of Android Studio add kotlin-stdlib-jre7 as a dependency when configuring Kotlin in the project, as Android Studio and build tools support Java 7 in a backward compatible way since Android Studio 0.3.2, released in October 2013. See also Does Android support JDK 6 or 7. Specifically, release notes state:

The main feature in this release is support for Android KitKat:

Support for language features like the diamond operator, multi-catch, try-with-resources, strings in switches, etc. When creating a new project, you can specify a source language level:

And:

Note that you can use all these language features not just with Android KitKat, but with older versions of Android too! There is one exception to that: try-with-resources, which will require minSdkVersion 19.

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
  • You state "kotlin-stdlib-jre7 is perfectly fine", the answer above states "kotlin-stdlib-jre7 is not supposed". Actually I'm looking some reference to explain the rule when to use kotlin-stdlib or kotlin-stdlib-jre7 – user2051551 Aug 30 '17 at 10:32
  • I'm using `kotlin-stdlib-jre7` in my projects, that's why I can confirm it works. That being said, I don't know why the answer you linked is saying that Android doesn't support Java 7. As I stated in my answer, there is even partial support for Java 8 in beta (through "desugar" process, which translates Java 8 language features to Java 7). Also, here you have another SO question saying that it's supported: [https://stackoverflow.com/questions/20480090/does-android-support-jdk-6-or-7](Does Android support JDK 6 or 7). Hope this makes things more clear. – Xavier Rubio Jansana Aug 30 '17 at 12:29
  • Updated the answer with the reason about why `jre7`. – Xavier Rubio Jansana Aug 30 '17 at 12:35
  • 1
    `kotlin-stdlib` was not _split_ in 1.1. These `kotlin-stdlib-jre7` and `-jre8` are additional artifacts which contain new API. – Ilya Aug 31 '17 at 01:47
  • 1
    Kotlin Language Documentation snippet above is not Android specific. Docs are general for all Kotlin supported platforms. AFAIU kotlin-stdlib-jre7/kotlin-stdlib-jre8 are right choice when you are targeting JDK 7 or JDK 8. I'm targeting Java 6. `If you're targeting JDK 7 or JDK 8, you can use extended versions of the Kotlin standard library which contain additional extension functions for APIs added in new JDK versions. Instead of kotlin-stdlib , use one of the following dependencies` – user2051551 Aug 31 '17 at 07:27
  • Ok, I assumed Android as you where talking about Android Studio. But fair enough, if you're targeting Java 6, then agree, the right library is `kotlin-stdlib`, not the `-jre*` ones. I've also updated my answer with all the discussion and additional notes on why I use Java 7. – Xavier Rubio Jansana Aug 31 '17 at 08:06
  • 1
    Yes, we are talking about Android and project minSdkVersion is 21. I'm targeting Java 6 in term of Kotlin. Project settings: Kotlin compiler targets JVM version 1.6. I assume this means that compiler will produce Java 6 bytecode and Java 7 libraries are not applicable in this case. Really thanks for your help. I might be not smart enough to understand. I will accept your answer if will not get alternative explanations. – user2051551 Aug 31 '17 at 09:22
  • Basically that should be it. The topic is tricky because Dalvik/ART are not JVM, but use different bytecode, which doesn't always match 100% with JVM capabilities for a given Java level. So, given you're targeting API 21, you could target both Java 7 and Java 6, including `try-with-resources`. And thanks, really enjoyed the discussion. – Xavier Rubio Jansana Aug 31 '17 at 09:35
  • 1. Kotlin compiler suggests only 2 target JVMs (1.6 or 1.8). There is no 1.7 option. 2. I thought Android Studio facility to use Java 7/8 language features and project result bytecode version are different stories. – user2051551 Aug 31 '17 at 11:03
  • Yes, I was speaking about using `kotlin-stdlib-jre7` here. And, about bytecode, I cannot say exactly how it's transformed from Java .class bytecode (which is stack-based VM) to DEX bytecode (register-based VM), but I know that it's not a 100% 1:1 match. – Xavier Rubio Jansana Aug 31 '17 at 11:56