Replacing the Deprecated kotlinOptions in a Java-Library & Kotlin Module

In Kotlin, the kotlinOptions configuration option in your Gradle build file is now deprecated. This article will guide you through the process of migrating your build configurations for both Java libraries and Kotlin modules.

Understanding the Deprecation

Previously, you would configure Kotlin-specific settings like language features and compiler options using kotlinOptions within your build.gradle file.

kotlinOptions {
    jvmTarget = "1.8" // Specify Java version compatibility
    freeCompilerArgs = ["-Xjsr305=strict"] // Enable annotation processing
}

However, Kotlin Gradle plugin versions 1.8.0 and higher introduced a more modular approach, separating these settings into dedicated blocks based on their purpose.

Migration Steps for Java Libraries

For Java libraries, you primarily need to migrate your Kotlin compilation settings from kotlinOptions to the kotlin block.

  1. Migrate compiler options: Move compiler-related options, such as target JVM version, into the kotlin block.
  2. kotlin {
        jvmTarget = "1.8" // Target Java version
        // Other compiler options
    }
    
  3. Migrate annotation processing options: Move options related to annotation processing, such as enabling or disabling certain annotations, to the compilerOptions block.
  4. compilerOptions {
        // Use "strict" mode for JSR305 annotations 
        // instead of freeCompilerArgs.
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    
  5. Migrate additional settings: Depending on your project’s specific requirements, you may have additional options within kotlinOptions. These should be migrated to the appropriate block within the kotlin section based on their purpose.

Migration Steps for Kotlin Modules

For Kotlin modules, the migration process is similar to Java libraries, with some additional considerations.

  1. Migrate compiler options: Similar to Java libraries, move compiler-related options from kotlinOptions to the kotlin block.
  2. Migrate annotation processing options: Migrate annotation processing options to the compilerOptions block as in Java libraries.
  3. Migrate DSL options: Migrate any Kotlin-specific DSL options, such as language features, to the kotlin block, typically within the sourceSets configuration.
  4. kotlin {
        sourceSets {
            main {
                kotlin.srcDirs("src/main/kotlin") // Specify Kotlin source directories
                // ... Other source set configurations
            }
        }
        // ... Other settings 
    }
    

Comparison Table:

Setting Old (Deprecated) New (Recommended)
Target JVM version kotlinOptions.jvmTarget kotlin.jvmTarget
Free compiler arguments kotlinOptions.freeCompilerArgs compilerOptions (e.g., freeCompilerArgs)
Annotation processing kotlinOptions.freeCompilerArgs (for -Xjsr305) compilerOptions.annotationProcessorPaths
Kotlin DSL options kotlinOptions.dsl. ... kotlin { sourceSets { main { kotlin.srcDirs(...) } } }

Benefits of Migration

Migrating away from kotlinOptions offers several benefits:

  • Improved clarity and organization: Separating settings into dedicated blocks based on their purpose makes your build configuration more readable and maintainable.
  • Simplified integration: The new approach aligns more closely with standard Gradle configuration conventions, simplifying integration with other Gradle plugins.
  • Consistency: Migrating to the new configuration scheme ensures consistency across your projects, regardless of the language used for specific modules.

Conclusion

By migrating your build configurations to the new recommended approach, you can leverage the improved modularity, clarity, and consistency offered by the Kotlin Gradle plugin.

Leave a Reply

Your email address will not be published. Required fields are marked *