.xcodeproj漫谈

.xcodeproj 是什么

project.pbxproj 文件被包含于 Xcode 工程文件 .xcodeproj 之中
,
.xcodeproj 是xcode用来组织工程项目的文件,它是一种旧风格的 Property List 文件,历史可追溯到 NeXT 的 OpenStep。
它用来组织我们工程中使用到的文件,各种工程配置信息等等.

怎么打开它?

有一款叫Plist Edit pro的软件可以打开它
当然我们也可以用xcode来打开,将一个project.pbxproj拖到一个工程中,右键选择 Open As - ASCII Property List方式打开


image.png

,这样会以对象的形式打开,便于我们观察.
当然,还可以利用plutil把它转换为json,xml,binary格式

plutil -convert json -o - project.pbxproj > abc.json

结构介绍

整体结构如下


image.png

这里你会看到每个对象都有一个24位的十六进制表示的标识符,这个标识符不仅是本工程中唯一,跨工程也是唯一的.我们无从知晓xcode本身是怎么生成这个唯一码的,不过不影响,我们可以给它任意的24位十六进制字符,只要它是唯一的就可以.

属性 类型 备注
archiveVersion String 1 目前看在所有的xcode中这个值都取1
classes Dictionary nil 一般都是空的
objectVersion String 50 48是xcode 8.0-compatible 50是xcode9.3-compatible 51是xcode10.0-compatible
objects Dictionary 成员
rootObject String root object的UUID 是objects中的某个成员的健值

objects 中的键值对被分成了若干个 section, section 的顺序是 Xcode 私有 API 钦定的,但每个 section 内部的键值对会根据 Key 的字典序排列,以此增加可读性。

内部都是通过UUID来进行引用的.从表面看,所有的对象都在objects下面,是一维的,但是,他们可以通过UUID来组成二维结构.

objectVersion的取值,会影响到objects中的object的类型,因为某些类型只有高级别的xcode才会支持.
常见的object类型有:

  • PBXBuildFile
  • PBXContainerItemProxy
  • PBXBuildPhase
    • PBXAppleScriptBuildPhase
    • PBXCopyFilesBuildPhase
    • PBXFrameworksBuildPhase
    • PBXHeadersBuildPhase
    • PBXResourcesBuildPhase
    • PBXShellScriptBuildPhase
    • PBXSourcesBuildPhase
  • PBXFileReference
  • PBXGroup
  • PBXTarget
    • PBXAggregateTarget
    • PBXLegacyTarget
    • PBXNativeTarget
  • PBXProject
  • PBXReferenceProxy
  • PBXVariantGroup
  • XCBuildConfiguration
  • XCConfigurationList

我们从可视化的角度来看待一个工程


image.png

这里对应的就是 PBXProject,它代表的就是我们平时看到的项目project.

Attribute Type Value Comment
reference UUID 24位16进制字符
isa PBXProject Empty
attributes Dictionary 工程的常用属性设置 有LastUpgradeCheck,ORGANIZATIONNAME,TargetAttributes等
buildConfigurationList Reference An element reference The object is a reference to a XCConfigurationList element.
compatibilityVersion String A string representation of the XcodeCompatibilityVersion.
developmentRegion String The region of development.
hasScannedForEncodings Number 0 or 1 Whether file encodings have been scanned.
knownRegions List A list of string The known regions for localized files.
mainGroup Reference An element reference The object is a reference to a PBXGroup element.
productRefGroup Reference An element reference The object is a reference to a PBXGroup element.
projectDirPath String The relative path of the project.
projectReferences Array of map Each map in the array contains two keys: ProductGroup and ProjectRef.
projectRoot String The relative root path of the project.
targets List A list of element reference The objects are a reference to a PBXTarget element.

示例如下

