Gradle 에서 TOML 사
Gradle 에서 TOML 사용하자
Kotlin Multiplatform (https://yunhos.blogspot.com/2022/09/kotlin-multiplatform.html) 에서의 설명에서는 프로젝트 루트 디렉토리에 디렉토리를 만든 후, buildSrc
해당 디렉토리에 별도의 버젼관리용 파일들을 만들었다.
이런 방식도 좋지만, 버젼관리용 파일을 따로 만드는 번거로움등이 있어서 프로젝트 구성자체가 복잡했었다.
libs.versions.toml 사용
Gradle이 버전 8.x부터는 toml은 읽기쉽고 작성하기 쉬운 설정파일을 관리하도록 도와준다.
먼저, Gradle 프로젝트의 루트 디렉토리에서 다음 gradle
디렉토리에 toml 파일(libs.versions.toml)을 만든다 .
내용은 KMP 샘플프로젝트 에서 참조하자면 아래와 같다.
[versions]
agp = "8.5.2"
android-compileSdk = "34"
android-minSdk = "24"
android-targetSdk = "34"
androidx-activityCompose = "1.9.3"
androidx-appcompat = "1.7.0"
androidx-constraintlayout = "2.2.0"
androidx-core-ktx = "1.15.0"
androidx-espresso-core = "3.6.1"
androidx-lifecycle = "2.8.4"
androidx-material = "1.12.0"
androidx-test-junit = "1.2.1"
compose-multiplatform = "1.7.0"
junit = "4.13.2"
kotlin = "2.1.0"
kotlinx-coroutines = "1.9.0"
ktor = "3.0.2"
logback = "1.5.12"
[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core-ktx" }
androidx-test-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-junit" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidx-espresso-core" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
androidx-material = { group = "com.google.android.material", name = "material", version.ref = "androidx-material" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "androidx-constraintlayout" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
androidx-lifecycle-viewmodel = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-viewmodel", version.ref = "androidx-lifecycle" }
androidx-lifecycle-runtime-compose = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "androidx-lifecycle" }
kotlinx-coroutines-swing = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" }
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
ktor-server-core = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty-jvm", version.ref = "ktor" }
ktor-server-tests = { module = "io.ktor:ktor-server-tests-jvm", version.ref = "ktor" }
[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
composeMultiplatform = { id = "org.jetbrains.compose", version.ref = "compose-multiplatform" }
composeCompiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
ktor = { id = "io.ktor.plugin", version.ref = "ktor" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
[versions] : 버젼값을 정의한다.
[libraries] : 종속성이 정의된다. 속성값으로 group, name,version 등을 정의해서 사용한다. version.ref 는 위의 versions에서 정의한 값을 참조할 수있다.
[bundles] : 해당 콘텐츠를 하나로 묶어서 한 번에 참조할 수 있도록 정의된 하나 이상의 종속성을 함께 패키징한다,
[plugins] : 플러그인을 정의한다. id, version을 사용한다.
build.gradle.kts 에서 사용하기
gradle에서는 자동으로 lib.version.toml 파일이 빌드되어서 lib 변수로 build.gradle.kts 에서 사용할수 있다.
( libs.plugins.kotlinMultiplatform 처럼 libs라는 변수가 보인다)
단, toml 파일에 androidx-lifecycle-viewmodel 라고 정의했다면 사용할때는 libs.androidx.lifecycle.viewmodel 가 된다.
다행히, Android Studio 에서는 ctrl+클릭으로 해당 변수로 이동해서 볼수있따.