本文为学习记录所作,内容来源于玉令天下的博客-Let's Talk About project.pbxproj
概述
在此之前对于project.pbxproj
这个文件只停留在git出文件冲突时, 使用Xcode打开它, 处理一下冲突并保存完事. 后来做项目混淆时, 其中一步是修改文件名, 而修改文件名之后原本Xcode中的引用就变成大红色了. 而之前的做法是, 删除引用并重新把改名后的文件拖进来, 因为文件多, Xcode需要加载好长时间. 为了解决这个问题, 故学习和使用到这个文件.
文件路径
项目名.xcodeproj-> 右击显示包内容 -> project.pbxproj
文件格式:
它本质上是一种旧风格的plist文件, 与如今Xcode中使用plist文件格式不同(现在新的plist文件采用xml格式), 它这样表示一个数组:
( "1", "2", "3" )
它这样表示一个字典:
{
"key" = "value";
...
}
内容规则
project.pbxproj
使用 UUID 作为交叉引用的索引, 保证每个配置信息对象的唯一性. 因为 UUID 猜测是根据机器硬件和时间戳等要素生成, 避免了多人在同一时间段操作修改工程文件带来的问题. 也就是说工程中每项配置对象(文件,目录,配置表等等)都有个唯一的 UUID, 然后其他配置对象想引用某个配置对象直接使用它的 UUID 即可. 这就跟我们编程时使用指针指向某个对象的地址一样, 其他对象的属性想引用它, 只需要给属性传个指针地址就行了.
整体内容格式
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
....
keys = values;(to much to show here)
....
};
rootObject = C5A75D711F5E7D2E000191E4 /* Project object */;
}
其中重要的 objects
和 rootObject
, 因为其它几个基本固定不变的. 所有的配置对象都放在objects
对应的 value
中, 包括根对象rootObject
. objects
对应的 value
也是一个字典, key
都为 UUID, value
依然是个字典. 读懂 project.pbxproj
的最好方式就是顺着 rootObject
的各个属性对应的 UUID 在 objects
中找到对应的对象, 然后一层层看下去. 这样整个文件的配置信息存放方式就慢慢摸清了.
配置信息对象的类型
objects
的键值对根据内容类型被分成了若干个section
, section
的数量和种类跟工程有关, 根据工程的配置多少而不同, 下面列出了一个section 列表(非完整):
PBXBuildFile
PBXBuildPhase
PBXAppleScriptBuildPhase
PBXCopyFilesBuildPhase
PBXFrameworksBuildPhase
PBXHeadersBuildPhase
PBXResourcesBuildPhase
PBXShellScriptBuildPhase
PBXSourcesBuildPhase
PBXContainerItemProxy
PBXFileElement
PBXFileReference
PBXGroup
PBXVariantGroup
PBXTarget
PBXAggregateTarget
PBXLegacyTarget
PBXNativeTarget
PBXProject
PBXTargetDependency
XCBuildConfiguration
XCConfigurationList
而每个section
/* Begin xxxx section */ (以这种注释开始)
...
/* End xxxx section */ (以这种注释结束)
一个section里面的内容类型是相同的. 而类型可以从isa这个key对应的value看出来. 比如项目中所有文件的引用是一种类型,
叫做 PBXFileReference
. 我新建了一个项目, 随意拖入的一些文件.
而在objects
中的表现如下:
/* Begin PBXFileReference section */
C55BB9141F67BE1C00D17310 /* TwoPanScrollView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TwoPanScrollView.h; sourceTree = "<group>"; };
C55BB9151F67BE1C00D17310 /* TwoPanScrollView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TwoPanScrollView.m; sourceTree = "<group>"; };
C55FA2A220FDE9B7006BBB41 /* Person.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Person.h; sourceTree = "<group>"; };
C55FA2A320FDE9B7006BBB41 /* Person.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Person.m; sourceTree = "<group>"; };
C5616BC21FA858C000A9C033 /* UIView+Toast.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+Toast.h"; sourceTree = "<group>"; };
C5616BC31FA858C000A9C033 /* UIView+Toast.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+Toast.m"; sourceTree = "<group>"; };
C57DC7251F67FAF600106DF4 /* SecondViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SecondViewController.h; sourceTree = "<group>"; };
C57DC7261F67FAF600106DF4 /* SecondViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SecondViewController.m; sourceTree = "<group>"; };
C5A75D791F5E7D2E000191E4 /* 学习之路.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "学习之路.app"; sourceTree = BUILT_PRODUCTS_DIR; };
C5A75D7D1F5E7D2E000191E4 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
C5A75D7F1F5E7D2E000191E4 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
C5A75D801F5E7D2E000191E4 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
C5A75D821F5E7D2E000191E4 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
C5A75D831F5E7D2E000191E4 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
C5A75D861F5E7D2E000191E4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
C5A75D881F5E7D2E000191E4 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
C5A75D8B1F5E7D2E000191E4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
C5A75D8D1F5E7D2E000191E4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C5A75D921F5E7D2E000191E4 /* 学习之路Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "学习之路Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
C5A75D961F5E7D2E000191E4 /* ____Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "____Tests.m"; sourceTree = "<group>"; };
C5A75D981F5E7D2E000191E4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C5A75D9D1F5E7D2E000191E4 /* 学习之路UITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "学习之路UITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
C5A75DA11F5E7D2E000191E4 /* ____UITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "____UITests.m"; sourceTree = "<group>"; };
C5A75DA31F5E7D2E000191E4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
/* End PBXFileReference section */
再比如项目中所有文件的分组是一种类型, 叫做 PBXGroup
. 如下:
/* Begin PBXGroup section */
C5616BC11FA858C000A9C033 /* Toast */ = {
isa = PBXGroup;
children = (
C5616BC21FA858C000A9C033 /* UIView+Toast.h */,
C5616BC31FA858C000A9C033 /* UIView+Toast.m */,
);
path = Toast;
sourceTree = "<group>";
};
C57EAA11212D02A4003D38CD /* TwoPanScrollView */ = {
isa = PBXGroup;
children = (
C55BB9141F67BE1C00D17310 /* TwoPanScrollView.h */,
C55BB9151F67BE1C00D17310 /* TwoPanScrollView.m */,
);
path = TwoPanScrollView;
sourceTree = "<group>";
};
C57EAA12212D02D7003D38CD /* Class */ = {
isa = PBXGroup;
children = (
C5A75D7F1F5E7D2E000191E4 /* AppDelegate.h */,
C5A75D801F5E7D2E000191E4 /* AppDelegate.m */,
C5A75D821F5E7D2E000191E4 /* ViewController.h */,
C5A75D831F5E7D2E000191E4 /* ViewController.m */,
C57DC7251F67FAF600106DF4 /* SecondViewController.h */,
C57DC7261F67FAF600106DF4 /* SecondViewController.m */,
C55FA2A220FDE9B7006BBB41 /* Person.h */,
C55FA2A320FDE9B7006BBB41 /* Person.m */,
C5616BC11FA858C000A9C033 /* Toast */,
C57EAA11212D02A4003D38CD /* TwoPanScrollView */,
);
path = Class;
sourceTree = "<group>";
};
C5A75D701F5E7D2E000191E4 = {
isa = PBXGroup;
children = (
C5A75D7B1F5E7D2E000191E4 /* 学习之路 */,
C5A75D951F5E7D2E000191E4 /* 学习之路Tests */,
C5A75DA01F5E7D2E000191E4 /* 学习之路UITests */,
C5A75D7A1F5E7D2E000191E4 /* Products */,
);
sourceTree = "<group>";
};
C5A75D7A1F5E7D2E000191E4 /* Products */ = {
isa = PBXGroup;
children = (
C5A75D791F5E7D2E000191E4 /* 学习之路.app */,
C5A75D921F5E7D2E000191E4 /* 学习之路Tests.xctest */,
C5A75D9D1F5E7D2E000191E4 /* 学习之路UITests.xctest */,
);
name = Products;
sourceTree = "<group>";
};
C5A75D7B1F5E7D2E000191E4 /* 学习之路 */ = {
isa = PBXGroup;
children = (
C57EAA12212D02D7003D38CD /* Class */,
C5A75D851F5E7D2E000191E4 /* Main.storyboard */,
C5A75D881F5E7D2E000191E4 /* Assets.xcassets */,
C5A75D8A1F5E7D2E000191E4 /* LaunchScreen.storyboard */,
C5A75D8D1F5E7D2E000191E4 /* Info.plist */,
C5A75D7C1F5E7D2E000191E4 /* Supporting Files */,
);
path = "学习之路";
sourceTree = "<group>";
};
C5A75D7C1F5E7D2E000191E4 /* Supporting Files */ = {
isa = PBXGroup;
children = (
C5A75D7D1F5E7D2E000191E4 /* main.m */,
);
name = "Supporting Files";
sourceTree = "<group>";
};
C5A75D951F5E7D2E000191E4 /* 学习之路Tests */ = {
isa = PBXGroup;
children = (
C5A75D961F5E7D2E000191E4 /* ____Tests.m */,
C5A75D981F5E7D2E000191E4 /* Info.plist */,
);
path = "学习之路Tests";
sourceTree = "<group>";
};
C5A75DA01F5E7D2E000191E4 /* 学习之路UITests */ = {
isa = PBXGroup;
children = (
C5A75DA11F5E7D2E000191E4 /* ____UITests.m */,
C5A75DA31F5E7D2E000191E4 /* Info.plist */,
);
path = "学习之路UITests";
sourceTree = "<group>";
};
/* End PBXGroup section */
更多配置对象属性和类型以及含义可以参照这篇文章提供的对照表:Xcode Project File Format英文 翻译版:中文翻译
运用
- 我们在使用
Cocoapods
时发现它可以更改Xcode工程目录和一些配置信息, 这其实就是Cocoapods
通过它的组件Xcodeproj
来对工程结构进行修改, 其中就包括读取project.pbxproj
信息, 进行相关的修改后再存入. 完成自动化导入第三方库. - 一些自动化打包工具也会通过读写
project.pbxproj
中的信息完成对证书的动态修改. - 简单实践一个例子. 比如我们将
Person.h
和Person.m
show in finder, 将他们的文件名称修改为FTPerson.h
和FTPerson.m
. 回到Xcode会发现原来的引用已经无效了.
这时候我们可以直接打开project.pbxproj
, 全局搜索Person
并替换为FTPerson
, 保存后发现Xcode中的引用恢复了正常. 这个也正是我在编写脚本实现项目中所有文件改名后, 再通过修改project.pbxproj
中的引用信息从而跳过了删除引用再添加引用的这个耗时操作的原理.
参考
http://www.cocoachina.com/ios/20170110/18549.html
http://yulingtianxia.com/blog/2016/09/28/Let-s-Talk-About-project-pbxproj/