Gradle Kotlin DSL
Gradle’s Kotlin DSL(domain-specific languages) provides an alternative syntax to the traditional Groovy DSL with an enhanced editing experience in supported IDEs, with superior content assist, refactoring, documentation, and more.
We can make a custom file to maintain configuration params and dependency version for the whole project in a single configuration file.
Step 1: Create a folder structure under the buildSrc
folder:
Step 2: Add file build.gradle.kts
1 2 3 4 5 6 7 8 9 |
import org.gradle.kotlin.dsl.`kotlin-dsl` plugins { `kotlin-dsl` } repositories { jcenter() } |
Step 3: NowAdd build configuration file Dependencies.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
object ApplicationId { val id = "com.demo.android" } object Modules { val app = ":app" val navigation = ":navigation" val network = ":network" val sample = ":sample" } object Releases { val versionCode = 100 val versionName = "1.01" } object Versions { val gradle = "3.4.2" val compileSdk = 28 val minSdk = 21 val targetSdk = 28 val appcompat = "1.0.2" val design = "1.0.0" val cardview = "1.0.0" val recyclerview = "1.0.0" val ktx = "1.0.0-alpha1" val kotlin = "1.3.41" } object Libraries { val kotlin = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.kotlin}" val ktx = "androidx.core:core-ktx:${Versions.ktx}" val appcompat = "androidx.appcompat:appcompat:${Versions.appcompat}" val design = "com.google.android.material:material:${Versions.design}" val cardview = "androidx.cardview:cardview:${Versions.cardview}" val recyclerview = "androidx.recyclerview:recyclerview:${Versions.recyclerview}" } |
Now you have completed DSL set up and you can use Dependencies.kt file configuration in your project as
1 2 3 4 5 6 7 8 9 10 |
dependencies { implementation project(Modules.app) implementation project(Modules.navigation) implementation Libraries.design implementation Libraries.cardview implementation Libraries.appcompat implementation Libraries.recyclerview implementation Libraries.koinAndroid } |