如何在Flutter项目中实现.a格式依赖与本地Objective-C框架共存
解决Flutter插件静态库与本地Objective-C动态框架共存的问题
我完全懂你现在的困扰:不加use_frameworks!时本地框架跑的好好的,一加之后Flutter插件就报头文件找不到的错误。核心要实现的就是让Flutter插件保持.a静态库格式,同时本地Objective-C框架用.framework动态库格式,下面是具体的解决方案:
核心思路
我们不需要全局启用use_frameworks!,而是专门为本地框架创建一个子Target并启用动态库模式,主Runner Target保持默认的静态库模式来兼容Flutter插件。通过拆分Target的方式,实现两种库格式的共存。
修改你的Podfile
按照以下步骤调整配置:
- 移除文件开头的全局
use_frameworks!语句 - 新增一个子Target用于承载本地框架,在这个子Target里启用
use_frameworks! - 让主Runner Target依赖这个子Target
修改后的完整Podfile如下:
# Uncomment this line to define a global platform for your project platform :ios, '10.0' # 移除了全局的use_frameworks!,避免影响Flutter插件 # CocoaPods analytics sends network stats synchronously affecting flutter build latency. ENV['COCOAPODS_DISABLE_STATS'] = 'true' project 'Runner', { 'Debug' => :debug, 'Profile' => :release, 'Release' => :release, } def applibs pod 'ThirdPartySDK-Debug', :configuration => ['Debug'], :path => 'Generated_Key_IOS_SDK/Debug/ThirdPartySDK-Debug.podspec' pod 'ThirdPartySDK-Release', :configuration => ['Release'], :path => 'Generated_Key_IOS_SDK/Release/ThirdPartySDK-Release.podspec' end def parse_KV_file(file, separator='=') file_abs_path = File.expand_path(file) if !File.exists? file_abs_path return []; end pods_ary = [] skip_line_start_symbols = ["#", "/"] File.foreach(file_abs_path) { |line| next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ } plugin = line.split(pattern=separator) if plugin.length == 2 podname = plugin[0].strip() path = plugin[1].strip() podpath = File.expand_path("#{path}", file_abs_path) pods_ary.push({:name => podname, :path => podpath}); else puts "Invalid plugin specification: #{line}" end } return pods_ary end # 新增:专门承载本地框架的子Target,启用动态库模式 target 'LocalFrameworks' do use_frameworks! applibs end target 'Runner' do workspace 'Runner.xcworkspace' project 'Runner.xcodeproj' # 让主Target依赖本地框架的子Target pod 'LocalFrameworks', :path => './' # Prepare symlinks folder. We use symlinks to avoid having Podfile.lock # referring to absolute paths on developers' machines. system('rm -rf .symlinks') system('mkdir -p .symlinks/plugins') # Flutter Pods generated_xcode_build_settings = parse_KV_file('./Flutter/Runner.xcconfig') if generated_xcode_build_settings.empty? puts "Runner.xcconfig must exist. If you're running pod install manually, make sure flutter pub get is executed first." end generated_xcode_build_settings.map { |p| if p[:name] == 'FLUTTER_FRAMEWORK_DIR' symlink = File.join('.symlinks', 'flutter') File.symlink(File.dirname(p[:path]), symlink) pod 'Flutter', :path => File.join(symlink, File.basename(p[:path])) end } # Plugin Pods plugin_pods = parse_KV_file('../.flutter-plugins') plugin_pods.map { |p| symlink = File.join('.symlinks', 'plugins', p[:name]) File.symlink(p[:path], symlink) pod p[:name], :path => File.join(symlink, 'ios') } end # Prevent Cocoapods from embedding a second Flutter framework and causing an error with the new Xcode build system. install! 'cocoapods', :disable_input_output_paths => true post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['ENABLE_BITCODE'] = 'NO' end end end
后续操作
修改完Podfile后,执行以下步骤清理并重新安装依赖:
- 回到项目根目录,执行
flutter clean - 进入iOS目录:
cd ios - 清理现有Pods:
pod deintegrate - 重新安装Pods:
pod install
这样配置后,Flutter插件(比如path_provider、image_cropper)会保持.a静态库格式,你的本地Objective-C框架会以.framework动态库格式存在,两者就能正常共存运行了。
内容的提问来源于stack exchange,提问作者rocknitesh4189
相关产品推荐
相关产品推荐

