Managing Android Multi-module Project with Gradle Plugin and Kotlin

Photo by Jan Kolar on Unsplash
build.gradle.kts file for an Android module with custom Gradle plugin applied.
Typical build.gradle file for an Android module.

Gradle Plugin with Kotlin

  • Applies important and required plugins, such as kotlin-android and kotlin-android-extensions.
  • Configures common android module settings for a module (minSdkVersion, targetSdkVersion, etc.) but still allows each module to override any settings.
  • Specifies the default proguard files.
  • Enables supported Java 8 features across all modules.
  • Adds required dependencies to a module, such as kotlin-stdlib
  • Configures Jacoco for each module and allow custom configuration block that supports both kotlin and groovy script.
buildSrc/build.gradle.kts file.
Extending the Plugin interface.

Apply Other Plugins

Applying other plugins through custom Gradle plugin.

Configure Common Android Settings

Setting common Android build properties.

Default Proguard Files

Declaring proguard files to use based on the module type.

Enable Supported Java 8 Features

Enabling Supported Java 8 Features.

Adding Required Dependencies

Adding external Gradle artefact dependencies through dependencies block.

Jacoco and Custom Settings Through Extension

  1. Whether to generate the coverage report for this module.
  2. File pattern for classes to be excluded from each module’s coverage report.
android { ... }myOptions {
jacoco {
isEnabled = true
excludes(
"file/pattern/to**Exclude**"
)
}
}
Extension class that declares the configurable Jacoco parameters.
Declaring extension for customizability.
Reading extension class’ properties.

Final Steps

Registering a Gradle plugin.

Other Benefits of Using Gradle Plugin

Extension function to declare dependencies on external Gradle artefacts.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store