在專案中新增元件
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
開始前,請先參閱「應用程式架構指南」,瞭解適用所有 Android 應用程式的實用原則,以及搭配使用架構元件的相關指引。
Google 的 Maven 存放區提供了架構元件,將存放區新增至專案,即可使用這些元件。
請開啟 settings.gradle
檔案並新增 google()
存放區:
Groovy
dependencyResolutionManagement {
...
repositories {
google()
jcenter()
}
}
Kotlin
dependencyResolutionManagement {
...
repositories {
google()
jcenter()
}
}
宣告依附元件
開啟應用程式或模組的 build.gradle
檔案,然後新增要做為依附元件的構件。您可以為所有架構元件新增依附元件或選擇子集。
請參閱版本資訊,瞭解如何為每個架構元件宣告依附元件:
請參閱 AndroidX 版本,瞭解各元件的最新版本號碼。
如想進一步瞭解 AndroidX 重構,以及它如何影響這些類別套件及模組 ID,請參閱「遷移至 AndroidX」。
Kotlin
Kotlin 擴充功能模組支援使用多個 AndroidX 依附元件。這些模組的名稱字尾有 -ktx
。例如:
Groovy
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
Kotlin
implementation("androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version")
設定為
Groovy
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
Kotlin
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
如需更多資訊 (包括 Kotlin 擴充功能的文件),請參閱 KTX 說明文件。
注意:針對 Kotlin 基礎應用程式,請務必使用 kapt
,而非 annotationProcessor
。建議您一併新增 kotlin-kapt
外掛程式。
為您推薦
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2023-09-24 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2023-09-24 (世界標準時間)。"],[],[],null,["# Add components to your project\n\n[Issue Tracker](https://issuetracker.google.com/issues/new?component=197448&template=878802)\n\nReport issues so we can fix bugs.\n\nBefore getting started, read the\n[Guide to app architecture](/topic/architecture) for useful principles that\napply to all Android apps and for guidance on\nusing architecture components together.\n\nArchitecture components are available from Google's Maven repository. To use\nthem, add the repository to your project.\n\nOpen the `settings.gradle` file and add the `google()` repository: \n\n### Groovy\n\n```groovy\ndependencyResolutionManagement {\n ...\n repositories {\n google()\n jcenter()\n }\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencyResolutionManagement {\n ...\n repositories {\n google()\n jcenter()\n }\n}\n```\n| **Warning:** The JCenter repository is read-only, as of March 31, 2021. For more information, see [JCenter service update](/studio/build/jcenter-migration).\n\nDeclare dependencies\n--------------------\n\nOpen the `build.gradle` file for your app or module and add the artifacts\nthat you need as dependencies. You can add dependencies for all architecture\ncomponents or choose a subset.\n\nSee the instructions for declaring dependencies for each architecture component in the release notes:\n\n- [Futures (found in androidx.concurrent)](/jetpack/androidx/releases/concurrent)\n- [Lifecycle (including ViewModel)](/jetpack/androidx/releases/lifecycle)\n- [Navigation (including SafeArgs)](/jetpack/androidx/releases/navigation)\n- [Paging](/jetpack/androidx/releases/paging)\n- [Room](/jetpack/androidx/releases/room)\n- [WorkManager](/jetpack/androidx/releases/work)\n\nSee [AndroidX releases](/jetpack/androidx/versions) for the most up-to-date\nversion numbers for each component.\n\nFor more information about the AndroidX refactor and how it affects these class\npackages and module IDs, see [Migrate to AndroidX](/jetpack/androidx/migrate).\n\nKotlin\n------\n\nKotlin extension modules are supported for several AndroidX dependencies. These\nmodules have the suffix `-ktx` appended to their names. For example: \n\n### Groovy\n\n```groovy\nimplementation \"androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version\"\n```\n\n### Kotlin\n\n```kotlin\nimplementation(\"androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version\")\n```\n\nbecomes \n\n### Groovy\n\n```groovy\nimplementation \"androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version\"\n```\n\n### Kotlin\n\n```kotlin\nimplementation(\"androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version\")\n```\n\nMore information, including docs for Kotlin extensions, can be found in the\n[KTX documentation](/kotlin/ktx).\n\n**Note:** For Kotlin based apps, make sure you use\n`kapt` instead of `annotationProcessor`. You should also\nadd the `kotlin-kapt` plugin.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [App Architecture: UI Layer - Get Started - Android Developers](/topic/libraries/data-binding/start)\n- [Work with observable data objects](/topic/libraries/data-binding/observability)\n- [Migrate from Kotlin synthetics to Jetpack view binding](/topic/libraries/view-binding/migration)"]]