Skip to main content
Version: Next

Detekt

Rules

NameDescription
AndroidXLifecycleScopeLooks for accidental usage of the androidx lifecyclescope extension.
HardCodedDispatcherDetects use of a hard-coded reference to the kotlinx.coroutines.Dispatchers singleton.

Setup

If you don't already have Detekt set up in your project, follow the official quick start guide.

After that is working, you need to add dependencies for the Detekt CLI and these extension rules to each module which will be analyzed. The easiest way to do this is to apply them from the root project-level gradle file.

Adding new dependencies

In root project-level build.gradle or build.gradle.kts:

allprojects {
dependencies {
detekt("io.gitlab.arturbosch.detekt:detekt-cli:1.14.2")

detektPlugins("com.rickbusarow.dispatch:dispatch-detekt:1.0.0-beta10-SNAPSHOT")
}
}

Configuration

After adding the dependencies, you'll want to add parameters to your detekt config .yml file.

If you don't already have a config file, you can create one by invoking:

./gradlew detektGenerateConfig

Then, add the following to the bottom of the detekt-config.yml file:

dispatch:
active: true # disables all dispatch checks
AndroidXLifecycleScope: # incorrect lifecycleScope
active: true
HardCodedDispatcher: # finds usage of Dispatchers.______
active: true
allowDefaultDispatcher: false # if true, Detekt will ignore all usage of Dispatchers.Default
allowIODispatcher: false # if true, Detekt will ignore all usage of Dispatchers.IO
allowMainDispatcher: false # if true, Detekt will ignore all usage of Dispatchers.Main
allowMainImmediateDispatcher: false # if true, Detekt will ignore all usage of Dispatchers.Main.immediate
allowUnconfinedDispatcher: false # if true, Detekt will ignore all usage of Dispatchers.Unconfined

Gradle Daemon bug

There is an issue with ClassLoader caching which may cause issues the first time running Detekt. The workaround is to execute ./gradlew --stop once via command line. You should only ever need to do this one time, if at all. The fix for this has already been merged into Detekt.