Migrate build configuration to version catalog

노준혁·2023년 6월 9일

https://developer.android.com/build/migrate-to-catalogs
https://www.youtube.com/watch?v=nwORxiiKuEc&ab_channel=Droidhalla
https://www.youtube.com/watch?v=WvtcCCCLfOc&ab_channel=TomGregory
https://www.youtube.com/watch?v=tWZgw127Vf8&ab_channel=PiAppStudio


Version Catalog

  • Gradle version catalogs 사용 시 확장 가능한 방식으로 dependency과 PlugIn 추가 유지 가능
  • 여러 모듈이 있을 때 dependency과 PlugIn 쉽게 관리 가능
  • 개별 빌드 파일에 dependency 이름과 버전을 하드코딩하고 dependency를 업그레이드할 때마다 각 항목을 업데이트 하는 대신 다양한 모듈이 안전한 방식으로 참조할 수 있는 dependency의 central version catalog 생성

Create a version catalog file

  1. Version catalog 파일 생성
  2. root project's gradle folder, create a file called libs.versions.toml.
  3. Gradle은 기본적으로 libs.versions.toml file에서 catalog를 찾기 때문에 파일 이름은 위 기본 명칭 그대로 가져가는게 좋다.

(Note: It's possible to change the catalog file name; however, this requires changing your build files, so we don't recommend doing it.)

  • libs.versions.toml에는 다음과 같은 3개의 섹션이 들어간다.
[versions] 

[libraries]

[plugins]

각 섹션은 다음과 같이 사용된다.

  • In the versions block, dependencies and plugins의 버전을 가지고 있는 변수를 정의한다. subsequent blocks (the versions and plugins blocks)에서 정의된 변수를 사용.
  • In the libraries block, dependencies을 정의한다.
  • In the plugins block, plugins을 정의한다.

마이그레이션 단계

빌드는 build scripts와 catalogs의 dependencies와 plugins를 동시에 사용할 수 있으므로 dependencies와 plugin을 개별적으로 마이그레이션해야 함.

The migration process is:

  1. catalog에 새 entry 추가
  2. Sync your Android project
  3. 기존에 String 선언 형태로 작성된 부분들을 catalog type-safe accessor로 바꾼다.

Migrate dependencies

아래 코드 스니펫은 build.gradle.kts file의 기존 dependency를 제거하기 전이다.

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

아래 코드 스니펫은 version catalog 파일에서 dependency를 정의하는 방법이다.

[versions]
ktx = "1.9.0"

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

catalog에서 권장되는 dependency block에 대한 네이밍 방식은 kebab case이다.

(such as androidx-ktx) → for better code completion assistance in your build files.

dependencies {
   implementation(libs.androidx.ktx)
}

Migrate plugins

  1. 파일의 version 및 plugin 섹션 모두에서 각 plugin에 대한 entry 추가
  2. Sync Project
  3. plugins{} block에 있는 기존 String 선언들을 catalog name으로 바꾼다.

아래 코드 스니펫은 build.gradle.kts file의 기존 plugin 네임을 제거하기 전이다.

// Top-level `build.gradle.kts` file
plugins {
   id("com.android.application") version "7.4.1" apply false
}

// Module-level `build.gradle.kts` file
plugins {
   id("com.android.application")
}

아래 코드 스니펫은 버전 카탈로그 파일에서 플러그인을 정의하는 방법.

[versions]
androidGradlePlugin = "7.4.1"

[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }

As with dependencies, the recommended formatting for plugins block catalog entries is kebab case (such as android-application) → for better code completion assistance in your build files.

아래 코드 스니펫은 catalog를 적용한 뒤.

// Top-level build.gradle.kts
plugins {
   alias(libs.plugins.android.application) apply false

}

// module build.gradle.kts
plugins {
   alias(libs.plugins.android.application)
}

주의!

plugins{}@Suppress("DSL_SCOPE_VIOLATION")

참고: 8.1 미만의 Gradle 버전을 사용하는 경우 version catalog를 사용할 때 블록에 주석을 달아야 한다.

자세한 내용은 문제 #22797을 참조

profile
https://github.com/nohjunh

0개의 댓글