【问题标题】:iOS build failed after adding Firebase to flutter app将 Firebase 添加到 Flutter 应用后,iOS 构建失败
【发布时间】:2021-02-26 08:24:35
【问题描述】:

我昨天添加了 Firebase,在 Android 上它可以正常运行。当我尝试运行 iOS 应用程序时,会出现以下错误消息:

Downloading ios tools...
Downloading ios-profile tools...
Downloading ios-release tools...
Running "flutter pub get" in ripped...
Launching lib/main.dart on iPhone 11 Pro in debug mode...
Running Xcode build...
Xcode build done.                                           21.9s
Could not build the application for the simulator.
Error launching application on iPhone 11 Pro.
Failed to build iOS app
Error output from Xcode build:
↳
    ** BUILD FAILED **


Xcode's output:
↳
    /Users/michi/Coding/flutter/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.16.2/darwin/Classes/AudioplayersPlugin.m:89:37: warning: incompatible pointer types sending 'FlutterEngine *' to parameter of type 'NSObject<FlutterBinaryMessenger> * _Nonnull' [-Wincompatible-pointer-types]
                        binaryMessenger:_headlessEngine];
                                        ^~~~~~~~~~~~~~~
    In module 'Flutter' imported from /Users/michi/Coding/flutter/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.16.2/darwin/Classes/AudioplayersPlugin.h:2:
    /Users/michi/AndroidStudioProjects/ripped/ios/Flutter/Flutter.framework/Headers/FlutterChannels.h:178:74: note: passing argument to parameter 'messenger' here
                          binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger;
                                                                             ^
    /Users/michi/Coding/flutter/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.16.2/darwin/Classes/AudioplayersPlugin.m:482:74: warning: 'initWithImage:' is deprecated: first deprecated in iOS 10.0 - Use -initWithBoundsSize:requestHandler: [-Wdeprecated-declarations]
                  MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: artworkImage];
                                                                             ^
    In module 'MediaPlayer' imported from /Users/michi/Coding/flutter/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.16.2/darwin/Classes/AudioplayersPlugin.m:7:
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.0.sdk/System/Library/Frameworks/MediaPlayer.framework/Headers/MPMediaItem.h:240:1: note: 'initWithImage:' has been explicitly marked deprecated here
    - (instancetype)initWithImage:(UIImage *)image MP_DEPRECATED("Use -initWithBoundsSize:requestHandler:", ios(5.0, 10.0));
    ^
    2 warnings generated.
    /Users/michi/Coding/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_analytics-6.2.0/ios/Classes/FLTFirebaseAnalyticsPlugin.m:49:19: warning: 'setScreenName:screenClass:' is deprecated: Use +[FIRAnalytics logEventWithName:kFIREventScreenView parameters:] instead. [-Wdeprecated-declarations]
        [FIRAnalytics setScreenName:screenName screenClass:screenClassOverride];
                      ^
    In module 'FirebaseAnalytics' imported from /Users/michi/AndroidStudioProjects/ripped/ios/Pods/Headers/Public/Firebase/Firebase.h:22:
    /Users/michi/AndroidStudioProjects/ripped/ios/Pods/FirebaseAnalytics/Frameworks/FirebaseAnalytics.framework/Headers/FIRAnalytics.h:115:5: note: 'setScreenName:screenClass:' has been explicitly marked deprecated here
        DEPRECATED_MSG_ATTRIBUTE(
        ^
    In module 'UIKit' imported from /Users/michi/AndroidStudioProjects/ripped/ios/Pods/Target Support Files/firebase_analytics/firebase_analytics-prefix.pch:2:
    In module 'Foundation' imported from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:
    In module 'CoreFoundation' imported from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:6:
    In module 'Darwin' imported from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.0.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:16:
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.0.sdk/usr/include/AvailabilityMacros.h:181:64: note: expanded from macro 'DEPRECATED_MSG_ATTRIBUTE'
                #define DEPRECATED_MSG_ATTRIBUTE(s) __attribute__((deprecated(s)))
                                                                   ^
    1 warning generated.
    /Users/michi/Library/Developer/Xcode/DerivedData/Runner-ctrarkqgeeshcpdfedxiktkemfwl/Build/Intermediates.noindex/Runner.build/Debug-iphonesimulator/Runner.build/Script-7CA6D22A4148A35EEC636DD2.sh: line 2: /Users/michi/AndroidStudioProjects/ripped/ios/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh: Permission denied
    Command PhaseScriptExecution failed with a nonzero exit code
    note: Using new build system
    note: Building targets in parallel
    note: Planning build
    note: Constructing build description

我的 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 flutter_root
  generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
  unless File.exist?(generated_xcode_build_settings_path)
    raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
  end

  File.foreach(generated_xcode_build_settings_path) do |line|
    matches = line.match(/FLUTTER_ROOT\=(.*)/)
    return matches[1].strip if matches
  end
  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
  end
end

我的 pubspec.yaml 有以下依赖项:

dependencies:
  firebase_core: ^0.5.2
  firebase_analytics: ^6.2.0
  cloud_firestore: ^0.14.3
  firebase_auth: ^0.18.3
  percent_indicator: ^2.1.8
  numberpicker: ^1.2.1
  email_validator: ^1.0.6
  simple_animations: ^2.2.3
  audioplayers: ^0.16.1
  expandable: ^4.1.4
  smooth_page_indicator: ^0.2.0
  auto_size_text: ^2.1.0
  tab_indicator_styler: ^1.0.0
  flutter_gradient_colors: ^1.0.0
  font_awesome_flutter: ^8.8.1
  flutter_svg:
  flutter:
    sdk: flutter

我的 AppDelegate.swift 看起来像这样

import UIKit
import Flutter
import Firebase

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    FirebaseApp.configure()
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

5 个小时以来,我一直在尝试不同的解决方案。 我已经尝试过的:

  • 确保 GoogleService-Info.plist 文件是通过 Xcode 添加的

  • 删除 Podfile 和 Podfile.lock

  • 在 Podfile 中取消注释 platform :ios, '9.0',也尝试使用 10.0 版 & 12.0

  • 手动将 pod 添加到 Podfile:

    pod 'Firebase/Analytics'

    pod 'Firebase/Core'

    pod 'Firebase/Auth'

    pod 'Firebase/Firestore'

它实际上在某个时候构建成功,但应用程序在启动时立即崩溃,所以我用flutter create完全重建了ios文件夹,不知道该做什么了。

更新

现在将 podfile 更改为:

 # Uncomment this line to define a global platform for your project
    platform :ios, '12.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 flutter_root
      generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
      unless File.exist?(generated_xcode_build_settings_path)
        raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
      end
    
      File.foreach(generated_xcode_build_settings_path) do |line|
        matches = line.match(/FLUTTER_ROOT\=(.*)/)
        return matches[1].strip if matches
      end
      raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
    end
    
    require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
    
    flutter_ios_podfile_setup
    
    target 'Runner' do
    
    # add pods for any other desired Firebase products
    # https://firebase.google.com/docs/ios/setup#available-pods
      pod 'Firebase/Analytics'
      pod 'Firebase/Core'
      pod 'Firebase/Auth'
      pod 'Firebase/Firestore'
    
      use_frameworks!
      use_modular_headers!
    
      flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
    end
    
    post_install do |installer|
      installer.pods_project.targets.each do |target|
        flutter_additional_ios_build_settings(target)
      end
    end

解决了构建错误,但当我想启动它时应用程序仍然立即崩溃

【问题讨论】:

    标签: ios swift firebase flutter build-error


    【解决方案1】:

    感谢How to fix iOS crash during the start Firebase configure in Flutter?,我终于找到了问题所在,我只需将FirebaseApp.configure() 移动一行,使其位于GeneratedPluginRegistrant.register(with: self) 上方,因此AppDelegate.swift 文件不是这样的:

    import UIKit
    import Flutter
    import Firebase
    
    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
      override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
      ) -> Bool {
        FirebaseApp.configure()
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-30
      • 2020-03-16
      • 1970-01-01
      • 2021-01-21
      • 2020-12-28
      • 2020-08-18
      • 2021-09-13
      • 2020-09-15
      • 2020-07-25
      相关资源
      最近更新 更多