[[["易于理解","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"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Migrate your build to version catalogs\n\n[Gradle version catalogs](https://docs.gradle.org/current/userguide/platforms.html)\nenable you to add and maintain dependencies and plugins in a scalable way.\nUsing Gradle version catalogs makes managing dependencies and plugins easier\nwhen you have [multiple modules](/topic/modularization). Instead of hardcoding\ndependency names and versions in individual build files and updating each\nentry whenever you need to upgrade a dependency, you can create a central\n*version catalog* of dependencies that various modules can reference in\na type-safe way with Android Studio assistance.\n\nThis page provides basic information about migrating your Android app to\nversion catalogs. To learn more, see\n[Add build dependencies](/build/dependencies) and the Gradle documentation.\n\nCreate a version catalog file\n-----------------------------\n\nStart by creating a version catalog file. In your root project's `gradle`\nfolder, create a file called `libs.versions.toml`. Gradle looks for the catalog\nin the `libs.versions.toml` file by [default](https://docs.gradle.org/current/userguide/platforms.html#sub:conventional-dependencies-toml),\nso we recommend using this default name.\n| **Note:** It's possible to change the catalog file name; however, this requires changing your build files, so we don't recommend doing it.\n\nIn your `libs.versions.toml` file, add these sections: \n\n [versions]\n\n [libraries]\n\n [plugins]\n\nThe sections are used as follows:\n\n- In the `versions` block, define variables that hold the versions of your dependencies and plugins. You use these variables in the subsequent blocks (the `libraries` and `plugins` blocks).\n- In the `libraries` block, define your dependencies.\n- In the `plugins` block, define your plugins.\n\nMigration steps\n---------------\n\nWe recommend you do the steps in the order listed. A build can consume\ndependencies and plugins from build scripts and catalogs simultaneously, so\ntake your time to migrate your dependencies and plugins individually.\n\nThe migration process is:\n\n1. Add the new entry to the catalog.\n2. Sync your Android project.\n3. Replace the previous string declaration with the catalog type-safe accessor.\n\n### Migrate dependencies\n\nAdd an entry for each dependency in both the `versions` and `libraries` sections\nof the `libs.versions.toml` file. Sync your project, and then replace their\ndeclarations in the build files with their catalog names.\n\nThis code snippet shows the `build.gradle.kts` file before removing the\ndependency: \n\n### Kotlin\n\n```kotlin\ndependencies {\n implementation(\"androidx.core:core-ktx:1.9.0\")\n\n}\n```\n\n### Groovy\n\n```groovy\ndependencies {\n implementation 'androidx.core:core-ktx:1.9.0'\n\n}\n```\n\nThis code snippet shows how to define the dependency in the version\ncatalog file: \n\n [versions]\n ktx = \"1.9.0\"\n\n [libraries]\n androidx-ktx = { group = \"androidx.core\", name = \"core-ktx\", version.ref = \"ktx\" }\n\nThe recommended naming for dependencies block in catalogs is kebab case (such as\n`androidx-ktx`) for better\n[code completion assistance](https://www.jetbrains.com/help/idea/auto-completing-code.html)\nin your build files.\n\nIn the `build.gradle.kts` file of each module that requires the dependency,\ndefine the dependencies by the names you defined in the TOML file. \n\n### Kotlin\n\n```kotlin\ndependencies {\n implementation(libs.androidx.ktx)\n\n}\n```\n\n### Groovy\n\n```groovy\ndependencies {\n implementation libs.androidx.ktx\n\n}\n```\n\n### Migrate plugins\n\nAdd an entry for each plugin in both the versions and the plugins sections of\nthe `libs.versions.toml` file. Sync your project, and then replace their\ndeclarations in the `plugins{}` block in the build files with their catalog\nnames.\n\nThis code snippet shows the `build.gradle.kts` file before removing the\nplugin: \n\n### Kotlin\n\n```kotlin\n// Top-level `build.gradle.kts` file\nplugins {\n id(\"com.android.application\") version \"7.4.1\" apply false\n\n}\n\n// Module-level `build.gradle.kts` file\nplugins {\n id(\"com.android.application\")\n\n}\n```\n\n### Groovy\n\n```groovy\n// Top-level `build.gradle` file\nplugins {\n id 'com.android.application' version '7.4.1' apply false\n\n}\n\n// Module-level `build.gradle` file\nplugins {\n id 'com.android.application'\n\n}\n```\n\nThis code snippet shows how to define the plugin in the version catalog file: \n\n [versions]\n androidGradlePlugin = \"7.4.1\"\n\n [plugins]\n android-application = { id = \"com.android.application\", version.ref = \"androidGradlePlugin\" }\n\nAs with dependencies, the recommended formatting for `plugins` block catalog\nentries is kebab case (such as `android-application`) for better\n[code completion assistance](https://www.jetbrains.com/help/idea/auto-completing-code.html)\nin your build files.\n\nThe following code shows how to define the `com.android.application` plugin in\nthe top and module level `build.gradle.kts` files. Use `alias` for plugins\nthat come from the version catalog file and `id` for plugins that don't come\nfrom the version catalog file, such as\n[convention plugins](https://docs.gradle.org/current/samples/sample_convention_plugins.html#organizing_build_logic). \n\n### Kotlin\n\n```kotlin\n// Top-level build.gradle.kts\nplugins {\n alias(libs.plugins.android.application) apply false\n\n}\n\n// module build.gradle.kts\nplugins {\n alias(libs.plugins.android.application)\n\n}\n```\n\n### Groovy\n\n```groovy\n// Top-level build.gradle\nplugins {\n alias libs.plugins.android.application apply false\n\n}\n\n// module build.gradle\nplugins {\n alias libs.plugins.android.application\n\n}\n```\n| **Note:** If you are using a version of Gradle below 8.1, you need to annotate the `plugins{}` block with `@Suppress(\"DSL_SCOPE_VIOLATION\")` when using version catalogs. Refer to [issue #22797](https://github.com/gradle/gradle/issues/22797) for more info.\n\nLearn more\n----------\n\nTo learn about additional options for configuring your version catalog, see\nthese resources:\n\n- [The version catalog TOML file format](https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format) documents additional options for configuring your catalog file.\n- [Now in Android](https://github.com/android/nowinandroid) is our sample app that uses version catalogs."]]