Flutter 项目无法在 Mac M1 上构建

Posted

技术标签:

【中文标题】Flutter 项目无法在 Mac M1 上构建【英文标题】:Flutter project failing to build on Mac M1 【发布时间】:2021-06-10 03:46:57 【问题描述】:

我最近购买了带有M1 芯片的新 Mac 书,在构建时遇到了 Flutter 和 Firebase 之间的巨大问题。我会同时列出 Flutter 错误日志和 Xcode 错误日志。

注意:我正在 Flutter 频道 Master

上构建

Flutter 错误日志:

    Launching lib/main.dart on iPod touch (7th generation) in debug mode...
    Xcode build done.                                           69.1s
    Failed to build ios app

Xcode 构建的错误输出:

objc[30795]: Class AMSupportURLConnectionDelegate is implemented in both ?? (0x1fe91f8f0) and ?? (0x1189402b8). One of the two will be used. Which one is undefined.
objc[30795]: Class AMSupportURLSession is implemented in both ?? (0x1fe91f940) and ?? (0x118940308). One of the two will be used. Which one is undefined.
** BUILD FAILED **

Xcode 的输出:

/Users/wissamboughannam/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7+1/ios/Classes/FLTImagePickerPlugin.m:141:20: warning: 'UIAlertView' is deprecated: first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead [-Wdeprecated-declarations]
    [[[UIAlertView alloc] initWithTitle:@"Error"
                   ^
In module 'UIKit' imported from /Volumes/Sub/Flutter-Projects/test-project/test_project_app_v2/test_app_2/ios/Pods/Target Support Files/image_picker/image_picker-prefix.pch:2:
/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.5.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIAlertView.h:27:12: note: 'UIAlertView' has been explicitly marked deprecated here
@interface UIAlertView : UIView
           ^
1 warning generated.
/Users/wissamboughannam/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7+1/ios/Classes/FLTImagePickerPhotoAssetUtil.m:17:44: warning: 'UIImagePickerControllerReferenceURL' is deprecated: first deprecated in iOS 11.0 - Will be removed in a future release, use phpicker. [-Wdeprecated-declarations]
  NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
                                           ^
In module 'UIKit' imported from /Volumes/Sub/Flutter-Projects/test-project/test_project_app_v2/test_app_2/ios/Pods/Target Support Files/image_picker/image_picker-prefix.pch:2:
/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.5.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIImagePickerController.h:62:51: note: 'UIImagePickerControllerReferenceURL' has been explicitly marked deprecated here
UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerReferenceURL API_DEPRECATED("Will be removed in a future release, use PHPicker.", ios(4.1, 11.0)) API_UNAVAILABLE(tvos); // an NSURL that references an asset in the AssetsLibrary framework
                                                  ^
/Users/wissamboughannam/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7+1/ios/Classes/FLTImagePickerPhotoAssetUtil.m:21:47: warning: 'fetchAssetsWithALAssetURLs:options:' is deprecated: first deprecated in iOS 11 - Will be removed in a future release [-Wdeprecated-declarations]
  PHFetchResult<PHAsset *> *result = [PHAsset fetchAssetsWithALAssetURLs:@[ referenceURL ]
                                              ^
In module 'Photos' imported from /Users/wissamboughannam/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7+1/ios/Classes/FLTImagePickerPhotoAssetUtil.h:6:
/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.5.sdk/System/Library/Frameworks/Photos.framework/Headers/PHAsset.h:74:1: note: 'fetchAssetsWithALAssetURLs:options:' has been explicitly marked deprecated here
+ (PHFetchResult<PHAsset *> *)fetchAssetsWithALAssetURLs:(NSArray<NSURL *> *)assetURLs options:(nullable PHFetchOptions *)options API_DEPRECATED("Will be removed in a future release", ios(8, 11), tvos(8, 11)) API_UNAVAILABLE(macos);
^
2 warnings generated.
1 warning generated.
error: the following command failed with exit code 0 but produced no further output
CompileC /Users/wissamboughannam/Library/Developer/Xcode/DerivedData/Runner-awkucyxpawezvvfjchbivjxuwsah/Build/Intermediates.noindex/Pods.build/Debug-iphonesimulator/file_picker.build/Objects-normal/x86_64/FilePickerPlugin.o /Users/wissamboughannam/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/file_picker-1.4.2/ios/Classes/FilePickerPlugin.m normal x86_64 objective-c com.apple.compilers.llvm.clang.1_0.compiler
/Volumes/Sub/Flutter-Projects/test-project/test_project_app_v2/test_app_2/ios/Pods/FirebaseCore/FirebaseCore/Sources/FIRLogger.m:58:28: warning: this old-style function definition is not preceded by a prototype [-Wstrict-prototypes]
void FIRLoggerInitializeASL() 
                           ^
/Volumes/Sub/Flutter-Projects/test-project/test_project_app_v2/test_app_2/ios/Pods/FirebaseCore/FirebaseCore/Sources/FIRLogger.m:101:20: warning: this old-style function definition is not preceded by a prototype [-Wstrict-prototypes]
void FIRResetLogger() 
                   ^
2 warnings generated.

/Users/wissamboughannam/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/provider-3.2.0/lib/src/provider.dart:259:19: Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/Users/wissamboughannam/Developer/flutter/packages/flutter/lib/src/widgets/framework.dart').
Try correcting the name to the name of an existing method, or defining a method named 'inheritFromWidgetOfExactType'.
        ? context.inheritFromWidgetOfExactType(type) as InheritedProvider<T>
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/Users/wissamboughannam/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/provider-3.2.0/lib/src/provider.dart:260:19: Error: The method 'ancestorInheritedElementForWidgetOfExactType' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/Users/wissamboughannam/Developer/flutter/packages/flutter/lib/src/widgets/framework.dart').
Try correcting the name to the name of an existing method, or defining a method named 'ancestorInheritedElementForWidgetOfExactType'.
        : context.ancestorInheritedElementForWidgetOfExactType(type)?.widget
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Command PhaseScriptExecution failed with a nonzero exit code
note: Using new build system
note: Building targets in parallel
note: Planning build
note: Analyzing workspace
note: Constructing build description
note: Build preparation complete
note: Removed stale file

关于 Xcode 的错误日志:

/Users/wissamboughannam/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/provider-3.2.0/lib/src/provider.dart:259:19: Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/Users/wissamboughannam/Developer/flutter/packages/flutter/lib/src/widgets/framework.dart').
Try correcting the name to the name of an existing method, or defining a method named 'inheritFromWidgetOfExactType'.
        ? context.inheritFromWidgetOfExactType(type) as InheritedProvider<T>
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/Users/wissamboughannam/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/provider-3.2.0/lib/src/provider.dart:260:19: Error: The method 'ancestorInheritedElementForWidgetOfExactType' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/Users/wissamboughannam/Developer/flutter/packages/flutter/lib/src/widgets/framework.dart').
Try correcting the name to the name of an existing method, or defining a method named 'ancestorInheritedElementForWidgetOfExactType'.
        : context.ancestorInheritedElementForWidgetOfExactType(type)?.widget

这是主要错误,有很多警告,但我不确定它们是否导致构建失败。

运行flutter doctor 结果:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 2.1.0-11.0.pre.227, on macOS 11.1 20C69 darwin-arm, locale en-SA)
[✓] android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.54.1)
[✓] Connected device (2 available)

• No issues found!

【问题讨论】:

你检查你的证书了吗?如果您运行flutter build ios --verbose,您是否会收到类似“无法为签名者构建自签名根的链......”的消息? 【参考方案1】:

我们知道 m1 mac 运行在基于 arm 的处理器上,因此很少有像 cocoa pods 和 ffi 这样的东西需要在此基础上构建。 经过长时间的研究,我想出了几个步骤,每次你坚持为 m1 mac 构建 xcode Flutter 应用程序时,这些步骤肯定会有所帮助。

要在带有 M1 芯片(Apple Silicon)的 Mac 上完全安装 cocoapods,请按照以下步骤操作:

在 M1 中安装 Cocoa Pods

sudo gem 安装 cocoapods sudo gem 安装 ffi arch -x86_64 sudo gem install cocoapods -n /usr/local/bin sudo gem install cocoapods -n /usr/local/bin

安装 ffi

sudo arch -x86_64 gem install ffi

#update repo

arch -x86_64 pod install --repo-update

Flutter iOS 构建

扑干净 flutter build ios

在flutter项目的iOS文件夹中重新安装依赖

arch -x86_64 pod install

【讨论】:

【参考方案2】:

我也有一台 M1 Mac,并且在稳定的频道上,没有任何错误。

先尝试切换到稳定版。

另外,尝试运行 $ flutter clean 并再次构建应用程序。

【讨论】:

谢谢 Kushal,但我已经设法弄清楚了,显然问题在于折旧的上下文,在迁移了我设法在开发通道上构建的所有软件包之后,现在一切正常。 【参考方案3】:

我删除了所有折旧,项目运行良好。

【讨论】:

在这种情况下什么是“折旧”?【参考方案4】:

我遇到了另一个与颤振依赖相关的问题,即在 firebase 依赖、图像裁剪器、条纹等上显示错误 OBJC_CLASS

我的 xcode 禁用存档和分发菜单,因为没有成功构建存档。 我用这个想法解决了这个问题:在终端/android studio 上构建应用程序,然后在 xcode 上存档和分发。

步骤是:

卸载 cocoapods

sudo gem uninstall cocoapods

安装 brew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

nano ~/.zshrc

export PATH="/opt/homebrew/bin:$PATH"

brew --version

安装 cocoapods

brew install cocoapods

pod --version

链接 xcode

flutter doctor

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

sudo xcodebuild -runFirstLaunch

转到你的 Flutter 项目并

flutter clean flutter pub get cd ios pod install pod update

使用 xcode 打开 Runner.xcworkspace 并设置您的签名和功能

在你的 Flutter 项目上打开终端并构建

flutter build ios --release

这将构建 Runner.app

转到 xcode,在 Runner 上选择任何 ios 设备 (arm64),以启用存档。

然后

product->archive->distribute app->next->手动登录->设置你的证书分发器

这将构建您的 .ipa

【讨论】:

以上是关于Flutter 项目无法在 Mac M1 上构建的主要内容,如果未能解决你的问题,请参考以下文章

在 M1 Mac 上运行 Flutter Doctor 时出错 - 无法找到捆绑的 Java 版本

带有 Xcode 12.5 的 M1 Mac 从不运行 Flutter 项目

迁移到 M1 Mac 后在 iOS 上运行 Flutter App 时出错

mac m1上flutter的Java配置问题

Xcode - 命令 PhaseScriptExecution 因非零退出代码而失败 - Mac M1

如何更改适用于 M1 Mac 的 Flutter 应用程序的名称?