尝试在 Flutter 上使用 MMKV (1.2.16)的时候,编译报错,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
Launching lib/main.dart on iPod touch (7th generation) in debug mode... Updating minimum iOS deployment target to 11.0. Upgrading Podfile Running pod install... CocoaPods' output: ↳ [!] Invalid `Podfile` file: undefined method `exists?' for File:Class. # from /Users/xxxx/MMKV/flutter/example/ios/Podfile:35 # ------------------------------------------- # plugin_deps_file = File.expand_path(File.join(flutter_application_path, '..', '.flutter-plugins-dependencies')) > if not File.exists?(plugin_deps_file) # is_module = true; # ------------------------------------------- /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-core-1.12.0/lib/cocoapods-core/podfile.rb:335:in `rescue in block in from_ruby' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-core-1.12.0/lib/cocoapods-core/podfile.rb:329:in `block in from_ruby' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-core-1.12.0/lib/cocoapods-core/podfile.rb:50:in `instance_eval' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-core-1.12.0/lib/cocoapods-core/podfile.rb:50:in `initialize' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-core-1.12.0/lib/cocoapods-core/podfile.rb:327:in `new' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-core-1.12.0/lib/cocoapods-core/podfile.rb:327:in `from_ruby' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-core-1.12.0/lib/cocoapods-core/podfile.rb:293:in `from_file' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-1.12.0/lib/cocoapods/config.rb:205:in `podfile' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-1.12.0/lib/cocoapods/command.rb:160:in `verify_podfile_exists!' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-1.12.0/lib/cocoapods/command/install.rb:46:in `run' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/claide-1.1.0/lib/claide/command.rb:334:in `run' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-1.12.0/lib/cocoapods/command.rb:52:in `run' /usr/local/Cellar/cocoapods/1.12.0/libexec/gems/cocoapods-1.12.0/bin/pod:55:in `<top (required)>' /usr/local/Cellar/cocoapods/1.12.0/libexec/bin/pod:25:in `load' /usr/local/Cellar/cocoapods/1.12.0/libexec/bin/pod:25:in `<main>' Error running pod install Error launching application on iPod touch (7th generation). |
1 2 |
$ ruby -v ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.x86_64-darwin20] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$ brew install ruby ==> Downloading https://formulae.brew.sh/api/formula.jws.json ######################################################################## 100.0% ==> Downloading https://formulae.brew.sh/api/cask.jws.json ######################################################################## 100.0% Warning: ruby 3.2.1 is already installed and up-to-date. To reinstall 3.2.1, run: brew reinstall ruby $ ruby -v ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.x86_64-darwin20] $ brew link ruby Warning: Refusing to link macOS provided/shadowed software: ruby If you need to have ruby first in your PATH, run: echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc For compilers to find ruby you may need to set: export LDFLAGS="-L/usr/local/opt/ruby/lib" export CPPFLAGS="-I/usr/local/opt/ruby/include" For pkg-config to find ruby you may need to set: export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig" |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ flutter doctor Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.7.7, on macOS 11.7.4 20G1120 darwin-x64, locale zh-Hans-CN) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1) [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2022.1) [✓] IntelliJ IDEA Ultimate Edition (version 2018.3.6) [✓] VS Code (version 1.76.2) [✓] Connected device (3 available) [✓] HTTP Host Availability • No issues found! |
这个问题是由于 cocoapods 升级到 1.12 版本之后,依赖的 ruby 升级到 3.2 版本,其中的 File.exists 函数被替换成 File.exist,导致编译异常。
刚刚开始以为是 Flutter 的原因,结果发现 Flutter 3.7.7版本已经修复这个问题。
尝试了半天,才发现是 MMKV 的问题,主要是 MMKV的 iOS 工程下 flutter/example/ios/Podfile 跟 flutter/example/mmkvpodhelper.rb里面的代码需要进行适配。
1 2 3 4 5 6 7 8 9 10 |
# from ruby 3.2 File.exists is broken, we need compat function def mmkv_file_exists(file) is_exist = false if File.methods.include?(:exists?) is_exist = File.exists? file else is_exist = File.exist? file end return is_exist end |
使用上面的代码替换 File.exists 即可。
参考链接
- Invalid 'Podfile' file: undefined method 'exists?' for File:Class解决方案
- podhelper.rb uses deprecated, recently-removed exists? method #109385
- Invalid 'Podfile' file: undefined method `exists?' for File:Class
- Cocoapods: Fix deprecated/removed File.exists method #3919
- 【Ruby】怎样判断一个类是否有某个方法,一个实例是否有某个属性?
- Why do I get "undefined method 'exist' for File:Class"?