gpt4 book ai didi

ios - 具有多个项目的 Cocoapods : Firebase causes "Class XXX is implemented in both (...)" warning

转载 作者:行者123 更新时间:2023-12-05 04:53:59 24 4
gpt4 key购买 nike

由于公司的决定,我目前正在处理一个 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 的回答,我在没有 ... 实现的情况下工作... 带有以下 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: https://stackoverflow.com/questions/65904011/cocoapods-with-multiple-projects-firebase-causes-class-xxx-is-implemented-in-b
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

最佳答案

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

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

因此您需要在 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

关于ios - 具有多个项目的 Cocoapods : Firebase causes "Class XXX is implemented in both (...)" warning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65904011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com