具有多个项目的 Cocoapods:Firebase 导致 "Class XXX is implemented in both (...)" 警告

Cocoapods with multiple projects: Firebase causes "Class XXX is implemented in both (...)" warning

由于公司的决定,我目前正在处理一个 Swift 项目,其中工作区包含多个项目 (ACM...)。

在使用 Cocoapods 添加 Firebase 时,我需要在两个项目中使用它(在 'myapp' 和 'ACMLoyalty' 中)。所以我的 .podfile 看起来像这样:

workspace 'myapp.xcworkspace'
platform :ios, '12.0'
use_frameworks!

def shared_pods
  source 'https://github.com/CocoaPods/Specs.git'

  pod 'Nuke', '9.2.3'
  pod 'Alamofire', '~> 4.9.1'
  pod 'DeepDiff', '2.3.1'
end

def firebase_pods
  pod 'Firebase/Analytics', '7.4.0'
  pod 'Firebase/Crashlytics', '7.4.0'
  pod 'Firebase', '7.4.0'
end

def appcenter_pods
  pod 'AppCenter', '4.1.0'
  pod 'AppCenter/Distribute', '4.1.0'
end

project 'myapp.xcodeproj'
project 'ACMCore/ACMCore.xcodeproj'
project 'ACMData/ACMData.xcodeproj'
project 'ACMLoyalty/ACMLoyalty.xcodeproj'
project 'ACMMVVM/ACMMVVM.xcodeproj'
project 'ACMNetwork/ACMNetwork.xcodeproj'
project 'ACMShared/ACMShared.xcodeproj'

target :myapp do
  project 'myapp.xcodeproj'
  shared_pods
  firebase_pods
  appcenter_pods
end

target :ACMCore do
  project 'ACMCore/ACMCore.xcodeproj'
  shared_pods
end

target :ACMData do
  project 'ACMData/ACMData.xcodeproj'
  shared_pods
end

target :ACMLoyalty do
  use_frameworks! :linkage => :dynamic
  project 'ACMLoyalty/ACMLoyalty.xcodeproj'
  shared_pods
  firebase_pods

  target 'ACMLoyaltyTests' do
    inherit! :search_paths
    shared_pods
    firebase_pods
  end

  target 'ACMLoyaltyTestHost' do
    inherit! :search_paths
    shared_pods
    firebase_pods
  end
end

target :ACMMVVM do
  project 'ACMMVVM/ACMMVVM.xcodeproj'
  shared_pods

  target 'ACMMVVMTests' do
    inherit! :search_paths
    shared_pods
  end
end

target :ACMNetwork do
  project 'ACMNetwork/ACMNetwork.xcodeproj'
  shared_pods

  target 'ACMNetworkTests' do
    inherit! :search_paths
    shared_pods
  end
end

