Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict kotlin from using java api which is announced above jdk1.6

My compilation environment is jdk1.8 and runtime environment is jdk1.6. java plugin of gradle has sourceCompatibility attribute. It is valid for java project.
For example: when sourceCompatibility=1.6, compiler will report error if I use the api such as Paths which is from jdk1.7.

but sourceCompatibility attribute doesn't work for kotlin project. I understand it is out of java plugin's scope. But I am strange whether kotlin plugin of gradle hasn't similar attribute. (jvmTarget attribute is 1.6 by default, it does't prevent me from using jdk1.7 api)

=== my code ===

kotlin code:

fun main(args: Array<String>) {
    val pp = Paths.get("/tmp")
    ... ...
}

I want kotlin's compiler to report errors, but the compilation is successful,

parent build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.0"
    }
}
subprojects {
    apply plugin: "java"
    compileJava.sourceCompatibility=1.6
    compileJava.targetCompatibility=1.6
    compileJava.options.encoding = 'UTF-8'

    // For ubuntu
    compileJava.options.bootClasspath = '/usr/lib/jvm/java-6-oracle/jre/lib/rt.jar'
}

child kotlin project build.gradle:

apply plugin: 'application'
apply plugin: 'kotlin'

mainClassName = 'net.cat.ApplictionKt'
version='1.0'

jar {
    manifest {
        attributes 'Implementation-Title': 'xxxx',
                'Implementation-Version': version,
                'Main-Class': mainClassName
    }
}


dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.0"
    compile "org.jetbrains.kotlin:kotlin-reflect:1.1.0"
    ... ... 
}
like image 661
Tody.Lu Avatar asked Oct 18 '22 16:10

Tody.Lu


1 Answers

You can set the jdkHome property of the Kotlin compiler:

compileKotlin {
    kotlinOptions {
        jdkHome = '[path to 1.6 JDK]'
    }
}
like image 162
Ingo Kegel Avatar answered Oct 24 '22 12:10

Ingo Kegel