Xcode Legacy Build System替换为New Build System编译报错及解决方法

Xcode 14把原来的Legacy Build System干掉了,默认用新的New Build System。
或者Xcode 14以下,工程直接使用的New Build System。
此文章用来记录自己遇到的问题。如有错误感谢指证。

一、pods中的resource bundles要指定team
在podfile文件中指定team(需要指定Team ID,这么配置在某些工程可能不适用,可临时解决问题)

post_install do |installer|
  installer.pods_project.build_configurations.each do |config|
      config.build_settings['CODE_SIGN_IDENTITY'] = "Team ID";
      config.build_settings['CODE_SIGN_IDENTITY[sdk=*]'] = "Team ID";
  end
end

或者

def fix_config(config)
  if config.build_settings['DEVELOPMENT_TEAM'].nil?
    config.build_settings['DEVELOPMENT_TEAM'] = 'Team ID'
  end
end

post_install do |installer|
  installer.generated_projects.each do |project|
    project.build_configurations.each do |config|
        fix_config(config)
    end
    project.targets.each do |target|
      target.build_configurations.each do |config|
        fix_config(config)
      end
    end
  end
end

二、pods执行脚本assets.car重复
这种错误一般出现在自己的私有库中,且库是静态库(动态库不会有这个问题),podspec文件中使用resources管理资源文件,未使用resource_bundles,就会导致在New Build System下报错“Multiple commands produce”。因为pods在执行脚本过程中,会将xcassets打包成assets.car,库中有xcassets文件,主工程中也有xcassets文件,会将这些assets.car都拷贝到app包的根目录下,自然文件冲突。解决方式是调整一下资源文件管理方式(见方法二)。

  1. 方法一:设置disable_input_output_paths => true可解决assets.car和重复的资源文件问题,但可能会有其他问题和减慢编译速度。
  2. 方法二:将库的podspec文件中resources改为resource_bundles(xcassets),相应的资源文件获取方式也需要修改。(bundle、xib是否放到resource_bundles中,自行决定。如果多个库/工程存在同名bundle、xib的文件,可加入到resource_bundles中,起到类似命名空间的作用。)
#  spec.resources = [
#      'Resources/**/*.xib',
#      'Resources/**/*.bundle',
#      'Resources/**/*.xcassets'
#  ]
  
# 使用spec.name作为bundle文件的文件名
spec.resource_bundles = {spec.name => [
  'Resources/**/*.xib',
  'Resources/**/*.bundle',
  'Resources/**/*.xcassets'
  ]}
/// 获取对应Bundle路径 ,在库中任意类中使用self都可以
/// 使用bundleForClass同时兼容动态库和静态库。如果在静态库中,会返回mainBundle,如果在动态库中,会返回库本身路径。

//使用了resource_bundles的方式管理资源(动态库/静态库)
NSBundle *mainBundle = [NSBundle bundleForClass:self.class];
NSString *path = [mainBundle pathForResource:@"Resources" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];

//使用了resources的方式管理资源(动态库)
//NSBundle *bundle = [NSBundle bundleForClass:self.class];

UIViewController *vc = [[super alloc] initWithNibName:@"UIViewController" bundle:bundle];
UIImage *image = [UIImage imageNamed:@"imageName" inBundle:bundle compatibleWithTraitCollection:nil];

三、直接放在项目中的多个同名资源文件报错

  1. 方法一:设置disable_input_output_paths => true可解决assets.car和重复的资源文件问题,但可能会有其他问题和减慢编译速度。
  2. 方法二:比如存在Test1/a.png和Test2/a.png会报错。如果是在同一个工程里,相同保留一份就好,如果不同修改文件名。如果是私有库,使用resources方式管理资源文件,也会和其他私有库/工程中同名文件冲突,这时建议使用resource_bundles方式管理(具体在上文第二点)。因为资源文件会直接打包进工程app文件根目录下,所以多个同名文件在New Build System下会报错“Multiple commands produce”的错。

你可能感兴趣的:(Xcode Legacy Build System替换为New Build System编译报错及解决方法)