Defining Dependencies in Gradle Kotlin DSL

A concise syntax to define reusable dependencies

Yingxin Wu
AndroidPub

--

Gradle + Kotlin = Awesome

Motivation

If you have a multi-module Gradle project (e.g. most Android projects), you may want to define all the dependencies in one place, then make references in sub-projects’ build file, keeping them consistent across the whole project.

This approach is exemplified by Jake Wharton’s U+2020 app, in which dependencies are pre-defined as Extra Properties:

It’s straightforward for Groovy-flavored Gradle scripts. However, it’ll be a bit different when it comes to Gradle Kotlin DSL (i.e. Gradle scripts written in Kotlin).

The Problem

Kotlin compiler, which is statically typed, will complain about the following statement, which is the Kotlin equivalent of the dependency reference we wrote in the snippets above.

--

--

Yingxin Wu
AndroidPub

Software developer, architect, swimmer, scuba diver