当一个工程随着时间的推移,开发人员的增加,工程变得硕大且不好维护,提交代码冲突重重,是时候考虑项目的组件化了。若从一开始就走组件化之路绝逼是一个好办法。
组件化的目的是为了达到解耦,多工程联编更是达到了物理解耦,最理想的是各种模块可以随意的应用到其他的工程。每天蹲坑的时候也会在思索如何去走这条道路。蘑菇街以及反革命主义者的文章给我启发很大,综合两者不失为一种好办法。但本文章主要是介绍如何多工程联编,这几天抽空写了个Demo(写的比较随意,大家忽略细节),废话不多说直接上步骤。
首先上下工程效果图:

建立一个新工程:

工程少不了pods吧,不然第三方的东西管理肿么办,我们偷偷的跑到根目录下去添加一下。为了方便我下载了一个pods的客户端,哈哈,只是不能好好的用命令行装逼了:

打开WorkPlace将Enable Bitcode设为No:

接下来就是各个组件Framework的建立了:

把他添加到WorkPlace里面来:

接下来的几个设置很关键了:



BuildPhases中添加CopyFile,在里面把编译出来的模块Framework,Bundle拷贝到App的ModuleFrames目录下,以便其他引用他的工程可以方便加入:


在CommonModule里面添加一点公用的东西:

把对外的头文件拖到public里面:

单独编译一下CommonModule工程,主工程就可以导入CommonModule使用啦:

主工程将他引入:


主工程编译:



要是Framework里面有资源文件怎么办,主工程怎么去调用,组件工程之前能不能相互调用?
接下来我们多建立几个组件工程并且添加部分资源:



添加ModuleOne工程要使用的资源以Storyboard为例:






Build Resource资源:


添加ModuleOne的Framework一个组件建立完成,按照此方法可以建立多个组件:

开发协作问题,自己负责的业务模块可以单独编译,并不影响其他业务模块。如果需要全部编译,一个一个去编译会显得有点SB。我们可以建立一个Target Build所有的模块也可以自己调整Framework的编译顺序,太棒了:



这样就可以实现全部工程的编译了,需要注意,模拟器跟真机切换时候需要分别编译,打包发布不能用模拟器编译。
子工程需要添加第三方库时:
把第三方Framework文件通过copy files拷贝到ModuleFrameworks里和自己Framework文件放在一起。
如果新增了第三方库和Bundle,需在copy files中添加拷贝文件。
模块内新增了图片等资源文件,需在Bundle的Resourse Target中将新文件添加到Copy Bundle Resource中才会被打到Bundle包里。
附上工程目录图:

PS:
特别鸣谢(金鸿,俊华)
大家有好的建议或者帮助欢迎留言(模块工程间的共用资源问题欢迎大家讨论留言)
Demo地址:https://pan.baidu.com/s/1hsKFMNa
如需转载标明出处
各位看官觉得有用的话就请Yes-Cui喝瓶水吧,打赏一个
网友评论
基本模块[8755:273255] -[UIViewController showPartTwoPage]: unrecognized selector sent to instance 0x7fd40ac3e1e0
基本模块[8755:273255] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController showPartTwoPage]: unrecognized selector sent to instance 0x7fd40ac3e1e0'
根据demo操作会报这些错误是为啥呢