Flutter iOS插件添加bundle类型x.framework遇链接错误求解决方案
解决Flutter iOS插件中Mach-O Bundle类型Framework的链接排除问题
你要引入的是运行时加载的Mach-O 64-bit bundle arm64类型框架,而非动态库,直接用s.ios.vendored_frameworks添加后会触发链接错误——因为Xcode只允许链接MH_OBJECT或MH_DYLIB类型文件。要实现“包含到App安装包但跳过链接”,可以按以下步骤操作:
修改Podspec配置,标记框架为仅复制不链接
在你的插件.podspec文件中,添加post_install钩子,给目标框架设置DoNotLink属性:s.ios.vendored_frameworks = 'path/to/x.framework' s.post_install do |installer| installer.pods_project.targets.each do |target| # 替换成你的Flutter插件target名称 next unless target.name == '你的插件名称' target.frameworks_build_phases.each do |phase| phase.files.each do |file| if file.path.end_with?('x.framework/x') # 标记为仅复制到安装包,不参与链接 file.settings = { 'ATTRIBUTES' => ['RemoveHeadersOnCopy', 'DoNotLink'] } end end end end endDoNotLink属性会让Xcode跳过该框架的链接步骤,只完成复制到App包的操作。重新构建验证
执行flutter build ios --release重新构建项目,此时链接错误会消失,同时x.framework会被打包到最终App的Frameworks目录下。运行时手动加载框架
由于框架未被链接,需要在插件代码中通过dlopen手动加载:NSString *frameworkPath = [[NSBundle mainBundle] pathForResource:@"x" ofType:@"framework"]; NSString *bundleBinaryPath = [frameworkPath stringByAppendingPathComponent:@"x"]; void *frameworkHandle = dlopen([bundleBinaryPath UTF8String], RTLD_NOW); if (frameworkHandle) { // 加载成功后,可通过dlsym获取函数/符号并调用 // 示例:获取名为"some_function"的函数指针 // void (*someFunction)(void) = dlsym(frameworkHandle, "some_function"); // if (someFunction) someFunction(); // 用完后记得关闭句柄(按需) dlclose(frameworkHandle); } else { // 处理加载失败场景 const char *errorMsg = dlerror(); NSLog(@"加载x.framework失败: %s", errorMsg); }
内容的提问来源于stack exchange,提问作者Feodor Fitsner
相关产品推荐
相关产品推荐

