前置 Mac下Flutter 安装与配置
本文是在已经下载flutter(上边有指导下载或者百度如何下载)并且本地flutter环境变量已经配置好的前提下进行操作
查看flutter环境变量是否配置完成,可以终端打开直接输入
flutter doctor -v
如果提示没有找到flutter,证明本地flutter环境变量还没有配置成功,
配置方法
- 打开终端输入
open .zshrc
- 因为我的flutter下载下来是放到桌面的,所以,应该这样输入
export PATH=/Users/yourname/Desktop/flutter/bin:$PATH
- 它看起来应该长这样
保存,然后退出
工程配置
- 桌面创建文件夹,命名为test
- 创建iOS/swift工程,命名为Flutter-iOSTest,将创建好的工程放入test文件夹下
- flutter module创建,打开终端,cd到test文件夹,终端输入
flutter create -t module my_flutter
成功之后你会看到和刚才创建的项目工程同级别下出现了flutter module 名称为咱们刚才写的 my_flutter
- 通过cocopod将iOS/swift工程和flutter进行关联
- 进入Flutter-iOSTest文件夹
- 执行 pod init
- 将配置项添加到pod文件夹中
flutter_application_path = '../my_flutter'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)
-
看起来应该是这样
保存,终端执行pod install
打开我们pod创建好的 Flutter-iOSTest.xcworkspace,进行基础配置
-
iOS工程Enable Bitcode 需要关闭,因为Flutter混合开发不支持Bitcode
添加Run Script
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed
执行到这里,command + B 编译,提示success 证明就接入成功了
如果pod install时出现下边的bug
- The 'Pods-ReciteWords' target has transitive dependencies that include statically linked binaries: (permission_handler)
解决方案:在pod中添加以下代码
pre_install do |installer|
# workaround for https://github.com/CocoaPods/CocoaPods/issues/3289
Pod::Installer::Xcode::TargetValidator.send(:define_method, :verify_no_static_framework_transitive_dependencies) {}
end
2019.11.5更新:官网最新提示如果您在2019年7月30日之前将Flutter添加到现有的iOS应用程序中,或者flutter --version低于Flutter 1.8.4-pre.21,将flutter工程与现有iOS工程关联你需要如下操作
1.Add the following lines to your Podfile:
flutter_application_path = 'path/to/my_flutter/'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
- For each Xcode target that needs to embed Flutter, call
install_all_flutter_pods(flutter_application_path)
.
target 'MyApp' do
install_all_flutter_pods(flutter_application_path)
end
target 'MyAppTests' do
install_all_flutter_pods(flutter_application_path)
end
3.如果您已经添加Run Script,需要修改如下,注释掉最后一个
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
#"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed
4.使用flutter1.9.1打包时,出现以下错误
1.Target file "lib/main.dart" not found
2./bin/sh: /packages/flutter_tools/bin/xcode_backend.sh: No such file or directory
3.Undefined symbols for architecture arm64
4.1找到的错误原因在于路径没有添加,需要手动添加TARGETS -> Build Settings -> User-Defined 下查看flutter路径是否添加,
参考文档