Migrate Xml Views To Jetpack Compose

android/skills/jetpack-compose/migration/migrate-xml-views-to-jetpack-compose

作者 android42dc2270e960Complete terms in LICENSE.txt7.7K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库13天前更新

Provides a structured workflow for migrating an Android XML View to Jetpack Compose. This skill details the step-by-step process, from planning and dependency setup, to theming and layout migration, validation and XML cleanup. Use this skill when you need to migrate an XML View to Jetpack Compose in an Android project. It solves the problem of converting the UI of a legacy XML View into modern, declarative Compose components while maintaining interoperability.

AI 生成的概览

指导将 Android XML View 布局迁移到 Jetpack Compose 的十步流程,并保持视觉一致。

功能
提供一套结构化的十步工作流,用于将单个旧版 Android XML View 布局转换为声明式 Jetpack Compose UI。流程涵盖候选选择、项目与布局分析、制定计划、捕获基准截图、配置 Compose 依赖与主题、布局迁移、替换用法、验证以及清理 XML。产出包括迁移后的 Compose 代码与 Compose Preview、互操作性指引以及 Compose UI 测试,目标是实现像素级视觉一致。
适用场景
适用于在 Android 项目中把 XML View 迁移到 Jetpack Compose,尤其是渐进式采用 Compose 的场景。既适合已明确指定某个 XML 布局的情况,也适合需要先确定最佳迁移候选的情况。该技能只处理 UI 迁移,不涉及更广泛的应用逻辑。
运行要求
仅为说明文档,不附带脚本。需要具备 Gradle 构建文件(build.gradle 或 libs.versions.toml)的 Android 项目,并能访问随附的参考文档。可选能力包括:支持用户交互以获取截图、可运行 Android 模拟器并具备 UI Automator 或 Espresso 等截图测试框架,以及 Compose 依赖与编译器配置。

This skill guides through the process of migrating an existing Android XML View to Jetpack Compose. It performs a stable, safe and visually consistent transition by following a structured, 10-step methodology. This skill migrates UI (XML to Jetpack Compose) only.

Objective

To systematically convert a single legacy XML layout into modern, declarative Jetpack Compose UI while maintaining pixel-perfect visual parity and functional integrity.

Summary of the 10-step migration process

  1. Identify the optimal XML candidate for migration
  2. Analyze the project and layout
  3. Create a plan
  4. Capture the XML View UI
  5. Set up Compose dependencies and compiler
  6. Set up Compose theming
  7. Migrate the XML layout to Compose
  8. Validate the migration
  9. Replace usages
  10. XML code removal

Detailed steps

Step 1: Identify the optimal XML candidate for migration

If the user has explicitly specified a target XML layout, proceed to Step 2. Otherwise, analyze the codebase to identify the best candidate for migration by following the logic in references/identify-optimal-xml-candidate.md [blocked].

Step 2: Analyze the project and layout

Analyze the identified XML View's structure, hierarchy, and implementation details. Use references/analysis-of-the-project-and-layout.md [blocked] to guide your technical audit of the layout and surrounding project context.

Step 3: Create a plan

Using the outputs and analysis done in the Step 1 and 2, generate a step-by-step plan for the migration. If you support user interaction, present to the user and ask for approval before proceeding. If user interaction is not supported, proceed to Step 4 following the generated plan.

Step 4: Capture the XML View UI

IF you support user interaction, ask the user to upload a screenshot of the XML View UI or provide an absolute path to a file. Use this image as a visual reference for the layout migration in Step 7. ELSE IF you are able to run an Android emulator, locate an existing screenshot test for the XML candidate. If none exists, create one using the existing project testing framework. If no framework exists, use UI Automator or Espresso to create a screenshot test with minimum required setup. Run the test and take a baseline screenshot of the XML UI. ELSE proceed to Step 5.

Step 5: Set up Compose dependencies and compiler

Check build.gradle or libs.versions.toml for Compose dependencies and compiler setup. If missing, use Setup Compose Dependencies and Compiler [blocked]. Run a sync to ensure dependencies resolve without errors.

Step 6: Set up Compose theming

If the project already has Compose theming set up, proceed to Step 7. If Compose theming is missing, initialize it. For Material-based projects, follow Material 3 migration guidelines [blocked]. For custom design systems, apply expert judgment to migrate XML theming and match existing styles. Constraints: Do not migrate the entire theme. Implement only the minimum theming required for the specific XML candidate. Maintain original XML themes for interoperability. Maintain existing project code conventions, patterns, names and values.

Step 7: Migrate the XML View to Compose

Convert the XML candidate to Jetpack Compose code, referencing references/xml-layout-migration.md [blocked] and the image from Step 4. You must include a Compose Preview for the newly created composable to facilitate visual verification.

Step 8: Replace usages

Replace the usages of the migrated XML layout to use the new Compose component.

  • To add Compose in Views, use Compose in Views [blocked].
  • To add Views in Compose, use Views in Compose [blocked].

Step 9: Validate the migration

Compare the baseline screenshot image from Step 4 with the rendered Compose Preview of the new composable. Ignore string content; focus on layout and styling. Iterate on the Compose code until visual parity is achieved. Once verified, write a Compose UI test for the new composable.

Step 10: XML code removal

Delete the migrated XML file and its associated legacy tests. Caution: Only remove code and resources that are not referenced by other parts of the project.

来源与署名

来源:android/skills位于jetpack-compose/migration/migrate-xml-views-to-jetpack-compose提交42dc227

许可证: Complete terms in LICENSE.txt

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架

更多来自 android/skills 的技能

Wear Compose M3

android

指导构建、更新和迁移 Wear OS Compose Material3 应用,并提供规范组件示例与版本规则。

Software Development7.7K13天前更新

Display Glasses With Jetpack Compose Glimmer

android

Provides guidelines for developing projected Android XR apps for display glasses using the Jetpack Compose Glimmer UI toolkit. This skill covers foundational Glimmer design principles, workflows for implementing Jetpack Compose Glimmer, and interaction models for the glasses form factor. Use this skill to build an Android XR Augmented Experience app with Jetpack Compose Glimmer that adheres to the Glimmer design system for optimized glasses styling.

待分类7.7K13天前更新

Play Billing Library Version Upgrade

android

指导将 Android 项目从旧版 Google Play 结算库升级到最新稳定版本。

Software Development7.7K13天前更新

Leanback To Compose Tv Migration

android

Provides instructions and architectural patterns for migrating Android TV applications from legacy Leanback UI Toolkit, Android Views, or Support Fragments to Jetpack Compose for TV (androidx.tv). Use this skill for Leanback to Compose migrations, including browse screen, settings screen, authentication screen, login screen, or video playback screen migrations, or when replacing BrowseSupportFragment, LeanbackSettingsFragment, PreferenceFragment, BaseLeanbackPreferenceFragmentCompat, VideoSupportFragment, GuidedStepSupportFragment, SearchSupportFragment, VerticalGridSupportFragment, Presenter, ArrayObjectAdapter, or CursorMapper with modern Compose equivalents, implementing immersive carousels with focus memory, Media3 video playback with PlayerSurface, or custom 10-foot hero layouts.

待分类7.7K13天前更新

Testing Setup

android

分析 Android 项目的测试配置,并安装测试框架、测试装置以及单元、UI、截图和端到端测试。

Software Development7.7K13天前更新

Android Profiler

android

指导 Android 性能分析:录制系统跟踪、堆转储和方法记录,并使用 Perfetto SQL 进行分析。

Software Development7.7K13天前更新