Skip to content

Latest commit

ย 

History

History
286 lines (232 loc) ยท 7.96 KB

kotlin_dsl_build_src.md

File metadata and controls

286 lines (232 loc) ยท 7.96 KB

Kotlin DSL + buildSrc ์œผ๋กœ ์˜์กด์„ฑ ๊ด€๋ฆฌ

Kotlin DSL์ด๋ž€?

DSL์ด๋ž€ Domain Specific Language์˜ ์•ฝ์–ด๋กœ ํŠน์ • ๋ถ„์•ผ์— ์ตœ์ ํ™”๋œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ๋œปํ•œ๋‹ค.
์ƒ์šฉ๊ตฌ ์ฝ”๋“œ๋ฅผ ์ตœ์†Œํ™” ํ•˜๊ธฐ ์œ„ํ•ด ๋ช…๋ นํ˜• ์ฝ”๋“œ ๋Œ€์‹  ์„ ์–ธ์  ์ฝ”๋“œ ํ˜•์‹์„ ๋”ฐ๋ฅธ๋‹ค.
Kotlin DSL์€ ์ฝ”ํ‹€๋ฆฐ์˜ ์–ธ์–ด์ ์ธ ํŠน์ง•์œผ๋กœ ๊ฐ€๋…์„ฑ์ด ์ข‹๊ณ  ๊ฐ„๋žตํ•œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Gradle ์Šคํฌ๋ฆฝํŒ…์„ ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•˜๋Š” DSL์ด๋‹ค.

Kotlin DSL ์žฅ, ๋‹จ์ 

์žฅ์ 

  • IDE ์ง€์› ํ–ฅ์ƒ๋œ ํŽธ์ง‘ํ™˜๊ฒฝ
    • Code highlighting
    • ์ž๋™์™„์„ฑ ์ง€์›
    • ์ฝ”๋“œ ํƒ์ƒ‰
    • ์˜ค๋ฅ˜ ์ฝ”๋“œ ๊ฐ•์กฐ
    • ๋ณ€์ˆ˜ ๋ฆฌํŽ™ํ† ๋ง ๊ฐ€๋Šฅ
  • ์ต์ˆ™ํ•œ Kotlin์–ธ์–ด ์‚ฌ์šฉ
    • ๋Ÿฌ๋‹ ์ปค๋ธŒ ๋‚ฎ์Œ
  • ๋ฉ€ํ‹ฐ ๋ชจ๋“ˆ์‚ฌ์šฉ์‹œ ์ค‘๋ณต ์˜์กด์„ฑ ์„ ์–ธ ํ•„์š”์—†์–ด์ง

๋‹จ์ 

  • ๋นŒ๋“œ ์บ์‹œ๊ฐ€ Invalidation ๋˜๊ฑฐ๋‚˜ ํด๋ฆฐ ๋นŒ๋“œ์‹œ์— Groovy DSL๋ณด๋‹ค ๋Š๋ฆผ
  • ์ƒˆ๋กœ์šด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฒ„์ „ Inspection ๊ธฐ๋Šฅ ๋ฏธ์ง€์›
  • Java8์ด์ƒ์—์„œ ๋™์ž‘

Kotlin DSL ์‚ฌ์šฉ

๊ธฐ์กด *.gradle ํŒŒ์ผ์„ *.gradle.kts ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

ex) build.gradle -> build.gradle.kts

์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ktsํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•˜์ž

buildSrc

์˜์กด์„ฑ ๊ด€๋ฆฌ์™€ 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 ๋ณ€๊ฒฝ

rootDir์— buildSrc Directory ์ƒ์„ฑ

image

buildSrc/build.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 ์ƒ์„ฑ

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) -> build.gradle.kts ๋ณ€๊ฒฝ

๊ธฐ์กด 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) -> build.gradle.kts ๋ณ€๊ฒฝ

๊ธฐ์กด 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 -> setting.gradle.kts ๋ณ€๊ฒฝ

๊ธฐ์กด setting.gradle

rootProject.name = "My Project"
include ':app'

๋ณ€๊ฒฝ setting.gradle.kts

rootProject.name = "My Project"
include(":app")