Flutter项目自定义插件iOS平台引用.a静态库遇到的坑

说明

在flutter开发中有些第三方插件满足不了项目的开发需求,避免不了自己写自定义插件,关于自定义插件如何写,此处不再详细描述,今天会主要总结一下自定义插件iOS平台引用.a静态库遇到的问题。

.podspec文件

#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
  s.name             = 'flutter_bt_printer'
  s.version          = '0.0.2'
  s.summary          = 'A new Flutter plugin.'
  s.description      = <<-DESC
A new Flutter plugin.
                       DESC
  s.homepage         = 'http://example.com'
  s.license          = {
    
     :file => '../LICENSE' }
  s.author           = {
    
     'Your Company' => '[email protected]' }
  s.source           = {
    
     :path => '.' }
  s.source_files = 'Classes/**/*'
  s.vendored_libraries = 'Classes/**/*.a'
  s.public_header_files = 'Classes/**/*.h'
  s.dependency 'Flutter'
  s.ios.deployment_target = '8.0'
end

1. 引用第三方库(.a文件)

s.dependency 'AFNetworking'

2. 引用三方静态库(.a文件)

s.vendored_libraries = 'Classes/xxx.a'

3. 当我们运行时,会报如下错误

ld: library not found for -lxxx
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这时候需要删除 flutter主工程的pods下对应的插件(要找到引用.a对应的插件)的TARGETS–>Build Settings->Other Linker Flags列表中的-l"XXX" 选项,再run一下还是报警告报错,内容如下:

Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_LPAPI", referenced from:
   objc-class-ref in Printermanager.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

说明 Printermanager文件引用了xxx.a的头文件。
这时候我们需要TARGETS–>Build Phases–>Compile Sources 删除xxx.a在Compile Sources同级的另一个选项Link Binary With Libraries 里添加刚刚删除的xxx.a文件。

4.Other Linker Flags配置

还要根据引用.a静态库的提示是否要在插件的TARGETS–>Build Settings->Other Linker Flags列表中加入-ObjC或-all_load等其他flag.

5.运行

最后 run 运行,如果不成功或如果上述步骤出错可以使用pod install重置设置选项。

猜你喜欢

转载自blog.csdn.net/gjm_123/article/details/103689082