MMKV编译报错Invalid `Podfile` file: undefined method `exists?' for File:Class.

尝试在 Flutter 上使用 MMKV1.2.16)的时候,编译报错,如下:

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).
$ ruby -v
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.x86_64-darwin20]
$ 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"
$ 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 的问题,主要是 MMKViOS 工程下 flutter/example/ios/Podfileflutter/example/mmkvpodhelper.rb里面的代码需要进行适配。

# 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 即可。

参考链接