Thuy's Blog

Installing detekt into a multi-module Android project

June 11, 2019

If you have already read long enough both the README file and the docs from detekt but you are still unsure about how to set up detekt for your Android project with multiple modules, then this blog-post is for you. Note that what I’m going to show you here may only be true for detekt 1.0.0-RC15.

Let’s assume that your Android project has a similar structure as my flickr-android project. This project has only 3 modules:

  • domain: A pure Kotlin module.
  • data: An Android library module.
  • app: An Android application module.

In the build file build.gradle at the root directory of the project, add following configuration. (Or refer to this commit for a better insight.)

plugins {
  id "io.gitlab.arturbosch.detekt" version "1.0.0-RC15"
}

subprojects {
  apply plugin: "io.gitlab.arturbosch.detekt"

  detekt {
    toolVersion = "1.0.0-RC15"
  }
}

And that’s it. That will add a new detekt task into each module. In order to run detekt on the entire project, just run a single command ./gradlew detekt. If we want to verify exactly whether that command will execute all detekt tasks from all modules, we can try this Gradle plugin namely Task Tree to print a dependency tree of the detekt task.

First, declare a new id beside detekt’s plugin id:

plugins {
  id "io.gitlab.arturbosch.detekt" version "1.0.0-RC15"
  id "com.dorongold.task-tree" version "1.4"
}

Then run ./gradlew taskTree detekt. In my case, part of the output will show:

:detekt
+--- :app:detekt
+--- :data:detekt
\--- :domain:detekt

So, that tree means, when I execute the detekt task of the root project, it will execute :app:detekt, :data:detekt, and :domain:detekt. However, if we want to omit a particular module (let’s say, data) from detekt, this can do the trick:

subprojects {
  if (project.name != "data") {
    apply plugin: "io.gitlab.arturbosch.detekt"

    detekt {
      toolVersion = "1.0.0-RC15"
    }
  }
}

If I run ./gradlew taskTree detekt again, the output will be:

:detekt
+--- :app:detekt
\--- :domain:detekt

Hope this will be helpful.


Thuy Trinh

Written by Thuy Trinh who lives and works in Frankfurt, Germany building robust Android apps. You should follow him on Twitter