I recently did something interesting on a multi-module kotlin project I'm working on, and I think I should share it with you guys. I ported all my gradle scripts that were written in groovy to Kotlin DSL. The reward was worth it. It reduced my gradle codes by 50 percent. I did a lot of things to achieve that, and one of them is creating a separate package to manage all my dependencies. And that is what this article is all about "Managing your dependencies in one place".
Let me re-emphasize that this technique should only be used in a multi-module project. I don't think this is necessary when building a monolithic app since all the third-party dependencies are probably been managed in one app-level gradle file. However, let me know if you disagree with me on this by making use of the comment section. Moving on...
ii. Code autocomplete: Android studio automatically suggests the dependencies you have already declared in your buildSrc package.
2. Inside the new directory, create a new file and call it build.gradle.kts.
3. Copy/paste the following code snippet., and then rebuild your project.
4. In the buildSrc package, create src -> main -> java. Just like the normal structure of your main project .
5. In the java package you just created, create a new kotlin file by left-clicking on the package name and selecting Kotlin File/Class. You can name the file whatever you want, but we'll be naming it Dependencies simply because we are going to be storing our dependencies inside it. And we want something that best describes that.
6. Declare two objects namely Versions and Deps. The versions object is going to hold your dependency versions while Deps will hold the fully qualify dependencies.
7. Now that we're done setting up the buildSrc, you can start migrating your current dependencies to the kotlin managed properties.
8. Once you're done, sync your project to make Android Studio aware of the new changes.
Did I miss out on something, or you have a better approach on how to achieve this, please let me know via the comment section.
Let me re-emphasize that this technique should only be used in a multi-module project. I don't think this is necessary when building a monolithic app since all the third-party dependencies are probably been managed in one app-level gradle file. However, let me know if you disagree with me on this by making use of the comment section. Moving on...
Advantages of Dependency management using Kotlin
i. Code reusability: Once you declare your dependencies, you can reuse them in any module in your project, hence reducing code duplicates.ii. Code autocomplete: Android studio automatically suggests the dependencies you have already declared in your buildSrc package.
How Does Dependency Management Works
When you run Gradle, it checks for a package name buildSrc. Then compile your codes, and add it to the classpath of your build script. This gives you access to the buildSrc properties which you can use in your build script.Setting up dependency management
1. Create a new directory and name it buildSrc.2. Inside the new directory, create a new file and call it build.gradle.kts.
3. Copy/paste the following code snippet., and then rebuild your project.
plugins{
`kotlin-dsl`
}
repositories{
jcenter()
}
4. In the buildSrc package, create src -> main -> java. Just like the normal structure of your main project .
5. In the java package you just created, create a new kotlin file by left-clicking on the package name and selecting Kotlin File/Class. You can name the file whatever you want, but we'll be naming it Dependencies simply because we are going to be storing our dependencies inside it. And we want something that best describes that.
6. Declare two objects namely Versions and Deps. The versions object is going to hold your dependency versions while Deps will hold the fully qualify dependencies.
object Versions{
const val retrofit ="2.8.1"
const val retrofit_converter ="2.1.0"
const val dagger ="2.27"
const val kotlin_core ="1.2.0"
}
const val retrofit_gson_converter="com.squareup.retrofit2:converter-gson:${Versions.retrofit_converter}"
const val retrofit_lib ="com.squareup.retrofit2:retrofit:${Versions.retrofit}"
const val dagger ="com.google.dagger:dagger:${Versions.dagger}"
const val kotlin_core_ktx ="androidx.core:core-ktx:${Versions.kotlin_core}" }
7. Now that we're done setting up the buildSrc, you can start migrating your current dependencies to the kotlin managed properties.
8. Once you're done, sync your project to make Android Studio aware of the new changes.
Did I miss out on something, or you have a better approach on how to achieve this, please let me know via the comment section.
Sign up here with your email
ConversionConversion EmoticonEmoticon