Gradle VersionCatalog 적용하기

taetae98·2023년 7월 4일

thumbnail

VersionCatalog

클린 아키텍쳐가 유행하면서 많은 프로젝트에서 멀티 모듈을 사용하고 있습니다. 멀티 모듈을 사용하면서 Gradle 코드가 필요성이 생기고, VersionCatalog를 사용하여 plugin, dependency 버전을 쉽게 관리할 수 있습니다.

Gradle 7.4부터 Stable로 변경됐습니다.
Android Giraffe부터 프로젝트 생성시 VersionCatalog 옵션을 제공합니다.

libs.versions.toml

VersionCatalog는 toml 언어를 사용합니다. 루트 프로젝트 gradle 폴더에 libs.versions.toml 파일을 생성하면 됩니다.

libs.versions.toml 구성

[versions]

compose-compiler = "1.4.8"      # https://developer.android.com/jetpack/androidx/releases/compose
compose-bom = "2023.06.01"      # https://developer.android.com/jetpack/compose/bom/bom-mapping

spotless = "6.19.0"             # https://github.com/diffplug/spotless/tree/main/plugin-gradle
ktlint = "0.49.1"               # https://github.com/pinterest/ktlint/releases

[libraries]

compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "compose-bom" }
compose-material3 = { group = "androidx.compose.material3", name = "material3" }
compose-material-icon = { group = "androidx.compose.material", name = "material-icons-extended" }

ktor-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
ktor-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktor" }
ktor-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" }
ktor-serialization-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktor" }

[plugins]
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }

[bundles]
compose = ["compose-material3", "compose-material-icon"]
ktor = ["ktor-core", "ktor-cio", "ktor-content-negotiation", "ktor-serialization-json"]

[versions], [libraries], [plugins]은 이름에서 알다시피 version, dependency, plugin을 관리합니다.
여기서 [bundles]는 [libraries]를 묶음으로 관리할 수 있습니다.

아래와 같은 방식으로 group, name 대신 module로 사용할 수 있습니다.
java-inject = { module = "javax.inject:javax.inject", version.ref = "java-inject" }

Gradle

// 플러그인 적용 방법
plugins {
    alias(libs.plugins.spotless)
}

android {
	composeOptions {
    	// 버전 직접 적용 방법
		kotlinCompilerExtensionVersion = libs.findVersion("compose-compiler").get().requiredVersion
	}
}

dependencies {
	// 라이브러리 적용 방법
    implementation(libs.java.inject)
    implementation(libs.paging.common)
    
	// 번들 적용 방법
    implementation(libs.bundles.ktor)
}

libs를 통해 불러올 수 있고 -으로 계층을 구분할 수 있습니다.

참고

profile
네이버 웹툰 안드로이드 개발자

0개의 댓글