JJD
JJD

Reputation: 51874

How to output deprecation warnings for Kotlin code?

I am using the following configuration snippet in my Java/Kotlin Android project in the app/build.gradle file:

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
    }
}

It generates a verbose output of Lint warnings in .java files when the project is compiled.
I would like to achieve the same for .kt files. I found out that Kotlin has compiler options:

gradle.projectsEvaluated {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            freeCompilerArgs = ["-Xlint:unchecked", "-Xlint:deprecation"]
        }
    }
}

However the compiler flags are not supported:

w: Flag is not supported by this version of the compiler: -Xlint:unchecked
w: Flag is not supported by this version of the compiler: -Xlint:deprecation

How can I output deprecation warnings for Kotlin code?

Upvotes: 3

Views: 1883

Answers (1)

Paul Hicks
Paul Hicks

Reputation: 14009

The java compiler and kotlin compiler have completely different options. The -Xlint option does not exist for kotlinc. You can run kotlinc -X to show all the -X options.

The -Xjavac-arguments argument allows you to pass javac arguments through kotlinc. For example:

kotlinc -Xjavac-arguments='-Xlint:unchecked -Xlint:deprecation' ...

In your gradle file, you can build an array of one argument:

gradle.projectsEvaluated {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            freeCompilerArgs = [
                "-Xjavac-arguments='-Xlint:unchecked -Xlint:deprecation'"
            ]
        }
    }
}

Other syntaxes may also work.

Aside: do the default warnings not include these? You could check by adding this snippet to ensure you're not suppressing warnings:

compileKotlin {
    kotlinOptions {
        suppressWarnings = false
    }
}

Upvotes: 5

Related Questions