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

by
Maya Patel
android deprecated gradle-kotlin-dsl groovy java-module

The Problem:

In an Android application, the use of kotlinOptions within a java-library and kotlin module is marked as deprecated by Android Studio Giraffe / Lint. However, there is no quick fix to apply, and the syntax for replacing it correctly is unclear. How can this be done effectively?

The Solutions:

Solution 1: Using compileTasks to update compilerOptions

To resolve the issue of the deprecated `kotlinOptions` in your `java-library` & `kotlin` module, you can leverage the `compileTasks` and `compilerOptions` to update the compiler options. Here’s an explanation:

1. compileTasks Configuration:

  • Identify all Kotlin compilation tasks using tasks.withType(KotlinCompile).
  • Configure each task individually using configureEach.

2. compilerOptions Update:

  • Inside the configureEach block, update the compilerOptions as per your requirements:
    • Set the jvmTarget to the desired Java version (e.g., JVM_11).
    • Add any necessary compiler arguments using freeCompilerArgs.

3. Updated Code:

tasks.withType(KotlinCompile).configureEach {
  compilerOptions {
    jvmTarget.set(JavaVersion.VERSION_11)
    freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn")
  }
}

This updated code replaces the deprecated kotlinOptions block and applies the changes to all Kotlin compilation tasks.

4. References:

By following this solution, you can successfully address the kotlinOptions deprecation issue in your java-library & kotlin module.

Q&A

How can I replace kotlinOptions properly?

KotlinOptions is deprecated, use compilerOptions input that uses a Property from Gradle Properties API as the return type.

How is compileOptions different from kotlinOptions?

compilerOptions uses a Property from the Gradle Properties API as the return type.

What is the updated code for kotlinOptions?

Updated code using compilerOptions set to JvmTarget and freeCompilerArgs.