Search code examples
androidkotlingroovydeprecatedjava-module

How to replace the deprecated kotlinOptions in a java-library & kotlin module?


Within my Android application project I am using the following Gradle (Groovy) configuration for a library module:

// build.gradle

apply plugin: "java-library"
apply plugin: "kotlin"

dependencies {
    api Libs.engelsystem
    implementation(Libs.retrofit) {
        exclude group: "com.squareup.okio", module: "okio"
    }

    testImplementation Libs.junit
    testImplementation Libs.kotlinCoroutinesTest
    testImplementation Libs.mockitoKotlin
    testImplementation Libs.okhttpMockWebServer
    testImplementation Libs.retrofitConverterMoshi
    testImplementation Libs.truth
}

sourceCompatibility = Config.compatibleJavaVersion
targetCompatibility = Config.compatibleJavaVersion

compileKotlin {
    kotlinOptions {
        jvmTarget = Config.compatibleJavaVersion // is set to JavaVersion.VERSION_11
        freeCompilerArgs += [
                "-opt-in=kotlin.RequiresOptIn"
        ]
    }
}

Android Studio Giraffe / Lint notifies me that kotlinOptions is deprecated.

Screenshot of the quick tip

There is no quick fix to apply. How can I replace the notation correctly? It seems there is a different syntax for this kind of library module. There is no deprecation warning in a com.android.library module nor in a com.android.application module. Both use the kotlin-android plugin if that is an important difference.

Potential answer

... consider them as non-officially deprecated ...

Source: https://youtrack.jetbrains.com/issue/KT-27301/Expose-compiler-flags-via-Gradle-lazy-properties#focus=Comments-27-6565858.0-0

Related


Solution

  • Compile tasks have the new compilerOptions input, which is similar to the existing kotlinOptions but uses Property from the Gradle Properties API as the return type.

    Here is your updated code in Groovy:

    import org.jetbrains.kotlin.gradle.dsl.JvmTarget
    import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
    
    
    tasks.withType(KotlinJvmCompile).configureEach {
      compilerOptions {
        jvmTarget.set(JvmTarget.JVM_11)
        freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn")
      }
    }
    

    Here is your updated code in Kotlin:

    import org.jetbrains.kotlin.gradle.dsl.JvmTarget
    import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
    
    tasks.withType<KotlinJvmCompile>().configureEach {
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_11)
            freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn")
        }
    }
    

    References: