一、背景
目前准备试水 Flutter,但是多数 native 开发是不了解 Flutter,因此需要设计一种比较“舒服”的集成方式。
二、混编方案
2.1 方案考量
基于以上两点思考,针对 Android iOS 有如下方案:
2.2 Android
先看下官方的集成方式:
# setting.gradle
setBinding(new Binding([gradle: this]))
evaluate(new File(
'../managementcenter/.android/include_flutter.groovy'
))
# build.gradle
dependencies {
implementation project(':flutter')
...........
}
复制代码
这种方式使得工程强耦合,虽然便于开发调试,但是违背了第一点,大多数 native 同学都需要配置 Flutter 环境, 成本很大。
2.3 iOS
2.3.1 官方 iOS 混编方案简介
在 native 项目 Podfile
中通过 eval binding 特性注入 podhelper.rb
脚本,在 pod install/update
时执行此脚本,脚本主要处理:
Pod 本地依赖 Flutter 引擎(Flutter.framework) 与 Flutter 插件注册表(FlutterPluginRegistrant)
Flutter 插件通过 flutter packagesget
指令安装后生成的 .flutter-plugins
文件解析,然后 Pod 本地依赖所有的插件
在 pod install 执行完的钩子 post_install
中,获取当前 pod target 工程对象,导入 Generated.xcconfig
配置,其中都为环境变量的配置,主要为后续的 xcode_backend.sh
脚本执行做准备
在构建阶段 BuildPhases
中注入构建是需要执行的 xcode_backend.sh
脚本,脚本主要完成 Flutter 产物的构建并将其添加到对应的 native 工程中去,后续会进一步介绍此脚本
2.3.2 优点
2.3.3 缺点
2.4 小结
基于以上思考,同时考虑到某个 Flutter 业务模块可能会引入到不同的 App 中,同时考虑到某个业务实现方式方面的解耦(某个业务可能用 native, flutter, weex 开发),有以下方案(中间产物库每个 Flutter 业务模块都是独立的):
Android:
iOS:
三、Flutter 产物结构
3.1 Android
3.2 iOS
关于编译模式了解更多可参考查看 Flutter 的编译模式。
四、Flutter 产物收集
4.1 Android
在 Android 端集成 Flutter 较为简单,只需要获取到上文所讲的 Flutter 产物即 aar 文件。但是由于插件文件散落每次获取比较麻烦所以目前简单用脚本收集。
脚本收集主要是依靠项目里 .flutter_plugins 文件,该文件会记录 flutter 项目中引用的插件名以及本地路径等,因此可以通过该路径抓取插件的 aar 文件。
from shutil import copyfile
import os
import requests
# 抓取文件类型
BuildRelease = True
aarType = "-release.aar" if BuildRelease else "-debug.aar"
pluginFilePath = '../.flutter-plugins'
# 当前项目的flutter.aar
currentFlutterPath = '../.android/Flutter/build/outputs/aar/'
# 输出地址
outputFilePath = os.path.abspath('flutter_aar.py').replace("flutter_aar.py", "aars/")
endPath = 'android/build/outputs/aar/'
def collect_aar(plugins):
all_collection_success = True
if os.path.exists(outputFilePath):
print('copy aar to: ' + outputFilePath)
else:
print('target path: ' + outputFilePath + ' not exist')
os.makedirs(outputFilePath)
print('create target path: ' + outputFilePath)
for key, value in plugins.items():
aar_path = value + key + aarType
try:
copyfile(aar_path, outputFilePath + key + aarType)
print('copy flutter aar success at path: ' + aar_path)
except IOError:
all_collection_success = False
print('copy flutter aar error at path: ' + aar_path)
pass
file_object = open(pluginFilePath, 'r')
try:
plugin_map = {}
for line in file_object:
array = line.split('=')
plugin_map[array[0]] = array[1].replace('\n', '') + endPath
plugin_map['flutter'] = currentFlutterPath
collect_aar(plugin_map)
finally:
file_object.close()
复制代码
目前该 python 脚本只抓取 Release 的 aar 文件,如果需要获取 debug 的可以手动修改:
执行抓取脚本 ./flutter_aar.sh
#!/usr/bin/env bash
cd ..
cd .android
echo "start clean"
./gradlew clean
echo "start assembleRelease"
./gradlew assembleRelease
cd ..
cd android-build
echo "clean old aar file"
rm -rf aars
echo "start copy aar file"
# 只抓取release
python flutter_aar.py
echo "copy aar file finish"
复制代码
脚本执行完 Flutter 产物 aar 文件统一生成在根目录下 android-build 文件夹中。
4.2 iOS
通过查看 Flutter 编译脚本 xcode_backend.sh
和测试单独引入编译产物,发现其实 只要拥有 Flutter 的编译产物,宿主项目就可以接入 Flutter 的功能。
4.2.1 脚本简单分析
if [[ -e "${project_path}/.ios" ]]; then
RunCommand rm -rf -- "${derived_dir}/engine"
mkdir "${derived_dir}/engine"
RunCommand cp -r -- "${flutter_podspec}" "${derived_dir}/engine"
RunCommand cp -r -- "${flutter_framework}" "${derived_dir}/engine"
RunCommand find "${derived_dir}/engine/Flutter.framework" -type f -exec chmod a-w "{}" ;
else
RunCommand rm -rf -- "${derived_dir}/Flutter.framework"
RunCommand cp -r -- "${flutter_framework}" "${derived_dir}"
RunCommand find "${derived_dir}/Flutter.framework" -type f -exec chmod a-w "{}" ;
fi
复制代码
RunCommand eval "$(echo "static const int Moo = 88;" | xcrun clang -x c
${arch_flags}
-dynamiclib
-Xlinker -rpath -Xlinker '@executable_path/Frameworks'
-Xlinker -rpath -Xlinker '@loader_path/Frameworks'
-install_name '@rpath/App.framework/App'
-o "${derived_dir}/App.framework/App" -)"
复制代码
RunCommand "${FLUTTER_ROOT}/bin/flutter" --suppress-analytics
${verbose_flag}
build aot
--output-dir="${build_dir}/aot"
--target-platform=ios
--target="${target_path}"
--${build_mode}
--ios-arch="${archs}"
${local_engine_flag}
${track_widget_creation_flag}
复制代码
StreamOutput " ├─Assembling Flutter resources..."
RunCommand "${FLUTTER_ROOT}/bin/flutter" --suppress-analytics
${verbose_flag}
build bundle
--target-platform=ios
--target="${target_path}"
--${build_mode}
--depfile="${build_dir}/snapshot_blob.bin.d"
--asset-dir="${derived_dir}/App.framework/flutter_assets"
${precompilation_flag}
${local_engine_flag}
${track_widget_creation_flag}
复制代码
4.2.2 方案分析
设计
脚本编写
echo "==b清理flutter历史编译==="
flutter clean
echo "===重新生成plugin索引==="
flutter packages get
echo "===生成App.framework和flutter_assets==="
flutter build ios --debug
echo "===获取所有plugin并找到头文件==="
while read -r line
do
if [[ ! "$line" =~ ^// ]]; then
array=(${line//=/ })
plugin_name=${array[0]}
cd .ios/Pods
echo "生成lib${plugin_name}.a..."
/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target ${plugin_name} BUILD_DIR=../../build/ios -sdk iphoneos -quiet
/usr/bin/env xcrun xcodebuild build -configuration Debug ARCHS='x86_64' -target ${plugin_name} BUILD_DIR=../../build/ios -sdk iphonesimulator -quiet
echo "合并lib${plugin_name}.a..."
lipo -create "../../build/ios/Debug-iphonesimulator/${plugin_name}/lib${plugin_name}.a" "../../build/ios/Release-iphoneos/${plugin_name}/lib${plugin_name}.a" -o "../../product/lib${plugin_name}.a"
echo "复制头文件"
classes=${array[1]}ios/Classes
for header in `find "$classes" -name *.h`; do
cp -f $header "../../product/"
done
else
echo "读取文件出错"
fi
done < .flutter-plugins
echo "===生成注册入口的二进制库文件==="
for reg_enter_name in "FlutterPluginRegistrant"
do
echo "生成libFlutterPluginRegistrant.a..."
/usr/bin/env xcrun xcodebuild build -configuration Release ARCHS='arm64 armv7' -target FlutterPluginRegistrant BUILD_DIR=../../build/ios -sdk iphoneos
/usr/bin/env xcrun xcodebuild build -configuration Debug ARCHS='x86_64' -target FlutterPluginRegistrant BUILD_DIR=../../build/ios -sdk iphonesimulator
echo "合并libFlutterPluginRegistrant.a..."
lipo -create "../../build/ios/Debug-iphonesimulator/FlutterPluginRegistrant/lib$FlutterPluginRegistrant.a" "../../build/ios/Release-iphoneos/FlutterPluginRegistrant/libFlutterPluginRegistrant.a" -o "../../product/libFlutterPluginRegistrant.a"
echo "复制头文件"
classes="../Flutter/FlutterPluginRegistrant/Classes"
for header in `find "$classes" -name *.h`; do
cp -f $header "../../product/"
done
done
复制代码
五、Flutter 产物上传
5.1 Android
上面产物搜集完成后,需要上传 maven 仓库,方便集成以及版本控制:
apply plugin: 'youzan.maven.upload'
zanMavenUpload {
version = '0.0.2'
childGroup = "flutter-apub"
}
uploadItems {
"fluttertoast" {
targetFile = file('../../android-build/aars/fluttertoast-release.aar')
}
"image_picker" {
targetFile = file('../../android-build/aars/image_picker-release.aar')
}
............
}
复制代码
因此引用链如下:
六、总结
以上比较全面的描述了有赞的 Flutter 混编方案,目前有赞已经在内部使用的 App 上使用 Flutter 开发了一些页面作为试点。后续会考虑在线上 App 试点,目前正在进行 Flutter 基础库的搭建,之后会专门有文章分享。
本文转载自公众号有赞 coder(ID:youzan_coder)。
原文链接:
https://mp.weixin.qq.com/s?__biz=MzAxOTY5MDMxNA==&mid=2455759923&idx=1&sn=030ef038363723928b74176ec67fb2d1&chksm=8c686a16bb1fe30094cd3e79f56f70dcb0a9bcc146822bca67acf9b14687ffaef6921b64d587&scene=27#wechat_redirect
评论