/* Begin PBXProject section */
        4CFBA7EB2099B5BC00E39A19 /* Project object */ = {
            isa = PBXProject;
            attributes = {
                LastUpgradeCheck = 0930;
                ORGANIZATIONNAME = pingan.inc;
                TargetAttributes = {
                    4C51184D2255E2DB00914224 = {
                        CreatedOnToolsVersion = 10.2;
                    };
                    4CE1AA532265BD64001EF020 = {
                        CreatedOnToolsVersion = 10.2;
                    };
                    4CFBA7F22099B5BC00E39A19 = {
                        CreatedOnToolsVersion = 9.3;
                    };
                    4CFBA80A2099B5BD00E39A19 = {
                        CreatedOnToolsVersion = 9.3;
                        TestTargetID = 4CFBA7F22099B5BC00E39A19;
                    };
                    4CFBA8152099B5BD00E39A19 = {
                        CreatedOnToolsVersion = 9.3;
                        TestTargetID = 4CFBA7F22099B5BC00E39A19;
                    };
                };
            };
            buildConfigurationList = 4CFBA7EE2099B5BC00E39A19 /* Build configuration list for PBXProject "DemoMain" */;
            compatibilityVersion = "Xcode 10.0";
            developmentRegion = en;
            hasScannedForEncodings = 0;
            knownRegions = (
                en,
                Base,
            );
            mainGroup = 4CFBA7EA2099B5BC00E39A19;
            productRefGroup = 4CFBA7F42099B5BC00E39A19 /* Products */;
            projectDirPath = "";
            projectReferences = (
                {
                    ProductGroup = AD433F0BCF3E4ED4D2C03BEB /* Products */;
                    ProjectRef = 24338332C0278FA0BF128891 /* YLBusiness1.xcodeproj */;
                },
                {
                    ProductGroup = 4C144E2B225598B50024F074 /* Products */;
                    ProjectRef = 4C144E2A225598B50024F074 /* YLBusiness2.xcodeproj */;
                },
                {
                    ProductGroup = 4C144E37225598BA0024F074 /* Products */;
                    ProjectRef = 4C144E36225598BA0024F074 /* YLBusiness3.xcodeproj */;
                },
                {
                    ProductGroup = 4C144E43225598BF0024F074 /* Products */;
                    ProjectRef = 4C144E42225598BF0024F074 /* YLBusiness4.xcodeproj */;
                },
            );
            projectRoot = "";
            targets = (
                4CFBA7F22099B5BC00E39A19 /* DemoMain */,
                4CFBA80A2099B5BD00E39A19 /* DemoMainTests */,
                4CFBA8152099B5BD00E39A19 /* DemoMainUITests */,
                4C51184D2255E2DB00914224 /* Hello */,
                4CE1AA532265BD64001EF020 /* LesGo */,
            );
        };
/* End PBXProject section */

其中的attributse中的TargetAttributes就是在图中的 工程的 targets列表.

还有个很常见的是 PBXGroup,这个是我们在工程组织中看到的group.也是我们看到的文件的组织结构.


image.png
/* Begin PBXGroup section */
        4C144E2B225598B50024F074 /* Products */ = {
            isa = PBXGroup;
            children = (
                4C144E31225598B50024F074 /* libBusiness2.a */,
                4C144E33225598B50024F074 /* Business2Tests.xctest */,
                4C144E35225598B50024F074 /* Business2Bundle.bundle */,
            );
            name = Products;
            sourceTree = "<group>";
        };
        4C144E37225598BA0024F074 /* Products */ = {
            isa = PBXGroup;
            children = (
                4C144E3D225598BA0024F074 /* libBusiness3.a */,
                4C144E3F225598BA0024F074 /* Business3Tests.xctest */,
                4C144E41225598BA0024F074 /* Business3Bundle.bundle */,
            );
            name = Products;
            sourceTree = "<group>";
        };
        4C144E43225598BF0024F074 /* Products */ = {
            isa = PBXGroup;
            children = (
                4C144E49225598BF0024F074 /* libBusiness4.a */,
                4C144E4B225598BF0024F074 /* Business4Tests.xctest */,
                4C144E4D225598BF0024F074 /* Business4Bundle.bundle */,
            );
            name = Products;
            sourceTree = "<group>";
        };
........太多了....省略
/* End PBXGroup section */
Attribute Type Value Comment
reference UUID A 96 bits identifier
isa PBXGroup Empty
children List A list of element reference The objects are a reference to a PBXFileElement element.
name String The filename.
sourceTree String 是个特定字符串 <absolute> :for absolute paths
<group> :for paths relative to the group SOURCE_ROOT: for paths relative to the project
DEVELOPER_DIR :for paths relative to the developer directory.
BUILT_PRODUCTS_DIR :for paths relative to the build products directory.
SDKROOT: for paths relative to the SDK directory.

这里的sourceTree的取值,在xcode中,是展示在最右边的属性中的


image.png

在group中的每一个文件,实际上都是一个 PBXFileReference对象,项目所引用的每一个外部文件,比如源代码文件、资源文件、库文件、生成目标文件等,每一个外部文件都可以在PBXFileReference中找到