target :ACMShared do
  project 'ACMShared/ACMShared.xcodeproj'
  shared_pods

  target 'ACMSharedTests' do
    inherit! :search_paths
    shared_pods
  end
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if Gem::Version.new('9.0') > Gem::Version.new(config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'])
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
      end
    end
  end
end

问题:

在 运行 期间,我在控制台中收到以下警告:

objc[2946]: Class FIRAnalyticsConnector is implemented in both /private/var/containers/Bundle/Application/05B31227-083E-42A7-9E27-DB7924A754B9/myapp.app/Frameworks/ACMLoyalty.framework/ACMLoyalty (0x107c4d1b8) and /private/var/containers/Bundle/Application/05B31227-083E-42A7-9E27-DB7924A754B9/myapp.app/myapp (0x104217298). One of the two will be used. Which one is undefined.

这只是示例中的一行,但还有许多其他具有相同描述的 Firebase 类。

经过一些研究我知道,问题是 Firebase 是一个静态库,将被简单地复制到导致此问题的特定目标。使用其他库(动态)不会出现此问题。

有人能解决我的问题吗?

编辑:回答@mbi

我已经尝试过 abstract_target 的解决方案。

请找到我的 .podfile 这次尝试:

# Uncomment the next line to define a global platform for your project
workspace 'myapp.xcworkspace'
platform :ios, '12.0'
use_frameworks!

project 'myapp.xcodeproj'
project 'ACMCore/ACMCore.xcodeproj'
project 'ACMData/ACMData.xcodeproj'
project 'ACMLoyalty/ACMLoyalty.xcodeproj'
project 'ACMMVVM/ACMMVVM.xcodeproj'
project 'ACMNetwork/ACMNetwork.xcodeproj'
project 'ACMShared/ACMShared.xcodeproj'

abstract_target 'myapp-app' do
  source 'https://github.com/CocoaPods/Specs.git'

  pod 'Nuke', '9.2.3'
  pod 'Alamofire', '~> 4.9.1'
  pod 'DeepDiff', '2.3.1'

  pod 'Firebase/Analytics', '7.4.0'
  pod 'Firebase/Crashlytics', '7.4.0'
  pod 'Firebase', '7.4.0'

  def appcenter_pods
    pod 'AppCenter', '4.1.0'
    pod 'AppCenter/Distribute', '4.1.0'
  end

  target :myapp do
    project 'myapp.xcodeproj'
    appcenter_pods
  end

  target :ACMCore do
    project 'ACMCore/ACMCore.xcodeproj'
  end

  target :ACMData do
    project 'ACMData/ACMData.xcodeproj'
  end

  target :ACMLoyalty do
    project 'ACMLoyalty/ACMLoyalty.xcodeproj'

    target 'ACMLoyaltyTests' do
    end

    target 'ACMLoyaltyTestHost' do
    end
  end

  target :ACMMVVM do
    project 'ACMMVVM/ACMMVVM.xcodeproj'

    target 'ACMMVVMTests' do
    end
  end

  target :ACMNetwork do
    project 'ACMNetwork/ACMNetwork.xcodeproj'

    target 'ACMNetworkTests' do
    end
  end

  target :ACMShared do
    project 'ACMShared/ACMShared.xcodeproj'

    target 'ACMSharedTests' do
    end
  end
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if Gem::Version.new('9.0') > Gem::Version.new(config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'])
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
      end
    end
  end
end

问题是,对于这个解决方案,我有更多的警告,因为 Firebase 是在所有项目和冲突中构建的。

EDIT2:根据@arturdev 的回答,我在没有 ... implemented in both... 警告的情况下使用了以下 podfile:

# Uncomment the next line to define a global platform for your project
workspace 'myapp.xcworkspace'
platform :ios, '12.0'
use_frameworks!

project 'myapp.xcodeproj'
project 'ACMCore/ACMCore.xcodeproj'
project 'ACMData/ACMData.xcodeproj'
project 'ACMLoyalty/ACMLoyalty.xcodeproj'
project 'ACMMVVM/ACMMVVM.xcodeproj'
project 'ACMNetwork/ACMNetwork.xcodeproj'
project 'ACMShared/ACMShared.xcodeproj'

abstract_target 'myapp-app' do
  source 'https://github.com/CocoaPods/Specs.git'

  pod 'Nuke', '9.2.3'
  pod 'Alamofire', '~> 4.9.1'
  pod 'DeepDiff', '2.3.1'

  def firebase_pods
    pod 'Firebase/Analytics', '7.4.0'
    pod 'Firebase/Crashlytics', '7.4.0'
    pod 'Firebase', '7.4.0'
  end

  def appcenter_pods
    pod 'AppCenter', '4.1.0'
    pod 'AppCenter/Distribute', '4.1.0'
  end

  target :myapp do
    project 'myapp.xcodeproj'
    firebase_pods
    appcenter_pods

    target 'myappTests' do
    end
  end

  target :ACMCore do
    project 'ACMCore/ACMCore.xcodeproj'

    target 'ACMCoreTests' do
      firebase_pods
    end
  end

  target :ACMData do
    project 'ACMData/ACMData.xcodeproj'

    target 'ACMDataTests' do
      firebase_pods
    end
  end

  target :ACMLoyalty do
    project 'ACMLoyalty/ACMLoyalty.xcodeproj'
    firebase_pods

    target 'ACMLoyaltyTests' do
    end

    target 'ACMLoyaltyTestHost' do
    end
  end

  target :ACMMVVM do
    project 'ACMMVVM/ACMMVVM.xcodeproj'

    target 'ACMMVVMTests' do
    end
  end

  target :ACMNetwork do
    project 'ACMNetwork/ACMNetwork.xcodeproj'

    target 'ACMNetworkTests' do
    end
  end

  target :ACMShared do
    project 'ACMShared/ACMShared.xcodeproj'

    target 'ACMSharedTests' do
    end
  end
end

PROJECT_ROOT_DIR = File.dirname(File.expand_path(__FILE__))
PODS_DIR = File.join(PROJECT_ROOT_DIR, 'Pods')
PODS_TARGET_SUPPORT_FILES_DIR = File.join(PODS_DIR, 'Target Support Files')

post_install do |installer|

  ## For more information: 
  remove_static_framework_duplicate_linkage({
     'ACMLoyalty' => ['FBLPromises', 'FIRAnalyticsConnector', 'FirebaseAnalytics', 'FirebaseCore', 'FirebaseCoreDiagnostics', 'FirebaseCrashlytics', 'FirebaseInstallations', 'GoogleAppMeasurement', 'GoogleDataTransport', 'GoogleUtilities']
  })

  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      #config.build_settings['LD_NO_PIE'] = 'NO'
      if Gem::Version.new('9.0') > Gem::Version.new(config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'])
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
      end
    end
  end
end

# CocoaPods provides the abstract_target mechanism for sharing dependencies between distinct targets.
# However, due to the complexity of our project and use of shared frameworks, we cannot simply bundle everything under
# a single abstract_target. Using a pod in a shared framework target and an app target will cause CocoaPods to generate
# a build configuration that links the pod's frameworks with both targets. This is not an issue with dynamic frameworks,
# as the linker is smart enough to avoid duplicate linkage at runtime. Yet for static frameworks the linkage happens at
# build time, thus when the shared framework target and app target are combined to form an executable, the static
# framework will reside within multiple distinct address spaces. The end result is duplicated symbols, and global
# variables that are confined to each target's address space, i.e not truly global within the app's address space.
#
# Previously we avoided this by linking the static framework with a single target using an abstract_target, and then
# provided a shim to expose their interfaces to other targets. The new approach implemented here removes the need for
# shim by modifying the build configuration generated by CocoaPods to restrict linkage to a single target.
def remove_static_framework_duplicate_linkage(static_framework_pods)
  puts "Removing duplicate linkage of static frameworks"

  Dir.glob(File.join(PODS_TARGET_SUPPORT_FILES_DIR, "Pods-*")).each do |path|
    pod_target = path.split('-', -1).last

    static_framework_pods.each do |target, pods|
      next if pod_target == target
      frameworks = pods.map { |pod| identify_frameworks(pod) }.flatten

      Dir.glob(File.join(path, "*.xcconfig")).each do |xcconfig|
        lines = File.readlines(xcconfig)

        if other_ldflags_index = lines.find_index { |l| l.start_with?('OTHER_LDFLAGS') }
          other_ldflags = lines[other_ldflags_index]

          frameworks.each do |framework|
            other_ldflags.gsub!("-framework \"#{framework}\"", '')
          end

          File.open(xcconfig, 'w') do |fd|
            fd.write(lines.join)
          end
        end
      end
    end
  end
end

def identify_frameworks(pod)
  frameworks = Dir.glob(File.join(PODS_DIR, pod, "**/*.framework")).map { |path| File.basename(path) }

  if frameworks.any?
    return frameworks.map { |f| f.split('.framework').first }
  end

  return pod
end

错误 Class XYZ is implemented in both ... 表示同一个 class 正在被编译并链接到多个构建的产品中。你让事情变得比他们需要的更复杂。

我真的很难通过查看给定的 Podfile 来弄清楚您实际想要实现的目标。显然有一些事情可以改进,但这在很大程度上取决于要求是什么。

例如,您应该阅读并使用 abstract_target 的概念以及 Podfile 中的嵌套目标。这使您可以只描述一次依赖关系并将它们继承到嵌套目标。参见 https://guides.cocoapods.org/using/the-podfile.html

此外,您必须记住,Cocoapods 正在应用一种称为去重复的方法,这基本上意味着无论您在 Podfile 中指定依赖项的频率如何,它都会尝试将它们压缩到生成的目标中尽可能少的目标 Pods 项目。所以对我来说,你的 Podfile 中的许多相同依赖项的声明(例如 share_pods 似乎有些错误。

而且在我看来,您正在以一种奇怪的方式使用继承。例如,您限制继承以排除 parent 的部门,只是为了在下一行再次添加它们。示例:

target :ACMMVVM do
  project 'ACMMVVM/ACMMVVM.xcodeproj'
  shared_pods  #  <-- dependency here

  target 'ACMMVVMTests' do
    inherit! :search_paths  # <-- excluded here
    shared_pods      # <-- added again here
  end
end

根据我的经验,在大多数情况下,嵌套测试目标不需要依赖项,因此像您一样仅继承搜索路径就可以了。但是只有你知道这个特定的测试目标是否真的依赖于share_pods

我很确定一旦您理清了您的实际需求并将 Podfile 重写为抽象和 non-abstract 目标的嵌套结构,您的问题很有可能会消失。

一些 pod 依赖项被编译为静态库而不是动态库(f.e。Firebase 和其他 Google 库)。

静态框架不能链接多次(f.e。在框架目标和主目标中也链接上面的框架),因为静态的链接框架在构建时发生,但对于动态框架,链接在运行时发生,链接器足够智能,可以避免重复。

因此您需要在 podfile 中编写一个脚本来删除静态库链接。

一个例子:

post_install do |installer|
  remove_static_framework_duplicate_linkage({
        'FrameworkTarget1' => ['Firebase', 'FirebaseAnalytics', 'FirebaseCore', 'FirebaseCoreDiagnostics', 'FirebaseCoreDiagnosticsInterop', 'FirebaseInstanceID', 'FirebaseInstallations', 'GoogleAppMeasurement', 'GoogleDataTransport', 'GoogleDataTransportCCTSupport', 'GoogleUtilities'],
        'FrameworkTarget2' => ['GoogleMaps', 'GooglePlaces'],
  })

    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
                config.build_settings['LD_NO_PIE'] = 'NO'
                config.build_settings.delete 'IPHONEOS_DEPLOYMENT_TARGET'
        end
  end
end


# CocoaPods provides the abstract_target mechanism for sharing dependencies between distinct targets.
# However, due to the complexity of our project and use of shared frameworks, we cannot simply bundle everything under
# a single abstract_target. Using a pod in a shared framework target and an app target will cause CocoaPods to generate
# a build configuration that links the pod's frameworks with both targets. This is not an issue with dynamic frameworks,
# as the linker is smart enough to avoid duplicate linkage at runtime. Yet for static frameworks the linkage happens at
# build time, thus when the shared framework target and app target are combined to form an executable, the static
# framework will reside within multiple distinct address spaces. The end result is duplicated symbols, and global
# variables that are confined to each target's address space, i.e not truly global within the app's address space.
#
# Previously we avoided this by linking the static framework with a single target using an abstract_target, and then
# provided a shim to expose their interfaces to other targets. The new approach implemented here removes the need for
# shim by modifying the build configuration generated by CocoaPods to restrict linkage to a single target.
def remove_static_framework_duplicate_linkage(static_framework_pods)
  puts "Removing duplicate linkage of static frameworks"

  Dir.glob(File.join(PODS_TARGET_SUPPORT_FILES_DIR, "Pods-*")).each do |path|
    pod_target = path.split('-', -1).last

    static_framework_pods.each do |target, pods|
      next if pod_target == target
      frameworks = pods.map { |pod| identify_frameworks(pod) }.flatten

      Dir.glob(File.join(path, "*.xcconfig")).each do |xcconfig|
        lines = File.readlines(xcconfig)

        if other_ldflags_index = lines.find_index { |l| l.start_with?('OTHER_LDFLAGS') }
          other_ldflags = lines[other_ldflags_index]

          frameworks.each do |framework|
            other_ldflags.gsub!("-framework \"#{framework}\"", '')
          end

          File.open(xcconfig, 'w') do |fd|
            fd.write(lines.join)
          end
        end
      end
    end
  end
end

def identify_frameworks(pod)
  frameworks = Dir.glob(File.join(PODS_DIR, pod, "**/*.framework")).map { |path| File.basename(path) }

  if frameworks.any?
    return frameworks.map { |f| f.split('.framework').first }
  end

  return pod
end