CocoaPods-单独修改Debug或者Release模式下的Build Settings

最近开发的时候遇到了一个问题,某些pod库在不同编译模式下所需要的编译选项或者 BuildSettings 不同,比如说 Release 模式下GCC_GENERATE_TEST_COVERAGE_FILESNO,但是 Debug 模式下为YES
我们不可能每次pod install完成之后手动去修改。
其实修改Podfile就可以达到想要的目的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if (target.name == 'APAddressBook' || target.name == 'CocoaLumberjack')
if (config.name == 'Debug')
config.build_settings['GCC_GENERATE_TEST_COVERAGE_FILES'] = 'YES'
config.build_settings['GCC_INSTRUMENT_PROGRAM_FLOW_ARCS'] = 'YES'
end
end
end
end
end
end

  • 如果主工程有多个BuildConfiguration,并且把主工程对应的BuildSettings -> Preprosessor Macros 加入DEBUG=1,对于加入的pod库是不会起作用的。也就是说使用这个BuildConfiguration,只有主功程能进行debug调试,而加入的第三方库不能。需要对pod库进行单独设置。或者直接修改Podfiles,来执行脚本进行安装时自动设置。下面是脚本。
1
2
3
4
5
6
7
8
9
post_install do |installer_representation|
installer_representation.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if config.name != 'Release'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']
end
end
end
end
  • 关闭Pod库的编译优化
1
2
3
4
5
6
7
8
9
10
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if (config.name == 'Debug')
config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
end
end
end
end
end
  • 禁止生成 dSYM 文件
1
config.build_settings['DEBUG_INFORMATION_FORMAT'] = 'dwarf'
  • 如果要复制debug模式下的环境,推荐加上如下设置
1
2
3
4
5
6
7
8
9
# Code Coverage Start
config.build_settings['GCC_GENERATE_TEST_COVERAGE_FILES'] = 'YES'
config.build_settings['GCC_INSTRUMENT_PROGRAM_FLOW_ARCS'] = 'YES'
# Code Coverage End

config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']
config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
config.build_settings['DEBUG_INFORMATION_FORMAT'] = 'dwarf'
config.build_settings['VALIDATE_PRODUCT'] = 'NO'

CocoaPods-单独修改Debug或者Release模式下的Build Settings
https://dnacore.github.io/post/42fa5109-5ffd-4428-9f79-383eb44e4f10.html
作者
DNACore
发布于
2019年2月27日
更新于
2023年7月12日
许可协议