cross-platform
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesecross-platform
跨平台开发
Purpose
用途
This skill guides the selection and implementation of cross-platform mobile frameworks like React Native with Expo, Flutter with Dart (using Bloc or Riverpod), Capacitor, and Kotlin Multiplatform Mobile (KMM). It focuses on choosing the right framework based on project needs and provides step-by-step integration for efficient mobile app development.
本技能指导如何选择和实现跨平台移动框架,包括搭配Expo的React Native、搭配Dart的Flutter(使用Bloc或Riverpod)、Capacitor以及Kotlin Multiplatform Mobile(KMM)。它聚焦于根据项目需求选择合适的框架,并提供分步集成方案,以实现高效的移动应用开发。
When to Use
适用场景
Use this skill for projects requiring multi-platform deployment (iOS/Android) to reduce code duplication. Apply it when building apps with shared business logic, such as e-commerce apps, social networks, or productivity tools. Ideal for teams with limited resources or when rapid prototyping is needed, like integrating native modules or handling state management across platforms.
当项目需要跨iOS/Android多平台部署以减少代码重复时,可使用本技能。适用于构建具备共享业务逻辑的应用,如电商应用、社交网络或生产力工具。尤其适合资源有限的团队,或需要快速原型开发的场景,比如集成原生模块或跨平台处理状态管理。
Key Capabilities
核心能力
- Framework selection: Choose React Native for JavaScript-heavy apps, Flutter for custom UI with Dart, Capacitor for web-to-mobile conversion, or KMM for shared Kotlin codebases.
- Expo integration: Use EAS (Expo Application Services) for streamlined builds, including OTA updates and cloud services.
- State management: Implement Bloc for event-driven state in Flutter or Riverpod for reactive state in Flutter/Dart apps.
- Cross-platform features: Handle platform-specific code, like accessing device APIs (e.g., camera, GPS) via Capacitor's bridge or KMM's expect/actual declarations.
- Configuration: Manage app configs in JSON/YAML formats, such as Expo's app.json for routing and permissions.
- 框架选型:JavaScript技术栈为主的项目选React Native;需要自定义UI的选Flutter;Web转移动应用选Capacitor;共享Kotlin代码库的选KMM。
- Expo集成:使用EAS(Expo Application Services)实现简化构建,包括OTA更新和云服务。
- 状态管理:在Flutter中实现基于事件驱动状态的Bloc,或基于响应式状态的Riverpod。
- 跨平台特性:处理平台专属代码,比如通过Capacitor的桥接层或KMM的expect/actual声明访问设备API(如相机、GPS)。
- 配置管理:使用JSON/YAML格式管理应用配置,例如Expo的app.json用于路由和权限设置。
Usage Patterns
使用模式
To select a framework, evaluate based on tech stack: use React Native if the team knows JS; Flutter for high-performance UIs. Start by initializing a project:
- For React Native with Expo: Run then add dependencies with
expo init MyApp --template blank.npm install @react-navigation/native - For Flutter with Riverpod: Create a project with and set up state:
flutter create MyAppin a Dart file. Always wrap platform-specific code: In KMM, usefinal counterProvider = StateProvider((ref) => 0);in common code and implement in iOS/Android modules. For Capacitor, convert a web app by runningexpect fun platformFunction() {}after setting up a web project.npx cap add android
选择框架时,根据技术栈评估:若团队熟悉JS则选React Native;需要高性能UI则选Flutter。从初始化项目开始:
- React Native搭配Expo:运行,然后通过
expo init MyApp --template blank添加依赖。npm install @react-navigation/native - Flutter搭配Riverpod:通过创建项目,在Dart文件中设置状态:
flutter create MyApp。 始终封装平台专属代码:在KMM中,在通用代码中使用final counterProvider = StateProvider((ref) => 0);,并在iOS/Android模块中实现。 对于Capacitor,在搭建Web项目后运行expect fun platformFunction() {}即可将其转换为移动应用。npx cap add android
Common Commands/API
常用命令/API
- Expo (React Native): Use (requires $EXPO_API_KEY env var),
expo loginfor multi-platform builds, and API endpointeas build --platform allfor project management.POST https://api.expo.dev/v2/projects - Flutter: Run for Bloc, or
flutter pub add flutter_blocto target a specific emulator; access APIs likeflutter run --device-id emulator-5554for state changes.BlocProvider.of(context).add(Event()) - Capacitor: Execute to update native projects, or
npx cap syncto launch Android Studio; use Capacitor's Plugins API, e.g.,npx cap open androidin JavaScript.Capacitor.Plugins.Camera.getPhoto() - KMM: Build with in the shared module, and use KMM Bridge for iOS:
./gradlew buildin Swift. Config formats: Edit Expo's app.json likeimport shared.Platform().hello(); for Flutter, modify pubspec.yaml with{ "expo": { "name": "MyApp", "slug": "myapp", "platforms": ["ios", "android"] } }.dependencies: flutter_riverpod: ^2.0.0
- Expo(React Native):使用(需$EXPO_API_KEY环境变量),
expo login用于多平台构建,项目管理API端点为eas build --platform all。POST https://api.expo.dev/v2/projects - Flutter:运行引入Bloc,或使用
flutter pub add flutter_bloc指定模拟器;通过flutter run --device-id emulator-5554等API处理状态变更。BlocProvider.of(context).add(Event()) - Capacitor:执行更新原生项目,或
npx cap sync启动Android Studio;使用Capacitor的Plugins API,例如在JavaScript中调用npx cap open android。Capacitor.Plugins.Camera.getPhoto() - KMM:在共享模块中运行进行构建,在iOS中使用KMM Bridge:
./gradlew build(Swift代码)。 配置格式:编辑Expo的app.json,示例:import shared.Platform().hello();对于Flutter,修改pubspec.yaml添加依赖:{ "expo": { "name": "MyApp", "slug": "myapp", "platforms": ["ios", "android"] } }。dependencies: flutter_riverpod: ^2.0.0
Integration Notes
集成说明
Integrate these frameworks with backends by setting environment variables for auth, e.g., export $BACKEND_API_KEY for API calls. For React Native, use Expo's Fetch API: Bearer ${process.env.BACKEND_API_KEY}. In Flutter, add HTTP packages: . For Capacitor, bridge web requests: Install and use . KMM integration: Share networking code via common module and call from native apps, ensuring Gradle sync for dependencies.
fetch('https://api.example.com/data', { headers: { Authorization: } })http.get(Uri.parse('https://api.example.com/data'), headers: {'Authorization': 'Bearer $envVar'}).then((response) => ...);@capacitor/coreCapacitorHttp.get({url: 'https://api.example.com/data', headers: {Authorization: $BACKEND_API_KEY}});将这些框架与后端集成时,设置身份验证相关的环境变量,例如导出$BACKEND_API_KEY用于API调用。在React Native中,使用Expo的Fetch API:Bearer ${process.env.BACKEND_API_KEY}。在Flutter中,添加HTTP包:。在Capacitor中,桥接Web请求:安装并使用。KMM集成:通过共享模块共享网络代码,并在原生应用中调用,确保Gradle同步依赖。
fetch('https://api.example.com/data', { headers: { Authorization: } })http.get(Uri.parse('https://api.example.com/data'), headers: {'Authorization': 'Bearer $envVar'}).then((response) => ...);@capacitor/coreCapacitorHttp.get({url: 'https://api.example.com/data', headers: {Authorization: $BACKEND_API_KEY}});Error Handling
错误处理
Handle common errors by checking for platform mismatches: In React Native, if fails with "Invalid platform", verify app.json platforms array. For Flutter, if errors on "No devices", use then retry. Capacitor errors like "Plugin not found" require and verifying package.json. In KMM, resolve build failures with for corrupted caches. Always log errors: In Expo, use and check EAS build logs; in Flutter, wrap with try-catch: . Use env vars for secure keys to avoid exposure.
expo buildflutter runflutter emulators --launch Pixel_API_33npx cap sync./gradlew cleanconsole.error(e)try { await http.get(...); } catch (e) { print(e.toString()); }通过检查平台匹配情况处理常见错误:在React Native中,若提示“Invalid platform”错误,需验证app.json中的platforms数组。在Flutter中,若提示“No devices”错误,使用启动模拟器后重试。Capacitor出现“Plugin not found”错误时,需执行并检查package.json。在KMM中,通过清理损坏的缓存以解决构建失败问题。始终记录错误:在Expo中,使用并查看EAS构建日志;在Flutter中,用try-catch包裹:。使用环境变量存储密钥以避免泄露。
expo buildflutter runflutter emulators --launch Pixel_API_33npx cap sync./gradlew cleanconsole.error(e)try { await http.get(...); } catch (e) { print(e.toString()); }Graph Relationships
关联关系
- Connected to: mobile cluster
- Relates to: react-native skill, flutter skill, expo skill, capacitor skill
- Depends on: cross-platform tags for embedding
- Interacts with: kmm framework in mobile ecosystem
- 关联集群:移动开发集群
- 相关技能:React Native技能、Flutter技能、Expo技能、Capacitor技能
- 依赖项:跨平台标签用于嵌入
- 交互对象:移动生态中的KMM框架