/* Begin PBXFileReference section */
        24338332C0278FA0BF128891 /* YLBusiness1.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = YLBusiness1.xcodeproj; path = Modules/YLBusiness1/YLBusiness1.xcodeproj; sourceTree = "<group>"; };
        377280FB4B7932AA0F83F08F /* Pods-DemoMainTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoMainTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-DemoMainTests/Pods-DemoMainTests.debug.xcconfig"; sourceTree = "<group>"; };
        41DB0A36B9FFEBB516E4ABE7 /* libPods-DemoMainTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-DemoMainTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
.....太多了省略.....
/* End PBXFileReference section */
Attribute Type Value Comment
reference UUID A 96 bits identifier
isa PBXFileReference Empty
fileEncoding Number See the PBXFileEncoding enumeration.
explicitFileType String See the PBXFileType enumeration.
lastKnownFileType String See the PBXFileType enumeration.
name String The filename.
path String The path to the filename.
sourceTree String See the PBXSourceTree enumeration.

还有个 PBXContainerItemProxy,也是比较常见的,它是用来描述一个target的


image.png
/* Begin PBXContainerItemProxy section */
        3BCAE28295B22FEE4F9370FC /* PBXContainerItemProxy */ = {
            isa = PBXContainerItemProxy;
            containerPortal = 24338332C0278FA0BF128891 /* YLBusiness1.xcodeproj */;
            proxyType = 2;
            remoteGlobalIDString = 4CE211C720A0577E000137EA;
            remoteInfo = Business1Tests;
        };
        4030246F37A79E480733FAF4 /* PBXContainerItemProxy */ = {
            isa = PBXContainerItemProxy;
            containerPortal = 24338332C0278FA0BF128891 /* YLBusiness1.xcodeproj */;
            proxyType = 2;
            remoteGlobalIDString = 4CE211E620A0800C000137EA;
            remoteInfo = Business1Bundle;
        };
......很多,省略.......
/* End PBXContainerItemProxy section */
Attribute Type Value Comment
reference UUID A 96 bits identifier
isa PBXContainerItemProxy Empty
containerPortal Reference An element reference The object is a reference to a PBXProject element.,这个element可以是PBXFileReference,也可以是其它,看具体情况
proxyType Number 1
remoteGlobalIDString Reference An element reference A unique reference ID.比如,有子工程的时候,这个target来自于子工程的,这个值就是存在子工程中
remoteInfo String

PBXAggregateTarget,这个不太常见,这个通常是在我们创建.a或者.framework时候,添加的aggrate target


image.png

image.png
属性 类型 备注
reference UUID
buildConfigurationList Refrence nil reference to a XCConfigurationList element
buildPhases List nil The objects are a reference to a PBXBuildPhase element.
dependencies List 依赖的element列表 The objects are a reference to a PBXTargetDependency element.
name String targe的名称
productName String
/* Begin PBXAggregateTarget section */
        4CE1AA532265BD64001EF020 /* LesGo */ = {
            isa = PBXAggregateTarget;
            buildConfigurationList = 4CE1AA542265BD64001EF020 /* Build configuration list for PBXAggregateTarget "LesGo" */;
            buildPhases = (
            );
            dependencies = (
            );
            name = LesGo;
            productName = LesGo;
        };
/* End PBXAggregateTarget section */

怎么操作它?

最常用的,也就是cocoapods官方开源并使用的Xcodeproj库,在安装cocoapods的时候,已经安装在我们的电脑上了,如果没有安装过cocoapods,可以使用 gem install xcodeproj来安装
官方地址在Xcodeproj
它是一个ruby的库,需要了解基本的ruby语法才能操作.

当然,还有其他语言写的库,例如
OC写的操纵库 XcodeEditor

swift写的XcodeGen ,它还提供了通过撰写一个.yml文件的方式来帮你生成整个xcodeproj工程.使用 xcodegen 管理 iOS 项目对这个工具的使用有个简单的介绍

另外一个使用swift来操作的库是tuisttuist/xcodeproj,更新的很频繁,对swift版本感兴趣的可以看看这个

国内还有些写了一些很实用的工具,来导出xcodeproj文件的不同点,具体请看
pbxprojHelper ,这个是一个mac上的GUI工具,pbxprojHelper是其中文说明.

还有个python版本的
mod-pbxproj ,文档比较详细

nodejs版本的,Cordova 就是基于它管理 Xcode 工程的
cordova-node-xcode

哪里使用它?

  1. podfile文件的高级定制
  2. 组件化建立工程

备注:

其实还有个能够生成xcodeproj的夸平台工具 cmake

参考

xcode-project-file-format
Xcode工程文件project.pbxproj小结
Let-s-Talk-About-project-pbxproj

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容