3

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?

JJD
  • 50,076
  • 60
  • 203
  • 339

1 Answers1

5

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
    }
}
Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • 2
    This seems to work: `freeCompilerArgs = ["-Xjavac-arguments=['-Xlint:unchecked', '-Xlint:deprecation']"]` – JJD Jul 26 '19 at 10:13