관리 메뉴

나만을 위한 블로그

[Android] 버전 카탈로그란? 본문

Android

[Android] 버전 카탈로그란?

참깨빵위에참깨빵 2024. 7. 6. 19:58
728x90
반응형

안드로이드 스튜디오 코알라에서 프로젝트를 기본 설정으로 생성하면 libs.versions.toml이란 파일이 생기는 걸 볼 수 있다.

이 파일을 실행하면 편집기에 아래 내용이 표시된다.

 

[versions]
agp = "8.5.0"
kotlin = "1.9.0"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.3"
activityCompose = "1.9.0"
composeBom = "2024.04.01"
jetbrainsKotlinJvm = "1.9.0"
appcompat = "1.7.0"
material = "1.12.0"
hilt = "2.48.1"
orbit = "6.1.0"
dataStore = "1.1.1"
coil = "2.6.0"
coroutines-core = "1.7.3"
activity = "1.9.0"
constraintlayout = "2.1.4"
room = "2.6.1"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }

navigation-compose = { group = "androidx.navigation" , name = "navigation-compose", version = "2.7.7"}

datastore = { module = "androidx.datastore:datastore-preferences", version = "1.0.0" }

kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version = "1.5.0"}
hilt = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
hilt-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hilt" }
hilt-navigation-compose = { group = "androidx.hilt" , name = "hilt-navigation-compose", version = "1.0.0"}

orbit-core = { group = "org.orbit-mvi", name = "orbit-core", version.ref = "orbit"}
orbit-viewmodel = { group = "org.orbit-mvi", name = "orbit-viewmodel", version.ref = "orbit"}
orbit-compose = { group = "org.orbit-mvi", name = "orbit-compose", version.ref = "orbit"}

coil = { group = "io.coil-kt", name = "coil", version.ref = "coil"}
coil-compose = { group = "io.coil-kt", name = "coil-compose", version.ref = "coil"}

androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }

room-runtime = { group = "androidx.room", name = "room-ktx", version.ref = "room"}
room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room"}
room-paging = { group = "androidx.room", name = "room-paging", version.ref = "room"}

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "jetbrainsKotlinJvm" }
android-library = { id = "com.android.library", version.ref = "agp" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt"}
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version = "1.8.10"}
kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }

 

앱 gradle도 이전과 다른 형태로 작성된 걸 볼 수 있다.

 

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)

 

이 방식은 버전 카탈로그라 불리는 방식으로 toml 파일에 versions, libraries, plugins 3가지 섹션으로 나눠서 의존성을 관리한다. 안드로이드 디벨로퍼에서 설명하는 버전 카탈로그를 확인해 본다.

 

https://developer.android.com/build/migrate-to-catalogs?hl=ko

 

버전 카탈로그로 빌드 이전  |  Android Studio  |  Android Developers

Gradle 구성 파일을 Gradle 버전 카탈로그로 이전합니다.

developer.android.com

gradle 버전 카탈로그를 쓰면...(중략)...여러 모듈이 있을 때 종속 항목, 플러그인을 더 쉽게 관리할 수 있다...(중략)...종속 항목의 중앙 버전 카탈로그를 생성하면 다양한 모듈에서 안드로이드 스튜디오 지원을 활용해 유형 안전 방식으로 이를 참조할 수 있다

 

지금까지 익숙하게 사용하던 의존성 관리 방식은 앱 gradle에 implementation을 아래와 비슷한 형태로 작성하는 것이었다.

 

dependencies {
    implementation("androidx.core:core-ktx:1.9.0")
}

 

버전 카탈로그를 적용하려면 아래와 같이 toml 파일에 작성한다.

 

[versions]
ktx = "1.9.0"

[libraries]
androidx-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "ktx" }

 

그리고 앱 gradle에 아래와 같이 적는다.

 

dependencies {
   implementation(libs.androidx.ktx)
}

 

라이브러리 의존성을 추가하는 경우, plugins에는 아무것도 적을 필요가 없을 수도 있다.

그럼 이 버전 카탈로그라는 것은 뭘까? gradle 홈페이지에 있는 설명을 확인한다.

 

https://docs.gradle.org/current/userguide/platforms.html

 

Sharing dependency versions between projects

Declaring a libs.versions.toml file doesn’t make it the single source of truth for dependencies: it’s a conventional location where dependencies can be declared. As soon as you start using catalogs, it’s strongly recommended to declare all your depen

docs.gradle.org

버전 카탈로그는 사용자가 빌드 스크립트에서 의존성을 선언할 때 사용자가 선택할 수 있는 의존성 좌표로 표시되는 의존성 목록이다. 예를 들어 문자열 표기법(string notation)을 써서 종속성을 선언하는 대신 버전 카탈로그에서 의존성 좌표를 선택할 수 있다
dependencies {
    implementation(libs.groovy.core)
}
이 맥락에서 libs는 카탈로그고 groovy는 이 카탈로그에서 사용할 수 있는 의존성이다. 버전 카탈로그는 빌드 스크립트에서 직접 의존성을 선언하는 것보다 여러 이점을 제공한다

- 각 카탈로그에 대해 gradle은 type-safe 접근자를 생성하므로 IDE에서 자동 완성으로 의존성을 쉽게 추가할 수 있다
- 각 카탈로그는 빌드의 모든 프로젝트에서 볼 수 있다. 의존성 버전을 선언하고 해당 버전에 대한 변경사항이 모든 하위 프로젝트에 적용되는지 확인할 수 있는 중심 장소다
- 카탈로그는 일반적으로 함께 쓰이는 의존성 그룹인 의존성 번들을 선언할 수 있다
- 카탈로그는 의존성 그룹, 이름을 실제 버전과 분리하고 버전 참조를 대신 사용할 수 있어서 여러 의존성 간에 버전 선언을 공유할 수 있다...(중략)

별칭(Aliases)은 대시("-", 권장함), 밑줄, 점으로 구분된 일련의 식별자로 구성돼야 한다. 식별자는 가급적 소문자, 소문자 다음에 숫자로 구성돼야 한다

- guava = 유효함
- groovy-core = 유효함
- commons-lang3 = 유효함
- androidx.awesome.lib = 유효함
- this.#is = 유효하지 않음

그 후 각 하위 그룹에 대해 type-safe 접근자가 생성된다. 버전 카탈로그에 libs라는 별칭이 있다고 가정한다면 아래와 같은 type-safe 접근자를 생성한다

- libs.guava
- libs.groovy.core
- libs.groovy.xml
- libs.groovy.json
- libs.androidx.awesome.lib

libs 접두사는 버전 카탈로그 이름에서 유래한다(안드로이드는 libs.versions.toml 파일의 첫 문자). 하위 그룹 접근자가 생성되지 않게 하려면 대소문자를 써서 구분하는 게 좋다...(중략)...별칭 선언 시 "-", 밑줄, 점을 구분 기호로 쓸 수 있지만 생성된 카탈로그는 모두 ":"로 정규화된다. foo-bar는 자동으로 foo.bar로 변환된다

 

반응형
Comments