Hi everyone! :wave: I've set up Detekt with type ...
# detekt
g
Hi everyone! 👋 I've set up Detekt with type resolution via CLI for our Android project, but I'm encountering some false positives. For example, in the snippet below, Detekt flags
productArgs.favoriteLink!!.id
as
UnnecessaryNotNullOperator
, even though
productArgs.favoriteLink
is in another module and cannot be smart casted (Android Studio confirms this).
if (productArgs.favoriteLink != null) {
println(productArgs.favoriteLink!!.id) // Reported as UnnecessaryNotNullOperator
println(productArgs.favoriteLink.id)  // Compiler error: smart cast not possible
}
Here’s how I resolve my classpath using a Gradle task:
tasks.register("resolveClasspath") {
def resolvedClasspath = providers.provider {
def classpathFiles = files()
// Iterate over application variants during the configuration phase
android.applicationVariants.all { variant ->
classpathFiles += variant.javaCompileProvider.get().classpath.filter { it.exists() }
}
// Convert classpath to a comma-separated string for later use
classpathFiles.files.collect { it.absolutePath }.join(",")
}
doLast {
println(resolvedClasspath.get())
}
}
Any ideas why Detekt might generate false positives in this setup? Am I missing something in my configuration? 🤔
b
Did you try detekt with gradle? Does that false positive still appears? Did you compare your classpath with the one that the Detekt plugin provides?
g
Thank you so much for great suggestions! I'll return with feedback a bit later
For now I can say, that I can't see any false-positives if I'm using
./gradlew detektMain
gradle plugin works well. My only concern - it takes so many time to run on our CI. I've placed
./gradlew detektMain
after app build on CI, so I believe some gradle caches is reusing, even so it takes about 15 minutes to run. Our app build process takes about 20 minutes,
detekt cli
without type resolution takes about a minute to run Is there any way to speed up execution of detekt with type resolution? unfortunately we can't use compiler plugin, cause We're using Kotlin 2.0.20 and we need it to be with K2 support. I tried to get a classpath manually and run
detekt cli
with type resolution, but I found it really hard to get a correct classpath from android project.
b
Those numbers are really bad. There must be an issue on the configuration or on detekt itself. Can you share a build scan? It looks like an issue somewhere. I run detekt on a big android project with type resolution and the time is way, way better.