1. 루트 디렉토리 build.gradle 변경사항
변경 전:
buildscript {
ext.kotlin_version = '1.5.0'
repository {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.17"
// 사용자 정의.gradle 플러그인
classpath "com.sharpcj.plugin:abc:1.0.6"
}
}
변경 후:
settings.gradle로 이동한 변경사항:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
maven {
allowInsecureProtocol = true
url "http://xx.xx.xx.xx:xxxx/xx/xx/"
}
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
maven {
allowInsecureProtocol = true
url "http://xx.xx.xx.xx:xxxx/xx/xx/"
}
}
}
7.0.x 이상 버전에서 http 프로토콜 사용 시 allowInsecureProtocol = true를 명시적으로 지정해야 합니다.
루트 디렉토리 build.gradle:
buildscript {
ext.kotlin_version = '1.5.0'
// 사용자 정의.gradle 플러그인
dependencies {
classpath "com.sharpcj.plugin:abc:1.0.6"
}
}
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
id 'com.google.protobuf' version '0.8.17' apply false
}
2. aar 패키지.gradle로의 의존성 추가
7.0.x 이전:
- libs 폴더에 aar 파일을 복사합니다.
- 모듈의 build.gradle 파일에서 android {} 외부에:
- dependencies에:
repositories {
flatDir {
dirs 'libs'
}
}
implementation files('libs/xxx.jar')
또는:
implementation(fileTree("libs"))
7.0.x 이상:
- libs 폴더에 aar 파일을 복사합니다.
- build.gradle의 dependencies에:
implementation(fileTree("libs"))
3. 버전 카탈로그를 통한 의존성 관리
3.1. Android 의존성 통합 관리 방법
3.1.1. 전통적인 apply from 방법
루트 디렉토리에 config.gradle 파일을 만들거나 루트 build.gradle에 변수를 정의합니다.
ext {
// ...
}
모듈 build.gradle에서:
apply from "config.gradle"
3.1.2. buildSrc 방법
buildSrc 디렉토리는 Gradle이 실행될 때 자동으로 컴파일합니다. 여러 프로젝트를 관리할 때 유용하지만, 의존성 업데이트시 전체 프로젝트가 다시 컴파일됩니다.
3.1.3. Composite Builds
Composite Builds는 다른 build를 포함하는 방법으로, 의존성 업데이트시 전체 프로젝트를 다시 컴파일하지 않습니다.
3.2. 버전 카탈로그 사용 방법
3.2.1. 버전 카탈로그의 특징
- 모든 모듈에서 공통적으로 의존성을 관리할 수 있습니다.
- 의존성 버전을 중앙에서 관리할 수 있습니다.
- 의존성 그룹핑이 가능합니다.
- 버전과 의존성명을 분리하여 관리할 수 있습니다.
- libs.versions.toml 파일을 통해 의존성을 관리할 수 있습니다.
3.2.2. 버전 카탈로그 활성화
settings.gradle
enableFeaturePreview("VERSION_CATALOGS")
7.4.1 버전 이후에는 더 이상 실험적 기능이 아니며 기본적으로 활성화됩니다.
3.2.3. 버전 카탈로그 사용 방법
settings.gradle에서 버전 카탈로그를 선언:
settings.gradle
versionCatalogs {
libs {
alias('coreKtx').to('androidx.core', 'core-ktx').version('1.7.0')
alias('appcompat').to('androidx.appcompat', 'appcompat').version('1.3.0')
// ...
}
}
또는 libs.versions.toml 파일을 통해:
[versions]
kotlin = "1.7.0"
appcompat = "1.3.0"
[libraries]
coreKtx = { module = "androidx.core:core-ktx", version.ref = "kotlin" }
appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
// ...
settings.gradle에서 TOML 파일을 참조:
libs {
from(files("./libs.versions.toml"))
}
모듈 build.gradle에서 버전 카탈로그를 사용:
plugins {
alias libs.plugins.kotlin.kapt
// ...
}
dependencies {
implementation libs.coreKtx
// ...
}