The principle of iOS-CocoaPods and the problem of Podfile.lock

background:

In daily development, we use third-party libraries in our projects and choose to use CocoaPods for management. We will often encounter 关于 podfile.lock 要不要提交,podfile.lock 的作用是干嘛的以及冲突怎么解决,pod install 和 pod update 有什么区别等这些问题these problems, and we will introduce how to solve these problems in detail below.

What are CocoaPods

CocoaPodsIt is a third-class library management tool under OS X and iOS. Through the CocoaPods tool, we can add so-called Podsdependent libraries to the project (these libraries must be supported by CocoaPods itself), and can easily manage its version.

Benefits of CocoaPods

1. When introducing third-party libraries, it can automatically complete various configurations for us, including configuring the compilation phase, linker options, and even -fno-objc-arc configuration in the ARC environment.

2. Using CocoaPods can easily find new third-party libraries. These class libraries are more "standard", rather than casually found on the Internet, so that we can find really useful class libraries.

About CocoaPods, you can refer to this article: www.jianshu.com/p/9e4e36ba8…

This article is mainly used to introduce the principles and precautions of CocoaPods.

CocoaPods integration principle

The principle of CocoaPods is to put all the dependent libraries into another project called Pods, but let the main project depend on the Pods project, so that the source code management tasks are moved from the main project to the Pods project.

1.Pods项目最终会编译成一个名为libPods.a的文件, 主项目只要依赖这个.a文件即可.
2.对于资源文件, CocoaPods提供了一个名为Pods-resources.sh的bash脚步, 该脚本在每次项目
 编译的时候都会执行,将第三方库的各种资源文件复制到目标目录中.
3.CocoaPods通过一个名为Pods.xcconfig的文件在编译设置所有的依赖和参数
复制代码
libPods.a
Pods-resources.sh
Pods.xcconfig
复制代码

How to download Cocoapods

s.source = { :git => '[email protected]:app/iOS-XXX.git', :tag => '1.0.0' }

  • Find the corresponding git repository according to: git => ' [email protected] :app/iOS-XXX.git';

  • 根据:tag => ‘1.0.0’定位到对应tag的提交(如果没有注明Pod依赖库版本则定位到最后一次的提交);

  • 在这次提交中检索后缀为.podspec的文件(文件可以随便命名),验证s.name是否与Podfile中的一致;

  • 如果不一致则install时会报错:[!]Unable to find a specification for ‘React’。验证成功后,就会根据Podspec中的s.source_files找到需要导入的代码文件,并通过其他的的数据找到对应的配置文件或资源文件等;

  • 然后将其下载到本地项目中。

注意:
如果是共有库,这些原理也相同。只是共有库要将podspec文件上传到cocoapods。在导入的时候通过名字React去cocoapods匹配对应的podspec,然后根据s.source去找到对应的仓库和对应的版本,然后会再去匹配新的podspec,后边的步骤就完全相同了。
复制代码

版本控制原理

当执行完 pod install之后,cocoapods 会生成一个podfile.lock的文件。podfile.lock 文件最大的用处在于多人开发。如果你没有在podfile中指定pods版本pod ‘React’,那么默认为获取当前React依赖库的最新版本

当团队中的某个人执行完 pod install 命令后,生成的 podfile.lock 文件就记录下了当时最新 pods 依赖库的版本,这时团队中的其他人 check 下来这份包含 podfile.lock 文件的工程以后,再去执行 pod install 命令时,获取下来的 pods 依赖库的版本和最开始用户获取到的版本一致。如果没有podfile.lock文件,后续所有用户执行pod install命令都会获取最新版本的React,这就可能造成一个团队使用的依赖库版本不一致,这对团队协作来说绝对是个灾难。在这种情况下,如果团队想使用当前最新版本的React依赖库,有两种方案

1、更改podfile,使其指向最新版本的‘React’依赖库

2、执行pod update命令;鉴于podfile.lock文件对团队协作如此重要,所以应该加入到版本控制里面。

podfile.lock到底要不要提交

podfile.lock文件是需要提交的。

当我们app组件化之后,一个app会包含各种不同的版本组件,而主工程变成一个壳子,用Podfile文件依赖了各种不同的组件, 然后我们的app完成了一次版本迭代上线之后,这个时候需要主工程相对应的组件版本,相对应版本的组件又依赖其他版本组件,如果我们手动通过spec管理肯定非常麻烦,又如果我们直接把全部的组件都写在podfile中,那我们每次的时候回溯(我们现在在开发2.0版本,想回到1.0版本)的时候,都需要首先记录之前app版本中对应的全部组件的版本,然后再在podfile中写上对应的组件版本号,如果组件特别多,需要回溯多次,这样也是非常痛苦的。那有没有最简单一句命令一个执行操作一分钟一步到位回溯之前的版本的方法呢?这个时候我们就需要依靠podfile.lock文件进行版本管理。

