Tony Robalik

Cut build time by 70% in your KMP projects with the Dependency Analysis Gradle Plugin

Bullet train

Photo by 7 on Unsplash

Kraken Technologies, which maintains a Kotlin Multiplatform (KMP) project of roughly 600 modules, was recently able to cut its average build time from about 6 minutes to about 1 minute and 50 seconds, or ~70%. They also report that sync times improved from roughly 5 minutes to roughly 45 seconds, or ~85%. These improvements are massive, and all the more impressive considering that the Dependency Analysis Gradle Plugin (DAGP) currently (as of v3.16.1) only supports JVM and Android targets in KMP projects.

YMMV. These results are for a particular project at a particular point in time.

The Dependency Analysis Gradle Plugin

In short, it is a static analysis tool that analyzes your Gradle project and produces a report on unused and misclassified dependencies; it also has the ability to automatically fix many of the issues it discovers. You can find it on Github.

I've been working on this plugin since 2019. It is, to my knowledge, unique. With support for analyzing Android and JVM projects from the beginning, its recent addition of KMP support truly cements its position as an essential tool in the Gradle ecosystem.

Repo shape

About 80% of Kraken's 600 modules are KMP modules with iOS and JVM targets. Another 2% are KMP with Android library targets. About 13% are Android-only. Finally, about 4% are JVM-only (Java/Kotlin).

The missing support for iOS target analysis wasn't a blocker for the first pass, which focused on finding unused dependencies.

Try it today

Detailed usage instructions are available in the README, but in brief, follow these steps:

  1. Method A
    1. Required. Apply the com.autonomousapps.dependency-analysis plugin to the root build script, with version.
    2. Optional. Apply the com.autonomousapps.dependency-analysis plugin to each module in which you want analysis. In general, this should be every module, but there may be cases where you don't want to exclude a module, and the simplest way to achieve this is to not apply the plugin in that case.
  2. Method B
    1. Required. Apply the com.autonomousapps.build-health plugin to the settings script.
  3. Run ./gradlew :buildHealth.

Apply the plugin (method A)

root build.gradle(.kts)

plugins {
  id("com.autonomousapps.dependency-analysis") version "3.16.1"

  // optional: if you use Android, Kotlin, or KMP, these plugins
  // must be loaded in the same classloader
  id("com.android.application") apply false
  id("com.android.kotlin.multiplatform.library") apply false
  id("org.jetbrains.kotlin.jvm") apply false
  id("org.jetbrains.kotlin.multiplatform") apply false
}

subprojects/build.gradle(.kts)

plugins {
  id("com.autonomousapps.dependency-analysis")
}

Apply the plugin (method B)

Alternatively, if you know what you want global analysis, replace the two blocks above with this:

settings.gradle(.kts)

plugins {
  id("com.autonomousapps.build-health") version "3.16.1"

  // optional: if you use Android, Kotlin, or KMP, these plugins
  // must be loaded in the same classloader
  id("com.android.application") apply false
  id("com.android.kotlin.multiplatform.library") apply false
  id("org.jetbrains.kotlin.jvm") apply false
  id("org.jetbrains.kotlin.multiplatform") apply false
}

The com.autonomousapps.build-health plugin automatically applies the com.autonomousapps.dependency-analysis plugin to all subprojects, in an Isolated Projects-safe way.

Run the analysis

DAGP has many useful tasks. The most important are presented below.

Run the analysis

./gradlew :buildHealth

Auto-fix all dependency issues

./gradlew fixDependencies

Note that this doesn't work correctly for KMP projects—yet.

See what other tasks are available

./gradlew :<module>:tasks --group dependency-analysis

The plugin has many other useful facilities. Check them out!