Pod install 未在 Flutter 应用程序中安装 Pod

Posted

技术标签:

【中文标题】Pod install 未在 Flutter 应用程序中安装 Pod【英文标题】:Pod install not installing pods in flutter app 【发布时间】:2020-11-13 15:14:35 【问题描述】:

我的 Flutter 应用在​​ android 模拟器上运行良好,但在尝试在 ios 模拟器上启动时,它会永远运行 pod install 命令。

于是我打开Xcode,发现这个错误:

沙盒与 Podfile.lock 不同步。运行“pod install”或 更新您的 CocoaPods 安装。

这是我的podfile

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', 
  'Debug' => :debug,
  'Profile' => :release,
  'Release' => :release,


def parse_KV_file(file, separator='=')
  file_abs_path = File.expand_path(file)
  if !File.exists? file_abs_path
    return [];
  end
  generated_key_values = 
  skip_line_start_symbols = ["#", "/"]
  File.foreach(file_abs_path) do |line|
    next if skip_line_start_symbols.any?  |symbol| line =~ /^\s*#symbol/ 
    plugin = line.split(pattern=separator)
    if plugin.length == 2
      podname = plugin[0].strip()
      path = plugin[1].strip()
      podpath = File.expand_path("#path", file_abs_path)
      generated_key_values[podname] = podpath
    else
      puts "Invalid plugin specification: #line"
    end
  end
  generated_key_values
end

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  # Flutter Pod

  copied_flutter_dir = File.join(__dir__, 'Flutter')
  copied_framework_path = File.join(copied_flutter_dir, 'Flutter.framework')
  copied_podspec_path = File.join(copied_flutter_dir, 'Flutter.podspec')
  unless File.exist?(copied_framework_path) && File.exist?(copied_podspec_path)
    # Copy Flutter.framework and Flutter.podspec to Flutter/ to have something to link against if the xcode backend script has not run yet.
    # That script will copy the correct debug/profile/release version of the framework based on the currently selected Xcode configuration.
    # CocoaPods will not embed the framework on pod install (before any build phases can generate) if the dylib does not exist.

    generated_xcode_build_settings_path = File.join(copied_flutter_dir, 'Generated.xcconfig')
    unless File.exist?(generated_xcode_build_settings_path)
      raise "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter pub get is executed first"
    end
    generated_xcode_build_settings = parse_KV_file(generated_xcode_build_settings_path)
    cached_framework_dir = generated_xcode_build_settings['FLUTTER_FRAMEWORK_DIR'];

    unless File.exist?(copied_framework_path)
      FileUtils.cp_r(File.join(cached_framework_dir, 'Flutter.framework'), copied_flutter_dir)
    end
    unless File.exist?(copied_podspec_path)
      FileUtils.cp(File.join(cached_framework_dir, 'Flutter.podspec'), copied_flutter_dir)
    end
  end

  # Keep pod path relative so it can be checked into Podfile.lock.
  pod 'Flutter', :path => 'Flutter'

  # Plugin Pods

  # Prepare symlinks folder. We use symlinks to avoid having Podfile.lock
  # referring to absolute paths on developers' machines.
  system('rm -rf .symlinks')
  system('mkdir -p .symlinks/plugins')
  plugin_pods = parse_KV_file('../.flutter-plugins')
  plugin_pods.each do |name, path|
    symlink = File.join('.symlinks', 'plugins', name)
    File.symlink(path, symlink)
    pod name, :path => File.join(symlink, 'ios')
  end
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['ENABLE_BITCODE'] = 'NO'
    end
  end
end

我一遍又一遍地运行 pod install 并尝试使用更新 cocoapods

gem install cocoapods

我该如何解决这个问题? 这真的让我烦恼了好几个星期。

【问题讨论】:

【参考方案1】:

如果您正在运行命令flutter run,该命令要求您在项目的根目录下,但是当您需要为 iOS 项目安装 cocoapods 时,您首先需要移动到 ios 文件夹,然后运行它。

cd ios/
pod install
cd ../
flutter run

或者您可以将所有这些都包含在一个命令中

cd ios/ && pod install && cd ../ && flutter run

【讨论】:

以上是关于Pod install 未在 Flutter 应用程序中安装 Pod的主要内容,如果未能解决你的问题,请参考以下文章

运行 pod install 时 Flutter iOS 构建失败

如何解决mac上flutter中的“error running pod install”?

Flutter 'Error running pod install' 'Pods-Runner' 目标具有传递依赖关系

Flutter小记7​Mac M1 使用 pod install编译常见两种错误解决方案(CocoaPod Error | LoadError)

Flutter小记7​Mac M1 使用 pod install编译常见两种错误解决方案(CocoaPod Error | LoadError)

Flutter小记7​Mac M1 使用 pod install编译常见两种错误解决方案(CocoaPod Error | LoadError)