Podfile.lock 中会标注项目当前依赖库的准确版本,其中包括了项目在 Podfile 中直接标注使用的库,以及这些库依赖的其他库。这样的好处是当你跟小伙伴协同开发时,你的小伙伴同步了你的 Podfile.lock 文件后,他执行 pod install 会安装 Podfile.lock 指定版本的依赖库,这样就可以防止大家的依赖库不一致而造成问题。因此,CocoaPods 官方强烈推荐把 Podfile.lock 纳入版本控制之下。

所以如果我们用 Podfile.lock 文件进行版本管理,只需要在主工程(就是那个壳子)中把Podfile.lock 纳入版本控制之下(也就是把主工程对应的 Podfile.lock文件上传到gitlab),并且主工程中的 Podfile文件一定不要指定版本号, 然后把app 每次封版本的时候在gitlab打上相应的版本号tag,每次我们想回溯之前版本 只需要切换到当时的tag,然后执行 pod install (千万不能执行 pod upodate,因为执行完 pod upodate 对应的 Podfile.lock 里面的库版本会跟着更新)就可以了就是这么简单。

podfile.lock冲突的解决

diff: /../Podfile.lock: No such file or directory

diff: Manifest.lock: No such file or directory

error: The sandbox is not **in** sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.`
复制代码

平常开发过程中,项目在执行完 pod install 后提示 Podfile.lock 文件被修改,需要提交。但是其他同事(同事电脑的pod版本和你的不一致)拉下来代码后,运行项目发现了这个错误,然后执行 pod install ,解决了这个问题,然后再提交了 Podfile.lock 文件。等你再更新代码发现也出现了这个问题,这就会陷入一个死循环。而我们又不能不提交 Podfile.lock 文件,所以这个问题的解决版本就是将大家电脑的pod版本号保持一致

删除旧版本号
sudo gem uninstall -n /usr/local/bin cocoapods -v XXX

安装新版本号的pod
sudo gem install -n /usr/local/bin cocoapods -v XXX
复制代码

Pod install 和 Pod update的区别

pod install:用于第一次为项目下载pods,还有后续增加或者删除库的时候。

pod update: 用于更新库的版本。会遍历所有的库进行更新,并更新 Podfile.lock 文件。

pod update name: 只会更新指定的库。

pod repo update: 更新整个.cocoapods下的所有库的配置文件,挨个检查对应的框架有没有新版本发布,有的话更新本地的资源配置文件.

pod install --no-repo-update:根据podfile文件或者podfile.lock下载并导入对应的第三方库,跳过检查资源配置文件是否需要更新的环节
复制代码

使用示例

示例1:项目需要依赖A、B、C三个库,当前三个库的版本都是1.0.0,创建 Podfile 文件,执行Pod install。会拉取这三个库,并且Podfile.lock中 A、B、C 的版本都会锁定为1.0.0。

示例2:项目需要增加一个 pod D,在 Podfile 文件中引入D,这里应该使用 pod install。因为只是想增加D,而不想更新其他三个库。

示例3:这时假如团队其他成员加入这个项目,克隆了项目仓库,然后执行 'pod install'。由于Podfile.lock被提交了,所以能确保新成员使用的库版本和我的是一样的。即使 pod A 有'1.1.0' 版本可以使用,因为 Podfile.lock中锁定的Pod A的版本是1.0.0。

示例4:检查版本更新,如果想更新 pod A到1.1.0版本,而 pod B也有新版本但是不想更新 pod B,这个时候就使用 pod update A就可以了,这个时候 Podfile.lock 中 pod B的版本信息也会更新。

Note : Using a fixed version of the Podfile, such as pod 'A', '~> 1.1.0', does this ensure that all members of the project are using the same version? The answer is no. When executing pod update, only the version of 'A' can be guaranteed to be fixed, but if 'A' still depends on pod A1, when classmate A pulls, the version of A1 is 1.0.0, and when B When students pull, the latest version of pod A1 is '1.1.0', which will cause the version of A1 to be '1.1.0'. That's why make sure each member of the team uses Podfile.lock when using the only way to use the same version on different computers and use pod install and pod update appropriately.

The logical relationship of version numbers

'> 0.1' --- 版本号大于0.1
‘>= 0.1’ --- 版本0.1和版本号大于0.1'< 0.1' --- 版本号小于0.1
‘<= 0.1' --- 版本号0.1和版本号小于0.1的
复制代码

Best match for version numbers

‘~> 0.1.2' --- 版本0.1.2和版本号处于0.1.2-0.2之间的,不包括0.2和更高版本
‘~> 0.1' --- 版本0.1和版本号处于0.1-1.0之间的,不包括1.0和更高版本
‘~> 0' --- 版本0和更高,和没设没啥区别
复制代码

Summarize

This article introduces whether podfile.lockto commit git commit or not, what the role of podfile.lock is, and how to resolve conflicts. What is the difference between pod install and pod update.

Reference article:

www.jianshu.com/p/52c5035c9…

www.jianshu.com/p/2dc97f9a6…

Guess you like

Origin juejin.im/post/7080333088186138661