I want to ensure that Android developers are able to use a library and therefore I want to specify the Java/JDK version in the build.gradle file. The Java plug-in for Gradle offers some settings, but how can I do it with the Kotlin plug-in?
What library are you building? Is it targeted for Android only or intended to be used with full JDK also?
Which particular java plugin properties do you refer to?
I’m building KotlinTest. It should be usable for Android (Java 6) and usual development (Java 8). The properties of the Java plug-in I’m referring to are targetCompatibility and sourceCompatibility.
sourceCompatibility parameter of the java plugin specifies which version of java language is used in the source code. The parameter is used to constrain language features which can be utilized in the code, so that the code could be compiled with a compiler of the specified version.
This parameter usually govern how the code is compiled and has no effect on how the compiled binaries can be used later.
There is no such option yet in Kotlin, because there is only one version of the language available, but later (in Kotlin 1.1) when we introduce new language features, we’ll make an option to constrain their usage —
On the contrary
targetCompatibility is an important parameter for the library consumers as it specifies what version of JVM bytecode you’ll get your library classes compiled to. Kotlin currently can only compile classes to the bytecode of JVM 6, so that the resulting library code can be executed on Android.
Later we’ll make the compiler able to produce the bytecode of JVM 8, but the default will remain the same and a special
jvmTarget switch must be engaged to toggle this ability on.
However, specifying the bytecode version isn’t enough to ensure your library can be used on Android. You should also guarantee that neither your library, nor any of its dependencies do not use JDK API that is not available in Android SDK of the least version you target.
The most straightforward way to achieve this is to run a build tool you use, such as Gradle, under the JDK 6 specified in the JAVA_HOME variable, so that only JDK6 classes would be available in the classpath of the library being compiled. However it will only make you sure about your library, and not about its dependencies.
To be really sure you can invoke a task like
proguard several times with a different SDK you target the library to specified each time. Such task ensures that there is no unresolved API called in the compiled code.
Thanks for the explanation and the advice!