Auto Added by WPeMatico

Kotlin DSL, BuildSrc, Product Flavors, Flavor Icon and more

For a long time, I wanted to migrate Gradle files to Kotlin DSL, but I was always procrastinating. I finally put an end to it and I also implemented buildSrc. It’s really important to check dependencies and versions. Especially in applications with more than one module, we can prevent different dependency management in each module with the buildSrc module.
Kotlin DSL 🤝 buildSrc

Let’s see, how we can do it?

What we will do in this article?

  1. refactor settings.gradle to settings.gradle.kts,
  2. create buildSrc package to project root directory,
  3. change .gradle files to .kts file,
  4. move versions and dependency strings to Kotlin files,
  5. extract keys from build.gradle and movekey.properties file,
  6. create product flavors and set configurations with Kotlin DSL.

The default project directory is like this:

1. Refactor settings.gradle to settings.gradle.kts

Rename settings.gradle file settings.gradle.kts and change a few code lines:

//before
rootProject.name = "KotlinDSL-BuildSrcExample"
include ':app'
// after
rootProject.name = "KotlinDSL-BuildSrcExample"
include(":app")

2. Create buildSrc package

Create “buildSrc” module to root project directory. And create build.gradle.kts file into it.

import org.gradle.kotlin.dsl.`kotlin-dsl`

plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
}

now rebuild project.

We have a buildSrc module. So, we can create Kotlin files in the module.

We will add Configs.kt,Versions.kt, Libraries.ktinto buildSrc/src//main/kotlin directory later.

3. refactor .gradle files to .kts file

We must change .gradle files to .gradle.kts. It’s the hardest part:

  • build.gradle(proje)

We use Kotlin in Gradle files. Let’s get the define Libraries.kt and Versions.kt Kotlin objects to buildSrc/src/main/kotlin

Hello Kotlin ✋

  • build.gradle(app module)

4. Move versions and dependency strings to Kotlin files

Now, we move out from defaultConfig to Configs.kt for app configs.

5. Extract keys from Gradle files (key.properties)

For more reliable codes, we must extract keys from Gradle and move another file like key.properties:

  • create key.properties file in the root project directory,
  • store keys (like signing keys) in this file,
  • create a helper class for getting the value of a key.
// build.gradle.kts (module :app)
signingConfigs {
create("signingConfigRelease") {
storeFile = file(KeyHelper.getValue(KeyHelper.KEY_STORE_FILE))
storePassword = KeyHelper.getValue(KeyHelper.KEY_STORE_PASS)
keyAlias = KeyHelper.getValue(KeyHelper.KEY_ALIAS)
keyPassword = KeyHelper.getValue(KeyHelper.KEY_PASS)
}
}
--------------------------------------------------------------------
// KeyHelper.kt
object KeyHelper {

const val KEY_STORE_FILE = "storeFile"
const val KEY_STORE_PASS = "storePassword"
const val KEY_ALIAS = "keyAlias"
const val KEY_PASS = "keyPassword"

private val properties by lazy {
Properties().apply { load(FileInputStream(File("key.properties"))) }
}

fun getValue(key: String): String {
return properties.getProperty(key)
}
}

6. Product Flavors and Configurations

Let’s create flavor together and set configurations with Kotlin DSL.

Create at least one flavorDimensions and your productFlavors. If you have environment specific value you can define in productFlavors:

flavorDimensions("appType")
productFlavors {
create("_dev") {
dimension = "appType"
applicationIdSuffix = ".dev"
versionNameSuffix = "-dev"

manifestPlaceholders(
mapOf(
"appIcon" to "@mipmap/ic_launcher_prod",
"appIconRound" to "@mipmap/ic_launcher_prod_round"
)
)
resValue("string", "app_name", "KotlinDslExample$versionNameSuffix")
buildConfigField("String", "ONESIGNAL_APP_ID", KeyHelper.KEY_ONESIGNAL_APP_ID_DEV)
}

In my case, besides the completely changed values, the change in base url was only in the extension part. And I handled it in the applicationVariants.all block.

And that’s all. I know it took a while, but I wanted to touch on the details. If you find any wrong info please leave a comment. I will fix it.

If this article helped you please press claps 👏

Resources

Project: https://github.com/mustafayigitt/KotlinDSL-BuildSrcExample

Linkedin: https://www.linkedin.com/in/mustafayigitt/

Website: https://www.mustafayigit.net/

Click 👏 to say “thanks!” and help others find this article.

To be up-to-date with great news on Kt. Academy, subscribe to the newsletter, observe Twitter and follow us on Medium.

If you need a Kotlin workshop, check how we can help you: kt.academy.


Kotlin DSL, BuildSrc, Product Flavors, Flavor Icon and more was originally published in Kt. Academy on Medium, where people are continuing the conversation by highlighting and responding to this story.

Continue ReadingKotlin DSL, BuildSrc, Product Flavors, Flavor Icon and more

End of content

No more pages to load