美文网首页奇葩bug搜集
已有iOS项目集成Flutter遇到的一些坑

已有iOS项目集成Flutter遇到的一些坑

作者: 徐鼎城 | 来源:发表于2019-08-02 19:53 被阅读0次

    1.XCode10.3,CocoaPods1.7.5, Flutter 1.8.4-pre.65(一定是master channel,不能是stable), 根据标题中的环境对应的版本自行升级,如版本高太多的话不敢保证一定会成功。

    2.参照官方文档https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps#ios的iOS部分很快就构建好了,于是Commod+B构建,可是马上会遇到第一个坑,如下图:

    ld: warning: directory not found for option '-L/Users/wang/Library/Developer/Xcode/DerivedData/test_flutter-dbqsilcpjusqpadpdwvgsoqctumw/Build/Products/Debug-iphonesimulator/FlutterPluginRegistrant'

    ld: library not found for -lFlutterPluginRegistrant

    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    我晕这是什么鬼啊,尝试了各种方式都无效:

    怀疑是没有找到静态库 lib(FlutterPluginRegistrant.a),工程目录和flutter_module目录以及flutterSDK的安装目录都搜遍了,也没有发现这个文件。。。。。。

    于是怀疑是新版本的Flutter将FlutterPluginRegistrant.a移除了,根本就不存在这个文件,接下来在宿主iOS工程下的Pods目录中的『Pods-test_flutter.debug.xcconfig』文件中搜索"FlutterPluginRegistrant",果然发现了一些线索

    FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_ROOT}/../../flutter_module/.ios/Flutter" "${PODS_ROOT}/../../flutter_module/.ios/Flutter/engine"GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1HEADER_SEARCH_PATHS = $(inherited) "${PODS_ROOT}/Headers/Public" "${PODS_ROOT}/Headers/Public/FlutterPluginRegistrant"LD_RUNPATH_SEARCH_PATHS = $(inherited) '@executable_path/Frameworks' '@loader_path/Frameworks'LIBRARY_SEARCH_PATHS = $(inherited) "${PODS_CONFIGURATION_BUILD_DIR}/FlutterPluginRegistrant"OTHER_CFLAGS = $(inherited) -isystem "${PODS_ROOT}/Headers/Public" -isystem "${PODS_ROOT}/Headers/Public/FlutterPluginRegistrant" -iframework "${PODS_ROOT}/../../flutter_module/.ios/Flutter/engine" -iframework "${PODS_ROOT}/../../flutter_module/.ios/Flutter"OTHER_LDFLAGS = $(inherited) -ObjC -l"FlutterPluginRegistrant" -framework "App" -framework "Flutter"PODS_BUILD_DIR = ${BUILD_DIR}PODS_CONFIGURATION_BUILD_DIR = ${PODS_BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)PODS_PODFILE_DIR_PATH = ${SRCROOT}/.PODS_ROOT = ${SRCROOT}/Pods

    接下来将 带有 『FlutterPluginRegistrant』内容的部分全部删除(Pods-test_flutter.debug.xcconfig也如法炮制),修改后的配置如下:

    FRAMEWORK_SEARCH_PATHS = $(inherited) "${PODS_ROOT}/../../flutter_module/.ios/Flutter" "${PODS_ROOT}/../../flutter_module/.ios/Flutter/engine"

    GCC_PREPROCESSOR_DEFINITIONS = $(inherited) COCOAPODS=1

    HEADER_SEARCH_PATHS = $(inherited)

    LD_RUNPATH_SEARCH_PATHS = $(inherited) '@executable_path/Frameworks' '@loader_path/Frameworks'

    LIBRARY_SEARCH_PATHS = $(inherited)

    OTHER_CFLAGS = $(inherited) -isystem "${PODS_ROOT}/Headers/Public" -iframework "${PODS_ROOT}/../../flutter_module/.ios/Flutter/engine" -iframework "${PODS_ROOT}/../../flutter_module/.ios/Flutter"

    OTHER_LDFLAGS = $(inherited) -ObjC -framework "App" -framework "Flutter"

    PODS_BUILD_DIR = ${BUILD_DIR}

    PODS_CONFIGURATION_BUILD_DIR = ${PODS_BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)

    PODS_PODFILE_DIR_PATH = ${SRCROOT}/.

    PODS_ROOT = ${SRCROOT}/Pods

    接下来Command+B再次构建,奇迹发生了,刚才的错误消失了,继续往下走,

    参照官方文档将 Appdelegate.m文件改为如下内容

    #import  <FlutterPluginRegistrant/GeneratedPluginRegistrant >// Only if you have Flutter Plugins

    #import "AppDelegate.h"

    @interface AppDelegate ()

    @end

    @implementation AppDelegate

    // This override can be omitted if you do not have any Flutter Plugins.

    - (BOOL)application:(UIApplication*)application

    didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

     self.flutterEngine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];

    [self.flutterEngine runWithEntrypoint:nil];

    [GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];

     return [super application:application didFinishLaunchingWithOptions:launchOptions];

    }

    Command+B走起,然而第二个坑来了,

    Undefined symbols for architecture x86_64:

      "_OBJC_CLASS_$_GeneratedPluginRegistrant", referenced from:

          objc-class-ref in AppDelegate.o

    ld: symbol(s) not found for architecture x86_64

    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    彻底晕倒,这又是什么鬼。。。自习一看才发试一个经典错误,原来是编译期的符号表$_GeneratedPluginRegistrant在链接期间找不到,由于GeneratedPluginRegistrant是在FlutterPluginRegistrant中定义的,这更加说明了FlutterPluginRegistrant.a根本不存在,于是接下去到刚才用命令"flutter create -t module --org com.example flutter_module"构建的 flutter_module 目录中去搜索文件GeneratedPluginRegistrant(mac系统下用finder搜索小伙伴们一定都会了),果然有所收获,在../flutter_module/.ios/Flutter/FlutterPluginRegistrant/Classes/目录下发现了两个文件GeneratedPluginRegistrant.h和GeneratedPluginRegistrant.m,这下就好办了,在宿主工程中把这两个文件添加进来,方式任意,可以reference也可以copy过来。

    二话不说马上Command+B构建工程,大功告成!!可以来支烟庆祝一下了!

    希望此文可以帮助迷茫中的你走出困境,现在网上关于这个错误的解决方案少之甚少,如果看到这里,此时你的问题已经得到解决了!!

    相关文章

      网友评论

        本文标题:已有iOS项目集成Flutter遇到的一些坑

        本文链接:https://www.haomeiwen.com/subject/cusryqtx.html