Flutter 2.0 推出后, 随即我就开始了升级, 如何升级 Flutter 请看这里, 在 iOS 上运行项目发现 pod install
会发生报错. 报错信息如下:
Generating Pods project
[!] An error occurred while processing the post-install hook of the Podfile.
undefined method `each_child' for #<Dir:0x00007fce67c598f0>
Did you mean? each_slice
/Users/linchuan/Applications/flutter/packages/flutter_tools/bin/podhelper.rb:57:in `block in flutter_additional_ios_build_settings'
/Users/linchuan/Applications/flutter/packages/flutter_tools/bin/podhelper.rb:54:in `each'
/Users/linchuan/Applications/flutter/packages/flutter_tools/bin/podhelper.rb:54:in `flutter_additional_ios_build_settings'
/Users/linchuan/Documents/Flutter/flutter_app_test/ios/Podfile:39:in `block (3 levels) in from_ruby'
/Users/linchuan/Documents/Flutter/flutter_app_test/ios/Podfile:38:in `each'
/Users/linchuan/Documents/Flutter/flutter_app_test/ios/Podfile:38:in `block (2 levels) in from_ruby'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-core-1.10.1/lib/cocoapods-core/podfile.rb:179:in `post_install!'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer.rb:897:in `run_podfile_post_install_hook'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer.rb:885:in `block in run_podfile_post_install_hooks'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/user_interface.rb:145:in `message'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer.rb:884:in `run_podfile_post_install_hooks'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer.rb:329:in `block (2 levels) in create_and_save_projects'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer/xcode/pods_project_generator/pods_project_writer.rb:61:in `write!'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer.rb:328:in `block in create_and_save_projects'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/user_interface.rb:64:in `section'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer.rb:307:in `create_and_save_projects'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer.rb:299:in `generate_pods_project'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer.rb:178:in `integrate'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer.rb:166:in `install!'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/command/install.rb:52:in `run'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/claide-1.0.2/lib/claide/command.rb:334:in `run'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/command.rb:52:in `run'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/bin/pod:55:in `<top (required)>'
/Users/linchuan/.rbenv/versions/2.5.1/bin/pod:23:in `load'
/Users/linchuan/.rbenv/versions/2.5.1/bin/pod:23:in `<main>'
我们仔细看一下这个报错信息.
undefined method `each_child' for #<Dir:0x00007fce67c598f0>
each_child
方法找不到, 这个方法的调用是在 flutter_tools/bin/podhelper.rb.
继续看报错信息
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-core-1.10.1/lib/cocoapods-core/podfile.rb:179:in `post_install!'
/Users/linchuan/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/cocoapods-1.10.1/lib/cocoapods/installer.rb:897:in `run_podfile_post_install_hook'
...
each_child
这个方法的实现是在 Ruby 中, 当前我的 Ruby 版本是 2.5.1, 通过查询 Ruby 的 版本更新记录 可以发现, 从 2.6 开始 each_child
才被添加.
所以我们必须将 Ruby 升级至2.6及之后的版本.
确保在 ~/.rbenv/versions/
这个目录下的 Ruby 版本是正确的. 如果你是在其他地方使用 Ruby, 则对应操作. 这里我用的是 rbenv 这个Ruby版本管理工具.
切换好新的Ruby版本后, 执行 pod install
之后还是不成功, 这里我们需要重装 cocoapods. 建议参考这个
由于我们是在 /usr/local/bin
此目录下安装 cocoapods , 以下查看 pod 位置
$ which pod
/usr/local/bin/pod
如果找不到 pod, 那么我们需要设置环境变量.
$ echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
如果你默认使用的 pod
指向下面这个的话, 需要按照上面的方法设置环境变量.
如果重装完 cocoapods , 运行项目依然报错, 提示Cocoapods安装异常.
其实这里并不是 Cocoapods 的问题, 我们需要
- 执行
sudo gem update
- 利用 Xcode 运行项目, 保证 Xcode 不报错.
- 重启 Android Studio, 利用AS运行项目.
此处借鉴于这里
参考
使用rbenv来管理Ruby版本的方法
使用rbenv 进行ruby 多版本的管理
pod setup失败解决方法
pod setup命令失败