Module 方式集成
1. 前置准备
在开始 Module 集成之前,请确保已完成以下准备工作:
- 已完成前述章节中的 SDK 接入快速接入
2. 项目结构
本节将以已集成 Conch 的 example_external_module 项目为示例,详细介绍如何在 iOS 和 Android 端以 Module 形式进行集成。
整体项目结构:
root/
├── example_external_module/ # Conch Module项目,已完成Conch集成配置
├── example_ios_module_host/ # iOS宿主应用示例项目
└── example_module_host_android/ # Android宿主应用示例项目
3. 配置 Conch Module 的 pubspec.yaml
在已完成 SDK 接入和 Conch 依赖配置的基础上,需要在 pubspec.yaml 中添加 Module 相关配置,使项目能够以 Module 形式被原生应用集成。
# 增加关键配置:标识这是一个 Flutter Module
flutter:
module:
androidX: true
androidPackage: com.example.example_external_module
iosBundleIdentifier: com.example.exampleExternalModule
配置说明:
androidX: true:启用 AndroidX 支持,确保与现代 Android 项目兼容androidPackage:指定 Android 平台的包名,需与宿主应用保持一致的命名规范iosBundleIdentifier:指定 iOS 平台的 Bundle 标识符
4. iOS 端集成
4.1 CocoaPods 方式集成
4.1.1 配置 iOS 宿主应用 Podfile
在 iOS 宿主项目的 Podfile 文件中添加 Flutter Module 的集成配置:
# Conch Module 项目相对路径(根据实际情况调整)
flutter_application_path = '../example_external_module'
# 加载 Flutter 的 podhelper
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'YourAppTargetName' do # 替换为你的 iOS 项目 Target 名称
# ... 其他 Pods ...
install_all_flutter_pods(flutter_application_path)
end
post_install do |installer|
flutter_post_install(installer) if defined?(flutter_post_install)
end
配置说明:
flutter_application_path:指定 Flutter Module 项目的相对路径,根据实际项目结构调整install_all_flutter_pods:自动安装 Flutter Module 的所有依赖flutter_post_install:执行 Flutter 特定的后安装配置
注意事项:
- 确保 Flutter Module 项目已正确配置
pubspec.yaml中的 Module 配置 - 如果 Flutter Module 使用了本地路径依赖,需要在 Podfile 中额外配置相关路径
- 首次集成时建议先执行
pod install验证配置是否正确
4.1.2 iOS 构建流程
# 1. 构建 Flutter Module 为 iOS Framework
cd example_external_module
flutter clean
flutter pub get
flutter build ios-framework --no-color
# 2. 安装 iOS 应用依赖
cd ../example_ios_module_host
pod install
# 3. 构建 iOS 应用
5. Android 端集成
5.1 仓库配置说明
Flutter Android 项目通常会从 Google Maven 仓库 (google()) 和 Maven Central (mavenCentral()) 获取其依赖。
为了确保项目能够正确构建并使用 Conch 的热修能力,我们需要在项目的 build.gradle 文件中配置额外的本地 Conch 引擎仓库以及添加 Shiply 仓库。
5.1.1 配置 Android 宿主应用 settings.gradle.kt
在 Android 宿主项目的 settings.gradle.kts 文件中添加必要的仓库配置:
特别注意:需要添加 Conch 引擎仓库和 Shiply 仓库,否则可能无法获取 Conch 相关依赖。
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
// 添加 Flutter AAR 本地仓库路径
maven { url = uri("${rootDir}/../example_external_module/build/host/outputs/repo") }
// 添加 Conch 引擎仓库
// 历史版本:通过 zip 包分发的 ConchSDK,需配置本地路径依赖(请替换为实际路径)
// maven { url = uri("{YOUR_PATH}/conch-flutter/bin/conch/maven") }
// 新版本(Conch 1.7.0 版本后):通过 Git 仓库分发的 Conch-Flutter SDK,支持 Maven 制品库(重点注意)
maven { url = uri("https://maven.cnb.cool/tencent-tds/conch-release-maven/-/packages/") }
// 添加 Shiply 仓库(重点注意)
maven { url = uri("https://maven.cnb.cool/tencent-tds/shiply-public/-/packages/") }
}
}
5.1.2 配置 app/build.gradle.kts
在应用模块的 build.gradle.kts 文件中添加 Flutter AAR 依赖:
dependencies {
// 添加 Flutter AAR 依赖
implementation("com.example.example_external_module:flutter_release:1.0")
// 其他现有依赖
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
// ...
}
5.1.3 Android 构建流程
# 1. 构建 Flutter Module 为 AAR
cd example_external_module
flutter clean
flutter pub get
flutter build --no-color aar
# 2. 构建 Android 应用
cd ../example_module_host_android
./gradlew assembleRelease