译文:How to build multiple targets from a single Xcode Project

本文翻译自Omegasoft:http://www.omegasoftweb.com/omega/blog.cfm?p=43

How to build multiple targets (as in Lite Version and Full Version of an iPhone or iPad app) from a single Xcode Project

译文:怎样在单一XCode工程中创建多Targets

先挖了一个坑在这有时间过来填一下,文章有点老不如Xcode版本什么的,但是思路是一样的。翻译拙劣,望指正!

There are times you may find yourself needing to create multiple variations of an Xcode project, the most common example of which being a Lite Version and a Full Version of an iPhone app or iPad app. One option to accomplish this is to simply copy the project folder and rename it, working with each project separately from that point on. This is not a good idea, as now you have twice the work to manage things going forward, and confusions are bound to arise. My friend Smasher puts it succinctly: “in that path lies madness!”, and with this he is correct.

译文:有些时候你可能需要给你的公程创建一些变体,最常见的情形就是一个iOS App 的精简版和完整版。 一种方法是把这个工程拷贝一份然后重命名,然后分别处理。这当然不是一个好主意,因为现在你有了双倍的工作去做,而且紧接着会产生一大堆问题。我的朋友Smasher精辟的说:“这么做简直是疯了!”,很显然他是对的。

A much better plan is to configure your single Xcode project with multiple targets, so that you can develop multiple versions of the project concurrently and selectively build the one you need at a given moment. Here is a step by step guide to do just this, synthesized from the info in various threads at iphonedevsdk coupled with personal trial and error, and using the case of Lite Version / Full Version:

更好的方法是把你的公程设置成一个多Target的公程,这样一来你就可以同时开发不同的版本而且可以随时编译任意一个。以下就是一个分步的指导:

Step 1: Make a copy of your original target by right clicking the existing target and selecting “duplicate”. Rename this target to YourAppLite

Step1:在你现有的target上右键选择“duplicate”复制一份出来,然后重名成“YourAppLite”

Step 2: When you created the second target; a new pList file should have appeared in the resources folder YourApp-info_copy.pList or something similar; rename this to YourAppLite-info.pList

Step2:当你复制玩这个target后,你会发现一个新的plist文件。名字是“YourApp-info_copy.pList”或者是其他一些类似的名字,把它重命名成“YourAppLite-info.pList”

Step 3: Edit this new pList to have a different Bundle Identifier, com.yourcompany.YourAppLite, for example. If you do not do this both versions of your app will be seen as the same app instead of two unique ones, and when you build the light version it will overwrite your full version in the simulator or testing device.

Step3:编辑这个新的plist文件,修改Bundle Identifier,举个例子修改成“com.yourcompany.YourAppLite”。如果不修改那么这两个版本就还是同一个app而不是两个独特的app,当你编译这个精简版的时候就会覆盖你的模拟器或者真机设备上的完整版本。

Step 4: If you are going to use a different application icon for the light version (as you should), edit this new pList’s Icon file property to reference a different icon from the full version, IconLite.png for example. Obviously you must create this asset and add it to your project for this new icon to appear for the lite version.

Step 4:如果你想让你的精简版本适用不同的应用图标的话,就要编辑这个plist文件的Icon file属性指向一个新的图标。很显然你必须为你的精简版本图标建一个新的asset并添加到的你的工程当中。

Step 5: Highlight the new target (YourAppLite) and right click to “Get Info”, then navigate to the build tab and set Configuration to “All Configurations”. Now scroll down a bit until you find the “Info.plist file” parameter under the Packaging group; alter this to reference the new lite version pList we created above instead of the original pList.

Warning-Potential Gotcha:You may have noticed when you duplicated the target another product with the same name as the first appeared in the products folder. You may, like I was, be tempted to name the second one YourAppLite and change some properties in the pList to reflect this. Do NOT do this; there appears to be a bug which makes the debugger unable to launch the executable when you build. Furthermore, once you have done this even if you change the product name back it still gives the error. Perhaps this was just me being silly, but others have run into a similar issue; at any rate it is not necessary to change the product name for everything to work correctly, so I recommend leaving it alone.

—————————————-INTERMISSION—————————————

Now you should be all ready to built multiple targets, but there are still some things we want to do to make managing both branches as seamless as possible. Go grab a drink, then resume the adventure.

——————————————————————————————————-


Step 6: Once again highlight the new target (YourAppLite) and right click to “Get Info”, then navigate to the build tab and make sure Configuration is still on “All Configurations”. Now scroll down quite a ways until you find the “Other C Flags” parameter under the GCC Language group; alter this to be “-DLITE_VERSION” (the leading -D is necessary). What this does is allow you to do things like:

#ifdef LITE_VERSION

// Code unique to your lite version

#else

// Code unique to your full version

#endif

in your code, just like a normal if statement.

Step 7: For any project resources that only need to be in the lite version OR the full version, you can “Get Info” on the file and then navigate to the “Targets” tab, deselecting the unnecessary target. This will help to keep each respective bundle size down, and is especially useful for large packages like adMob (which you’d likely only want in your lite version). Keep in mind that removing assets from a target in this way will likely coincide with the type of branching code described in step 6.

Step 8 (Optional): If you want a separate Default.png file for your lite version, first create the file and name it DefaultLite.png and add it to your project along side the regular Default.png. Now, right click on your YourAppLite target, select Add –> New Build Phase –> New Run Script Build Phase and paste the following in the script field:

mv ${TARGET_BUILD_DIR}/${CONTENTS_FOLDER_PATH}/DefaultLite.png ${TARGET_BUILD_DIR}/${CONTENTS_FOLDER_PATH}/Default.png

Now close the window and expand the arrow next to YourAppLite target. You should see the run script you created, now grab it and move it just below “Copy Bundle Resources”. This will overwrite your Default.png with the DefaultLite.png at run time when you are building your lite version, just as you want.

Warning-Potential Weird Gotcha: Whenever I duplicate a target I notice that any Library Search Paths get slightly altered, so if you are using Library Search Paths and are getting strange errors, make sure you have the same values there in your Lite and Full version targets.

———————————————-Victory!——————————————–

And there you have it; multiple targets at your fingertips. Please feel free to leave some comments or point out any mistakes or gotchas. I hope you found this useful; good luck!

——————————————————————————————————-

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

推荐阅读更多精彩内容