How to use Kotlin in Gradle? How to use Kotlin in Gradle? Step by step Kotlin-DSL
migration guide.
Create buildSrc
directory in root of your project.
Create src/main/java
directory in buildSrc
Create your build.gradle.kts
file which contains Kotlin-DSL plugin.
import org.gradle.kotlin.dsl.`kotlin-dsl`
plugins {
`kotlin-dsl`
}
repositories {
jcenter()
}
Create your dependency and config files in buildSrc/src/main/java
ClassPaths:
object ClassPaths {
val gradleClasspath = "com.android.tools.build:gradle:${Versions.gradleVersion}"
val kotlinGradlePluginClasspath = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlinVersion}"
}
Config:
object Config {
val applicatiınId = "com.raqun.phonebox"
val testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
}
Dependencies.kt:
object CoreLibraries {
val kotlin = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.kotlinVersion}"
}
object SupportLibraries {
val appCompat = "com.android.support:appcompat-v7:${Versions.appCompatVersion}"
}
object TestLibraries {
val jUnit = "junit:junit:${Versions.jUnitVersion}"
val runnner = "com.android.support.test:runner:${Versions.testRunnerVersion}"
val espressoCore = "com.android.support.test.espresso:espresso-core:${Versions.espressoCoreVersion}"
}
Modules:
object Modules {
val app = ":app"
}
Plugins.kt:
object Plugins {
val androidApplication = "com.android.application"
val kotlinAndroid = "kotlin-android"
val kotlinAndroidExtensions = "kotlin-android-extensions"
}
Versions.kt:
object Release {
val versionCode = 1
val versionName = "1.0.0"
}
object Versions {
/**
* Sdk Versions
*/
val compileSdkVersion = 28
val minSdkVersion = 17
val targetSdkVersion = 28
/**
* Dependency Versions
*/
const val gradleVersion = "3.2.1"
const val kotlinVersion = "1.3.11"
const val appCompatVersion = "28.0.0"
const val jUnitVersion = "4.12"
const val testRunnerVersion = "1.0.2"
const val espressoCoreVersion = "3.0.2"
}
Rename your settings.gradle
to settings.gradle.kts
Change include ':app'
to include(Modules.app)
Rename your project level build.gradle
to build.gradle.kts
Refactor your code like the following:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath (ClassPaths.gradleClasspath)
classpath (ClassPaths.kotlinGradlePluginClasspath)
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task<Delete>("clean") {
delete(rootProject.buildDir)
}
Rename your module level build.gradle
to build.gradle.kts
Refactor your code like the following:
plugins {
id(Plugins.androidApplication)
id(Plugins.kotlinAndroid)
id(Plugins.kotlinAndroidExtensions)
}
android {
compileSdkVersion(Versions.compileSdkVersion)
defaultConfig {
applicationId = Config.applicatiınId
minSdkVersion(Versions.minSdkVersion)
targetSdkVersion(Versions.targetSdkVersion)
versionCode = Release.versionCode
versionName = Release.versionName
testInstrumentationRunner = Config.testInstrumentationRunner
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
}
dependencies {
// Core Libraries
implementation(CoreLibraries.kotlin)
// Support Libraries
implementation(SupportLibraries.appCompat)
// Testing
testImplementation(TestLibraries.jUnit)
androidTestImplementation(TestLibraries.runnner)
androidTestImplementation(TestLibraries.espressoCore)
}