Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Kotlin Multiplatform có một trình bổ trợ Gradle để định cấu hình mô-đun thư viện.
Trình bổ trợ com.android.kotlin.multiplatform.library là giải pháp thay thế chính thức cho phương thức trước đây để thêm các mục tiêu Android vào thư viện KMP bằng trình bổ trợ Gradle thông thường cho thư viện Android (com.android.library).
Phương pháp trước đây hiện không được dùng nữa, thay vào đó là trình bổ trợ (còn được gọi là trình bổ trợ Android-KMP). JetBrains sẽ không còn hỗ trợ việc tiếp tục sử dụng trình bổ trợ com.android.library cho KMP và trình bổ trợ này sẽ không được hưởng lợi từ các bản cập nhật và điểm cải tiến trong tương lai.
Trình bổ trợ Android-KMP được thiết kế riêng cho các dự án KMP và khác với trình bổ trợ com.android.library tiêu chuẩn ở một số khía cạnh chính:
Cấu trúc một biến thể: Trình bổ trợ này sử dụng một biến thể duy nhất, loại bỏ việc hỗ trợ các phiên bản sản phẩm và loại bản dựng, giúp đơn giản hoá cấu hình và nâng cao hiệu suất bản dựng.
Được tối ưu hoá cho KMP: Trình bổ trợ này được thiết kế cho các thư viện KMP, tập trung vào mã Kotlin dùng chung và khả năng tương tác, bỏ qua việc hỗ trợ các bản dựng gốc dành riêng cho Android, AIDL và RenderScript.
Kiểm thử bị tắt theo mặc định: Cả kiểm thử đơn vị và kiểm thử thiết bị (đo lường) đều bị tắt theo mặc định để tăng tốc độ bản dựng. Bạn có thể bật các tính năng này nếu cần.
Không có tiện ích Android cấp cao nhất: Cấu hình được xử lý bằng khối androidLibrary trong DSL KMP của Gradle, duy trì cấu trúc dự án KMP nhất quán. Không có khối tiện ích android cấp cao nhất.
Chọn tham gia biên dịch Java: Theo mặc định, tính năng biên dịch Java sẽ bị tắt. Hãy sử dụng withJava() trong khối androidLibrary để bật tính năng này. Điều này giúp cải thiện thời gian tạo bản dựng khi không cần biên dịch Java.
Lợi ích của trình bổ trợ thư viện Android-KMP
Trình bổ trợ Android-KMP mang lại những lợi ích sau cho các dự án KMP:
Cải thiện hiệu suất và độ ổn định của bản dựng: Được thiết kế để tối ưu hoá tốc độ bản dựng và tăng cường độ ổn định trong các dự án KMP. Việc tập trung vào quy trình KMP góp phần tạo nên quy trình xây dựng hiệu quả và đáng tin cậy hơn.
Tích hợp IDE nâng cao: Tính năng này giúp hoàn thành mã, điều hướng, gỡ lỗi và mang lại trải nghiệm tổng thể tốt hơn cho nhà phát triển khi làm việc với các thư viện KMP Android.
Đơn giản hoá cấu hình dự án: Trình bổ trợ này đơn giản hoá cấu hình cho các dự án KMP bằng cách loại bỏ các điểm phức tạp dành riêng cho Android, chẳng hạn như các biến thể bản dựng. Điều này giúp các tệp bản dựng trở nên gọn gàng và dễ duy trì hơn.
Trước đây, việc sử dụng trình bổ trợ com.android.library trong dự án KMP có thể tạo ra các tên tập hợp nguồn gây nhầm lẫn, chẳng hạn như androidAndroidTest. Quy ước đặt tên này ít trực quan hơn đối với những nhà phát triển quen thuộc với cấu trúc dự án KMP tiêu chuẩn.
Áp dụng trình bổ trợ Android-KMP cho một mô-đun hiện có
Để áp dụng trình bổ trợ Android-KMP cho một mô-đun thư viện KMP hiện có, hãy làm theo các bước sau:
Khai báo trình bổ trợ trong danh mục phiên bản. Mở tệp TOML của danh mục phiên bản (thường là gradle/libs.versions.toml) rồi thêm phần định nghĩa trình bổ trợ:
# 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"}
Áp dụng khai báo trình bổ trợ trong tệp bản dựng gốc. Mở tệp build.gradle.kts nằm trong thư mục gốc của dự án. Thêm các bí danh trình bổ trợ vào khối plugins bằng cách sử dụng apply false. Điều này giúp mọi dự án con đều có thể sử dụng các bí danh trình bổ trợ mà không cần áp dụng logic trình bổ trợ cho chính dự án gốc.
Kotlin
// Root build.gradle.kts fileplugins{alias(libs.plugins.kotlin.multiplatform)applyfalse// Add the followingalias(libs.plugins.android.kotlin.multiplatform.library)applyfalse}
Groovy
// Root build.gradle fileplugins{alias(libs.plugins.kotlin.multiplatform)applyfalse// Add the followingalias(libs.plugins.android.kotlin.multiplatform.library)applyfalse}
Áp dụng trình bổ trợ trong tệp bản dựng mô-đun thư viện KMP. Mở tệp build.gradle.kts trong mô-đun thư viện KMP và áp dụng trình bổ trợ ở đầu tệp trong khối plugins:
Kotlin
// Module-specific build.gradle.kts fileplugins{alias(libs.plugins.kotlin.multiplatform)// Add the followingalias(libs.plugins.android.kotlin.multiplatform.library)}
Groovy
// Module-specific build.gradle fileplugins{alias(libs.plugins.kotlin.multiplatform)// Add the followingalias(libs.plugins.android.kotlin.multiplatform.library)}
Định cấu hình mục tiêu KMP của Android. Định cấu hình khối Kotlin Multiplatform (kotlin) để xác định mục tiêu Android. Trong khối kotlin, hãy chỉ định mục tiêu Android bằng cách sử dụng androidLibrary:
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-21 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-21 UTC."],[],[],null,["# Migrate to the Android-KMP library plugin\n\nKotlin Multiplatform has a Gradle plugin to configure the\n[library module](/studio/projects/android-library).\n\nThe `com.android.kotlin.multiplatform.library` plugin is the official\nreplacement for the previous method of adding Android targets to KMP libraries\nusing the regular Android library Gradle plugin (`com.android.library`).\n\nThe previous approach is now deprecated in favor of the plugin, also referred to\nas the **Android-KMP plugin** . Continuing to use the `com.android.library`\nplugin for KMP will no longer be supported by JetBrains and won't benefit from\nfuture updates and improvements.\n| **Note:** Configuring the Android **application** module using `com.android.application` doesn't change.\n\nTo migrate to this plugin, refer to the [Apply the Android-KMP plugin](#apply)\nsection.\n\nKey features and differences\n----------------------------\n\nThe Android-KMP plugin is tailored specifically for KMP projects and differs\nfrom the standard `com.android.library` plugin in several key aspects:\n\n- **Single variant architecture:** The plugin uses a single variant, removing\n support for product flavors and build types, which simplifies configuration\n and enhances build performance.\n\n- **Optimized for KMP:** The plugin is designed for KMP libraries, focusing on\n shared Kotlin code and interoperability, omitting support for\n Android-specific native builds, AIDL, and RenderScript.\n\n- **Tests disabled by default:** Both unit and device (instrumentation) tests\n are disabled by default to enhance build speed. You can enable them if\n required.\n\n- **No top-Level Android extension:** Configuration is handled with an\n `androidLibrary` block within the Gradle KMP DSL, maintaining a consistent\n KMP project structure. There's no top-level `android` extension block.\n\n- **Opt-in Java compilation:** Java compilation is disabled by default. Use\n `withJava()` in the `androidLibrary` block to enable it. This improves build\n times when Java compilation is not needed.\n\nBenefits of the Android-KMP library plugin\n------------------------------------------\n\nThe Android-KMP plugin provides the following benefits for KMP projects:\n\n- **Improved build performance and stability:** It's engineered for optimized\n build speeds and enhanced stability within KMP projects. It's focus on KMP\n workflows contribute to a more efficient and reliable build process.\n\n- **Enhanced IDE integration:** It provides better code completion,\n navigation, debugging, and overall developer experience when working with\n KMP Android libraries.\n\n- **Simplified project configuration:** The plugin simplifies configuration\n for KMP projects by removing Android-specific complexities like build\n variants. This leads to cleaner and more maintainable build files.\n Previously, using the `com.android.library` plugin in KMP project could\n create confusing source set names, such as `androidAndroidTest`. This naming\n convention was less intuitive for developers familiar with standard KMP\n project structures.\n\nApply the Android-KMP plugin to an existing module\n--------------------------------------------------\n\nTo apply the Android-KMP plugin to an existing KMP library module, follow these\nsteps:\n\n1. **Declare plugins in version catalog.** Open the version catalog TOML file\n (usually `gradle/libs.versions.toml`) and add the plugin definitions\n section:\n\n # To check the version number of the latest Kotlin release, go to\n # https://kotlinlang.org/docs/releases.html\n\n [versions]\n androidGradlePlugin = \"8.12.0\"\n kotlin = \"\u003cvar translate=\"no\"\u003eKOTLIN_VERSION\u003c/var\u003e\"\n\n [plugins]\n kotlin-multiplatform = { id = \"org.jetbrains.kotlin.multiplatform\", version.ref = \"kotlin\" }\n android-kotlin-multiplatform-library = { id = \"com.android.kotlin.multiplatform.library\", version.ref = \"androidGradlePlugin\" }\n\n2. **Apply the plugin declaration in root build file.** Open the\n `build.gradle.kts` file located in the root directory of your project. Add\n the plugin aliases to the `plugins` block using `apply false`. This makes\n the plugin aliases available to all subprojects without applying the plugin\n logic to the root project itself.\n\n ### Kotlin\n\n ```kotlin\n // Root build.gradle.kts file\n\n plugins {\n alias(libs.plugins.kotlin.multiplatform) apply false\n\n // Add the following\n alias(libs.plugins.android.kotlin.multiplatform.library) apply false\n }\n ```\n\n ### Groovy\n\n ```groovy\n // Root build.gradle file\n\n plugins {\n alias(libs.plugins.kotlin.multiplatform) apply false\n\n // Add the following\n alias(libs.plugins.android.kotlin.multiplatform.library) apply false\n }\n ```\n3. **Apply the plugin in a KMP library module build file.** Open the\n `build.gradle.kts` file in your KMP library module and apply the plugin at\n the top of your file within the `plugins` block:\n\n ### Kotlin\n\n ```kotlin\n // Module-specific build.gradle.kts file\n\n plugins {\n alias(libs.plugins.kotlin.multiplatform)\n\n // Add the following\n alias(libs.plugins.android.kotlin.multiplatform.library)\n }\n ```\n\n ### Groovy\n\n ```groovy\n // Module-specific build.gradle file\n\n plugins {\n alias(libs.plugins.kotlin.multiplatform)\n\n // Add the following\n alias(libs.plugins.android.kotlin.multiplatform.library)\n }\n ```\n4. **Configure Android KMP target.** Configure the Kotlin Multiplatform block\n (`kotlin`) to define the Android target. Within the `kotlin` block, specify\n the Android target using `androidLibrary`:\n\n ### Kotlin\n\n ```kotlin\n kotlin {\n androidLibrary {\n namespace = \"com.example.kmpfirstlib\"\n compileSdk = 33\n minSdk = 24\n\n withJava() // enable java compilation support\n withHostTestBuilder {}.configure {}\n withDeviceTestBuilder {\n sourceSetTreeName = \"test\"\n }\n\n compilations.configureEach {\n compilerOptions.configure {\n jvmTarget.set(\n org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8\n )\n }\n }\n }\n\n sourceSets {\n androidMain {\n dependencies {\n // Add Android-specific dependencies here\n }\n }\n getByName(\"androidHostTest\") {\n dependencies {\n }\n }\n\n getByName(\"androidDeviceTest\") {\n dependencies {\n }\n }\n }\n // ... other targets (JVM, iOS, etc.) ...\n }\n ```\n\n ### Groovy\n\n ```groovy\n kotlin {\n androidLibrary {\n namespace = \"com.example.kmpfirstlib\"\n compileSdk = 33\n minSdk = 24\n\n withJava() // enable java compilation support\n withHostTestBuilder {}.configure {}\n withDeviceTestBuilder {\n it.sourceSetTreeName = \"test\"\n }\n\n compilations.configureEach {\n compilerOptions.options.jvmTarget.set(\n org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8\n )\n }\n }\n\n sourceSets {\n androidMain {\n dependencies {\n }\n }\n androidHostTest {\n dependencies {\n }\n }\n androidDeviceTest {\n dependencies {\n }\n }\n }\n // ... other targets (JVM, iOS, etc.) ...\n }\n ```\n5. **Apply changes.** After applying the plugin and configuring the `kotlin`\n block, sync your Gradle project to apply the changes.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Set up your environment](/kotlin/multiplatform/setup)\n- [Add KMP module to a project](/kotlin/multiplatform/migrate)"]]