最近在集成flutter进项目
以收集编译产物并以cocopods方式集成
产物大概放入两个pod库
这个暂且叫pod1,pod1放flutter.framework,第三方plugin.a,自己写的基础plugin,比如networking,hud等等(由于是混编,不可避免存在很多原生基础组件,所以能公用的基础组件都会弄一个flutter-plugin桥接)
第二个pod2放编译之后的app.framwork,注册文件GeneratedPluginRegistrant,以及各个业务模块.a(有可能没有)结构大概如下面
podspec大概如下
s.source_files = 'GDFlutter/Classes/**/*' , 'flutter_module/product/libFlutterPluginRegistrant/*.{h,m}' , 'flutter_module/product/libGDFlutterBusiness*/*.{h,m}'
arr = Array.new
arr.push('flutter_module/product/**/App.framework')
s.ios.vendored_frameworks = arr
arr1 = Array.new
arr1.push('flutter_module/product/libGDFlutterBusiness*/*.a')
s.ios.vendored_libraries = arr1
主工程引入这两个pod库即可
接下来从零开始搭建上文所说的
flutter create flutter_module
先创建一个flutter module
这个是主flutter工程,用来集成businessModule以及生成app.framework
结构如下图
再生成一个业务工程
flutter create --template=plugin GDFlutterBusinessAboutGD
注意 我规定业务模块必须以GDFlutterBusiness开头 不然上文提到的pod2无法收集产物
注意此时还需要进入example生成ios和Android工程,不然无法单独编译运行
cd example
flutter create .
这样 这个单独的业务模块就可以单独跑起来了
此时主flutter工程和业务工程均搭建完毕
在主工程pubspec.ymal文件讲两个工程关联
好了 接下来就是编写脚本收集产物了
if [ ! -d "./product" ];then
mkdir ./product
else
echo -e " 文件夹已经存在 "
rm -rf ./product/*
fi
config='Debug'
appConfig="$(echo $config | tr '[:upper:]' '[:lower:]')"
#
#echo -e " ===清理Flutter历史编译=== "
##flutter clean
#
echo -e " ===重新生成plugin索引=== "
flutter packages get
#
# 删除原生成的App.framework
rm -rf ./ios/Flutter/App.framework
echo -e " ===生成App.framework=== "
echo -e " flutter build ios --${appConfig} "
flutter build ios --${appConfig}
# 创建放置产物的目录
if [ ! -d "./product/app" ];then
mkdir ./product/app
fi
# 收集App.framework,生成podspec
cp -fr ./.ios/Flutter/App.framework ./product/app
cp -f $FLUTTER_ROOT/App.podspec ./product/app
#
## 收集Flutter.framework与podspec
echo -e " ===拷贝Flutter.framework=== "
cp -fr ${FLUTTER_ROOT}/bin/cache/artifacts/engine/ios/Flutter.framework ./product/engine
cp -f ${FLUTTER_ROOT}/bin/cache/artifacts/engine/ios/Flutter.podspec ./product/engine
if [[ ! -d "./product/libFlutterPluginRegistrant" ]]; then
mkdir "./product/libFlutterPluginRegistrant"
fi
cp -rf ./ios/Runner/GeneratedPluginRegistrant.h "./product/libFlutterPluginRegistrant/"
cp -rf ./ios/Runner/GeneratedPluginRegistrant.m "./product/libFlutterPluginRegistrant/"
if [[ "$config" =~ "Release" ]];then
lipo -remove "x86_64" "./product/app/App.framework" -output "./product/app/App.framework"
lipo -remove "x86_64" "./product/engine/Flutter.framework" -output "./product/engine/Flutter.framework"
fi
echo -e " ===生成plugin的静态库=== "
while read -r line
do
if [[ ! "$line" =~ ^// ]]; then
array=(${line//=/ })
plugin_name=${array[0]}
echo -e " ===修正注册文件=== "
perl -pi -e "s|\<${plugin_name}\/|\"|g" ../../product/libFlutterPluginRegistrant/GeneratedPluginRegistrant.m
perl -pi -e "s|.h\>|.h\"|g" ../../product/libFlutterPluginRegistrant/GeneratedPluginRegistrant.m
cd ios/Pods
/usr/bin/env xcrun xcodebuild build -configuration ${config} ARCHS='arm64 armv7' -target ${plugin_name} BUILD_DIR=../../build/ios -sdk iphoneos -quiet
/usr/bin/env xcrun xcodebuild build -configuration ${config} ARCHS='x86_64' -target ${plugin_name} BUILD_DIR=../../build/ios -sdk iphonesimulator -quiet
echo -e " 合并lib${plugin_name}.a... "
if [[ ! -d "../../product/lib${plugin_name}" ]]; then
mkdir "../../product/lib${plugin_name}"
fi
lipo -create "../../build/ios/${config}-iphonesimulator/${plugin_name}/lib${plugin_name}.a" "../../build/ios/${config}-iphoneos/${plugin_name}/lib${plugin_name}.a" -o "../../product/lib${plugin_name}/lib${plugin_name}.a"
if [[ "$config" =~ "Release" ]];then
lipo -remove "x86_64" "../../product/lib${plugin_name}/lib${plugin_name}.a" -output "../../product/lib${plugin_name}/lib${plugin_name}.a"
fi
echo -e " 复制头文件 "
classes=${array[1]}ios/Classes
for header in `find "$classes" -name *.h`; do
cp -f $header "../../product/lib${plugin_name}/"
done
echo -e " 复制podspec文件 "
specDir=${array[1]}ios
for spec in `find "$specDir" -name *.podspec`; do
cp -f $spec "../../product/lib${plugin_name}/"
done
else
echo -e " 读取文件出错 "
fi
done < .flutter-plugins
echo -e " 编译完成 "
编译完成之后会在flutter主工程product生成如下文件
将上面文件分类收集做成文章开头的pod1,pod2 ,在native工程引入即可
在集成flutter的过程中 踩不少坑 也阅读很多前辈的文章,在此一并感谢