DSL์ด๋ Domain Specific Language์ ์ฝ์ด๋ก ํน์ ๋ถ์ผ์ ์ต์ ํ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ฅผ ๋ปํ๋ค.
์์ฉ๊ตฌ ์ฝ๋๋ฅผ ์ต์ํ ํ๊ธฐ ์ํด ๋ช
๋ นํ ์ฝ๋ ๋์ ์ ์ธ์ ์ฝ๋ ํ์์ ๋ฐ๋ฅธ๋ค.
Kotlin DSL์ ์ฝํ๋ฆฐ์ ์ธ์ด์ ์ธ ํน์ง์ผ๋ก ๊ฐ๋
์ฑ์ด ์ข๊ณ ๊ฐ๋ตํ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ Gradle ์คํฌ๋ฆฝํ
์ ํ๋ ๊ฒ์ ๋ชฉ์ ์ผ๋ก ํ๋ DSL์ด๋ค.
- IDE ์ง์ ํฅ์๋ ํธ์งํ๊ฒฝ
- Code highlighting
- ์๋์์ฑ ์ง์
- ์ฝ๋ ํ์
- ์ค๋ฅ ์ฝ๋ ๊ฐ์กฐ
- ๋ณ์ ๋ฆฌํํ ๋ง ๊ฐ๋ฅ
- ์ต์ํ Kotlin์ธ์ด ์ฌ์ฉ
- ๋ฌ๋ ์ปค๋ธ ๋ฎ์
- ๋ฉํฐ ๋ชจ๋์ฌ์ฉ์ ์ค๋ณต ์์กด์ฑ ์ ์ธ ํ์์์ด์ง
- ๋น๋ ์บ์๊ฐ Invalidation ๋๊ฑฐ๋ ํด๋ฆฐ ๋น๋์์ Groovy DSL๋ณด๋ค ๋๋ฆผ
- ์๋ก์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฒ์ Inspection ๊ธฐ๋ฅ ๋ฏธ์ง์
- Java8์ด์์์ ๋์
๊ธฐ์กด *.gradle
ํ์ผ์ *.gradle.kts
๋ก ๋ณ๊ฒฝํ๋ค.
ex) build.gradle -> build.gradle.kts
์๋ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ์ฌ ktsํ์ผ๋ก ๋ณํํ์
์์กด์ฑ ๊ด๋ฆฌ์ IDE ์๋์์ฑ ์ง์์ ์ํด kotlin ์ฝ๋๋ฅผ ๊ฐ์ง๋ buildSrc ๋ชจ๋์ ๋ง๋ค ์ ์๋ค.
Gradle ๋ฌธ์๋ฅผ ๋ณด๋ฉด
Gradle์ด ์ํ๋๋ฉด buildSrc ๋๋ ํ ๋ฆฌ๊ฐ ์กด์ฌํ๋์ง ์ฒดํฌํ๋ค.
์ด ๊ฒฝ์ฐ Gradle์ ์๋์ ์ผ๋ก ์ฝ๋๋ฅผ ์ปดํ์ผํ๊ณ ํ ์คํธํ ๋ค ๋น์ ์ ๋น๋ ์คํฌ๋ฆฝํธ์ classpath์ ๋ฃ๋๋ค. ์ด ๋ฐฉ๋ฒ์ ์ ์ง ๋ณด์, ๋ฆฌํฉํ ๋ง ๋ฐ ์ฝ๋ ํ ์คํธ๊ฐ ๋ ์ฝ๋ค.
์ฆ, buildSrc๋ ๋น๋ ๋ก์ง์ ํฌํจํ ์ ์๋ Gradle ํ๋ก์ ํธ ๋ฃจํธ ๋๋ ํ ๋ฆฌ๋ก
buildSrc๊ณผ Kotlin DSL์ ์ฌ์ฉํด์ ๋งค์ฐ ์ ์ ๊ตฌ์ฑ์ผ๋ก ์ปค์คํ
๋น๋ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ์ ์ฒด ํ๋ก์ ํธ์์ ์ด ๋ก์ง์ ๊ณต์ ํ ์ ์๋ค.
buildSrc๋ฅผ ๋ณ๊ฒฝํ๋ฉด ์ ์ฒด ํ๋ก์ ํธ์ ๋น๋์บ์๊ฐ ๋ฌดํจํ๋๊ธฐ ๋๋ฌธ์ ์ฆ์ ์์ ์ ํผํด์ผ ํ๋ค.
- rootDir์ buildSrc Directory ์์ฑ
- buildSrc/build.gradle.kts ์์ฑ
- buildSrc/src/main/kotlin/Dependency.kt ์์ฑ
- build.gradle(:app) -> build.gradle.kts ๋ณ๊ฒฝ
- build.gradle(:project) -> build.gradle.kts ๋ณ๊ฒฝ
- setting.gradle -> setting.gradle.kts ๋ณ๊ฒฝ
Gradle์ ์คํํ๋ฉด buildSrc๋ผ๋ ๋๋ ํ ๋ฆฌ๊ฐ ์กด์ฌํ๋์ง ๊ฒ์ฌํ๋ค.
์กด์ฌํ๋ค๋ฉด ์๋์ผ๋ก buildSrc/build.gradle.kts
์ฝ๋๋ฅผ ์ปดํ์ผํ๊ณ ํ
์คํธํ ๋ค์ ๋น๋ ์คํฌ๋ฆฝํธ์ ํด๋์คํจ์ค์ ์ง์ด ๋ฃ๋๋ค.
buildSrc/build.gradle.kts
plugins {
`kotlin-dsl` // kotlin dsl ์ค์
}
repositories {
google()
mavenCentral()
}
object PluginVersion {
const val GRADLE = "4.2.2"
const val KOTLIN = "1.5.21"
}
dependencies {
implementation("com.android.tools.build:gradle:${PluginVersion.GRADLE}")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${PluginVersion.KOTLIN}")
}
buildSrc/src/main/kotlin
๊ฒฝ๋ก์ Dependency.kt
ํ์ผ์ ์์ฑํ๋ค.
object Dependency {
object Kotlin {
const val SDK = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.21"
}
object AndroidX {
const val MATERIAL = "androidx.compose.material:material:1.0.0-rc02"
const val CONSTRAINT_LAYOUT = "androidx.constraintlayout:constraintlayout:2.1.0-rc01"
const val APP_COMPAT = "androidx.appcompat:appcompat:1.3.1"
}
object KTX {
const val CORE = "androidx.core:core-ktx:1.7.0-alpha01"
}
object Test {
const val JUNIT = "junit:junit:4.13.2"
const val ANDROID_JUNIT_RUNNER = "AndroidJUnitRunner"
}
object AndroidTest {
const val TEST_RUNNER = "androidx.test:runner:1.4.0"
const val ESPRESSO_CORE = "androidx.test.espresso:espresso-core:3.4.0"
}
}
๊ธฐ์กด build.gradle(:app)
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "io.beomjo.kakao.search"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation `org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version`
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
๋ณ๊ฒฝ build.gradle.kts(:app)
plugins {
android
`kotlin-android`
}
android {
compileSdkVersion(30)
buildToolsVersion = "30.0.3"
defaultConfig {
applicationId = "io.beomjo.sample"
minSdkVersion(21)
targetSdkVersion(30)
vectorDrawables.useSupportLibrary = true
versionCode = 1
versionName = "1"
testInstrumentationRunner = Dependency.Test.ANDROID_JUNIT_RUNNER
}
buildTypes {
getByName("release") {
minifyEnabled(false)
proguardFiles(
getDefaultProguardFile(
"proguard-android-optimize.txt"
),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}
dependencies {
implementation(Dependency.Kotlin.SDK)
implementation(Dependency.KTX.CORE)
implementation(Dependency.AndroidX.APP_COMPAT)
implementation(Dependency.AndroidX.MATERIAL)
implementation(Dependency.AndroidX.CONSTRAINT_LAYOUT)
testImplementation(Dependency.Test.JUNIT)
androidTestImplementation(Dependency.AndroidTest.TEST_RUNNER)
androidTestImplementation(Dependency.AndroidTest.ESPRESSO_CORE)
}
๊ธฐ์กด build.gradle(:project)
buildscript {
ext.kotlin_version = "1.5.21"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
๋ณ๊ฒฝ build.gradle.kts(:project)
subprojects {
repositories {
google()
mavenCentral()
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
๊ธฐ์กด setting.gradle
rootProject.name = "My Project"
include ':app'
๋ณ๊ฒฝ setting.gradle.kts
rootProject.name = "My Project"
include(":app")