Kotlin Multiplatform에는 라이브러리 모듈을 구성하는 Gradle 플러그인이 있습니다.
com.android.kotlin.multiplatform.library
플러그인은 일반 Android 라이브러리 Gradle 플러그인 (com.android.library
)을 사용하여 KMP 라이브러리에 Android 타겟을 추가하는 이전 방법을 공식적으로 대체합니다.
이전 접근 방식은 이제 Android-KMP 플러그인이라고도 하는 플러그인으로 대체되어 지원 중단됩니다. KMP용 com.android.library
플러그인을 계속 사용하면 JetBrains에서 더 이상 지원하지 않으며 향후 업데이트 및 개선사항의 혜택을 누릴 수 없습니다.
이 플러그인으로 이전하려면 Android-KMP 플러그인 적용 섹션을 참고하세요.
주요 기능 및 차이점
Android-KMP 플러그인은 KMP 프로젝트에 맞게 특별히 조정되었으며 다음과 같은 몇 가지 주요 측면에서 표준 com.android.library
플러그인과 다릅니다.
단일 변형 아키텍처: 플러그인은 단일 변형을 사용하여 제품 버전과 빌드 유형 지원을 삭제하므로 구성이 간소화되고 빌드 성능이 향상됩니다.
KMP에 최적화: 이 플러그인은 공유 Kotlin 코드와 상호 운용성에 중점을 두고 Android 전용 네이티브 빌드, AIDL, RenderScript 지원을 생략하여 KMP 라이브러리용으로 설계되었습니다.
기본적으로 사용 중지된 테스트: 빌드 속도를 높이기 위해 단위 테스트와 기기 (계측) 테스트가 모두 기본적으로 사용 중지됩니다. 필요한 경우 사용 설정할 수 있습니다.
최상위 Android 확장 프로그램 없음: 구성은 Gradle KMP DSL 내의
androidLibrary
블록으로 처리되어 일관된 KMP 프로젝트 구조를 유지합니다. 최상위android
확장 프로그램 차단이 없습니다.선택적 Java 컴파일: Java 컴파일은 기본적으로 사용 중지되어 있습니다.
androidLibrary
블록에서withJava()
를 사용하여 사용 설정합니다. 이렇게 하면 Java 컴파일이 필요하지 않은 경우 빌드 시간이 개선됩니다.
Android-KMP 라이브러리 플러그인의 이점
Android-KMP 플러그인은 KMP 프로젝트에 다음과 같은 이점을 제공합니다.
빌드 성능 및 안정성 개선: KMP 프로젝트 내에서 최적화된 빌드 속도와 향상된 안정성을 위해 설계되었습니다. KMP 워크플로에 중점을 두어 빌드 프로세스를 더 효율적이고 안정적으로 만들 수 있습니다.
향상된 IDE 통합: KMP Android 라이브러리로 작업할 때 더 나은 코드 완성, 탐색, 디버깅, 전반적인 개발자 환경을 제공합니다.
간소화된 프로젝트 구성: 이 플러그인은 빌드 변형과 같은 Android 관련 복잡성을 제거하여 KMP 프로젝트의 구성을 간소화합니다. 이렇게 하면 더 깔끔하고 유지관리 가능한 빌드 파일이 생성됩니다. 이전에는 KMP 프로젝트에서
com.android.library
플러그인을 사용하면androidAndroidTest
과 같은 혼동스러운 소스 세트 이름이 생성될 수 있었습니다. 이 명명 규칙은 표준 KMP 프로젝트 구조에 익숙한 개발자에게는 직관적이지 않았습니다.
기존 모듈에 Android-KMP 플러그인 적용
기존 KMP 라이브러리 모듈에 Android-KMP 플러그인을 적용하려면 다음 단계를 따르세요.
버전 카탈로그에서 플러그인 선언 버전 카탈로그 TOML 파일(일반적으로
gradle/libs.versions.toml
)을 열고 플러그인 정의 섹션을 추가합니다.# To check the version number of the latest Kotlin release, go to # https://kotlinlang.org/docs/releases.html [versions] androidGradlePlugin = "8.12.0" kotlin = "KOTLIN_VERSION" [plugins] kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } android-kotlin-multiplatform-library = { id = "com.android.kotlin.multiplatform.library", version.ref = "androidGradlePlugin" }
루트 빌드 파일에 플러그인 선언을 적용합니다. 프로젝트의 루트 디렉터리에 있는
build.gradle.kts
파일을 엽니다.apply false
을 사용하여 플러그인 별칭을plugins
블록에 추가합니다. 이렇게 하면 플러그인 로직을 루트 프로젝트 자체에 적용하지 않고도 모든 하위 프로젝트에서 플러그인 별칭을 사용할 수 있습니다.Kotlin
// Root build.gradle.kts file plugins { alias(libs.plugins.kotlin.multiplatform) apply false // Add the following alias(libs.plugins.android.kotlin.multiplatform.library) apply false }
Groovy
// Root build.gradle file plugins { alias(libs.plugins.kotlin.multiplatform) apply false // Add the following alias(libs.plugins.android.kotlin.multiplatform.library) apply false }
KMP 라이브러리 모듈 빌드 파일에 플러그인을 적용합니다. KMP 라이브러리 모듈에서
build.gradle.kts
파일을 열고plugins
블록 내 파일 상단에 플러그인을 적용합니다.Kotlin
// Module-specific build.gradle.kts file plugins { alias(libs.plugins.kotlin.multiplatform) // Add the following alias(libs.plugins.android.kotlin.multiplatform.library) }
Groovy
// Module-specific build.gradle file plugins { alias(libs.plugins.kotlin.multiplatform) // Add the following alias(libs.plugins.android.kotlin.multiplatform.library) }
Android KMP 타겟을 구성합니다. Android 타겟을 정의하도록 Kotlin Multiplatform 블록(
kotlin
)을 구성합니다.kotlin
블록 내에서androidLibrary
를 사용하여 Android 타겟을 지정합니다.Kotlin
kotlin { androidLibrary { namespace = "com.example.kmpfirstlib" compileSdk = 33 minSdk = 24 withJava() // enable java compilation support withHostTestBuilder {}.configure {} withDeviceTestBuilder { sourceSetTreeName = "test" } compilations.configureEach { compilerOptions.configure { jvmTarget.set( org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8 ) } } } sourceSets { androidMain { dependencies { // Add Android-specific dependencies here } } getByName("androidHostTest") { dependencies { } } getByName("androidDeviceTest") { dependencies { } } } // ... other targets (JVM, iOS, etc.) ... }
Groovy
kotlin { androidLibrary { namespace = "com.example.kmpfirstlib" compileSdk = 33 minSdk = 24 withJava() // enable java compilation support withHostTestBuilder {}.configure {} withDeviceTestBuilder { it.sourceSetTreeName = "test" } compilations.configureEach { compilerOptions.options.jvmTarget.set( org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8 ) } } sourceSets { androidMain { dependencies { } } androidHostTest { dependencies { } } androidDeviceTest { dependencies { } } } // ... other targets (JVM, iOS, etc.) ... }
변경사항 적용 플러그인을 적용하고
kotlin
블록을 구성한 후 Gradle 프로젝트를 동기화하여 변경사항을 적용합니다.
추천 서비스
- 참고: JavaScript가 사용 중지되어 있으면 링크 텍스트가 표시됩니다.
- 환경 설정
- 프로젝트에 KMP 모듈 추가