Flutter应用转化为模块(App to Module)
方法一:拷贝法(粗暴直接)
// 1. 创建一个新的module工程
$ flutter create --template module hello_module
// 2. 拷贝lib文件夹到hello_module中
// 3. 拷贝`pubspec.yaml`文件,注意保留底部的这几行
module:
androidX: true
androidPackage: com.example.hello_module
iosBundleIdentifier: com.example.helloModule
方法二:微调法(推荐)
- 修改
pubspec.yaml, 在最底部添加(注意缩进,左边有两个空格)。
module:
androidX: true
androidPackage: com.example.hello_module
iosBundleIdentifier: com.example.helloModule
- 修改
.metadata,将project_type: app改为project_type: module。
- 移除
ios和android文件夹。
- 清理并生成
.ios和.android。
$ flutter clean
$ flutter pub get
碰到的疑问
- 过程中一直纠结着转化为module时怎么运行工程的问题,后来反过来想想,实际单独生成module也可以运行,.ios和.android内部包含了一个demo工程。
- 方法2一定要移除ios和android文件夹,否则否则生成的.ios会缺少demo工程。
- 如果想将模块转化为应用呢?实践了下发现还是只能类似方案一的方式,因为没法自动生成ios和android文件夹。