198

I would like to be able to duplicate an entire Xcode project, rename the project and the associated files without Xcode going mental, and without having to manually re-import all files to the project.

Sammio2
  • 7,162
  • 7
  • 32
  • 48

9 Answers9

378

This answer is the culmination of various other StackOverflow posts and tutorials around the internet brought into one place for my future reference, and to help anyone else who may be facing the same issue. All credit is given for other answers at the end.

Duplicating an Xcode Project

  • In the Finder, duplicate the project folder to the desired location of your new project. Do not rename the .xcodeproj file name or any associated folders at this stage.

  • In Xcode, rename the project. Select your project from the navigator pane (left pane). In the Utilities pane (right pane) rename your project, Accept the changes Xcode proposes.

  • In Xcode, rename the schemes in "Manage Schemes", also rename any targets you may have.

  • If you're not using the default Bundle Identifier which contains the current PRODUCT_NAME at the end (so will update automatically), then change your Bundle Identifier to the new one you will be using for your duplicated project.

Renaming the source folder

So after following the above steps you should have a duplicated and renamed Xcode project that should build and compile successfully, however your source code folder will still be named as it was in the original project. This doesn't cause any compiler issues, but it's not the clearest file structure for people to navigate in SCM, etc. To rename this folder without breaking all your file links, follow these steps:

  • In the Finder, rename the source folder. This will break your project, because Xcode won't automatically detect the changes. All of your xcode file listings will lose their links with the actual files, so will all turn red.

  • In Xcode, click on the virtual folder which you renamed (This will likely be right at the top, just under your actual .xcodeproject) Rename this to match the name in the Finder, this won't fix anything and strictly isn't a required step but it's nice to have the file names matching.

  • In Xcode, Select the folder you just renamed in the navigation pane. Then in the Utilities pane (far right) click the icon that looks like dark grey folder, just underneath the 'Location' drop down menu. From here, navigate to your renamed folder in the finder and click 'Choose'. This will automagically re-associate all your files, and they should no longer appear red within the Xcode navigation pane.

Icon to click

  • In your project / targets build settings, search for the old folder name and manually rename any occurrences you find. Normally there is one for the prefix.pch and one for the info.plist, but there may be more.

  • If you are using any third party libraries (Testflight/Hockeyapp/etc) you will also need to search for 'Library Search Paths' and rename any occurrences of the old file name here too.

  • Repeat this process for any unit test source code folders your project may contain, the process is identical.

This should allow you to duplicate & rename an xcode project and all associated files without having to manually edit any xcode files, and risk messing things up.

Credits

Many thanks is given to Nick Lockwood, and Pauly Glott for providing the separate answers to this problem.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Sammio2
  • 7,162
  • 7
  • 32
  • 48
  • 3
    After following the above I just searched the hole project for "old name" and found what needed to be changed (Prefix header) and it compiled ok with "new name" and in another place. Great tutorial - just what I needed. (Tested w Xcode 5.0.1 / iOS 7.0) – Mac Oct 22 '13 at 15:23
  • This did not seem to get me all the way there (on Xcode 5.0.2 here, with a project that was migrated from 4.6.3 to 5.0.0). There were many things inside the `*.xcodeproj/project.pbxproj` file that contained the original folder path. In particular, the build was failing because it kept looking for an info.plist in the original folder. – Steven Lu Nov 23 '13 at 22:41
  • Actually, never mind, the info.plist was my mistake not following the last bullet in the first section (couldn't find it, found it now). But now there are issues with the build schemes it seems, it's looking for pch files in the wrong dir)... Seems like this got me most of the way there. Thanks! – Steven Lu Nov 23 '13 at 22:47
  • Note: Deleting the [Derived Data](http://stackoverflow.com/a/7284632/340947) folder will hopefully clear out any pch-related issues. These got created when I built the project after completing the steps halfway (and renaming the folder subsequently screwed it up) – Steven Lu Nov 23 '13 at 22:56
  • 2
    As a curiosity, here's kind of an old school way to do this ... http://stackoverflow.com/questions/10006482/how-to-rename-the-folder-name-in-xcode-project-with-out-corrupting-the-files (Daniel's answer) – Fattie Dec 06 '13 at 09:34
  • To change the name of the app, then you need to go to the info.plist and write the desired name in "bundle display name". http://stackoverflow.com/a/1960069/1250021 – AlvaroSantisteban Feb 06 '14 at 09:42
  • In my code I have a path to a local html file. I use NSBundle *thisBundle = [NSBundle mainBundle]; path = [thisBundle pathForResource:@"cgv" ofType:@"html"]; Unfortunately it still uses a path to my old source folder. What part did I miss ? – Nicolas Roy Apr 25 '14 at 09:09
  • `[NSBundle mainBundle]` will not be affected by this, as that is the bundle referenced at runtime once the build has been compiled. I'd suggest maybe cleaning your project, then cleaning the associated build folders too. (Hold Alt when showing the 'product' menu) – Sammio2 Apr 25 '14 at 21:23
  • I am having a problem when running. Getting an 'NSInternalInconsistencyException', reason:'Could not load NIB in bundle 'NSBundle (loaded)' with name 'newname'' Has anyone else seen this? I renamed my storyboard to match the new project name. – BeccaP Sep 18 '14 at 22:51
  • After searching, it turns out that somehow in my info.plist "Main Storyboard File Base Name" got changed to "Main NIB File Base Name". I'm posting this here in case someone else runs into this problem. Thank you for the instructions - everything else worked perfectly. – BeccaP Sep 19 '14 at 00:00
  • This helped me a lot. Thank you. I just wanted to add that I was able to rename the source folder before opening the duplicated project. There was no need to go back to the second step of your process because Xcode found the right containing folder on the initial launch of the project. – aasatt Dec 06 '14 at 18:33
  • This was helpful, but setting a new scheme also helped me (found in comments on this answer) http://stackoverflow.com/questions/19442867/how-do-i-rename-a-project-in-xcode-5 – deebs Jan 05 '15 at 18:38
  • I'm still getting a compiler error with the project supposedly not finding a file in derived data folder. Even after deleting derived data it still has the error. Any help? Xcode 6.3 iOS 8.3 – Ethan Parker Jun 02 '15 at 20:54
  • 1
    Xcode 7.1.1 crashes for me at the first hurdle - renaming the project. Will investigate, but has anyone else found this? – HughHughTeotl Dec 21 '15 at 16:32
  • One last step I had to do: I had old paths in Build Phases > Compile Sources. So I had to expand the folder in the navigation pane (left), then I saw the filenames (file links) in red. Then I followed the same steps to turn the red filenames to black text. Xcode 7.2.1. – Donn Lee May 06 '16 at 02:02
  • "Manage Schemes" is in: Menu bar > Project > Scheme > Manage Schemes (Xcode 7.2.1) – Donn Lee May 06 '16 at 02:03
  • Xcode 7.3.1. "In Xcode, click on the virtual folder which you renamed ..." AFAICT, Xcode did this for me automatically. The name of the project in the nav pane has changed. "In Xcode, Select the folder you just renamed ...". This crashes Xcode. see also comment by @HughHughTeotl – garyp Nov 04 '16 at 13:52
  • 5
    It's missing how to handle when you have Cocoapods integration in the project. I found a helpful link to handle that as well: https://h4ckish.com/2015/01/22/renaming-your-cocoapods-ios-project/ – K.K Nov 09 '16 at 12:01
  • Excellent! My app compiled and worked just fine, but I got hundreds of "missing file" warnings. It turns out you just need to stage or commit the deletions or else xcode complains. Thanks to http://stackoverflow.com/questions/25338428/xcode-issue-file-xxx-png-is-missing-from-working-copy-at-project-building – mpoisot Dec 15 '16 at 23:31
  • This didn't work for me - the magic "this should fix it" step did not result in the files all changing from red to black. I ended up having to edit .pbxproj as per @fs_tigre 's answer. – GreenAsJade Jan 03 '17 at 04:00
  • I got stuck on the first step - 'rename the project' where is that functionality? Couldn't find it in the left pane. – Joshua Dance Sep 16 '17 at 18:09
  • changing 'Framework Search Paths' helped me in Xcode 9.3 – canbax May 04 '18 at 08:04
  • Didn't really work for me with Xcode 9.4 - had to edit a few files, mostly Acme.xcodeproj/project.pbxproj Hard to believe Apple doesn't provide this out of the box in this day and age.. very M$esque, so much easier in the IntelliJ world with whichever JVM based build system (SBT/maven/gradle etc) – Peter vR Aug 08 '18 at 00:36
  • In addition in Xcode 10.1, I found I had to do a few more steps similar to above on the test folders and had to change the path of info.plist but for the rest it worked perfectly – Jeremy Andrews Jan 26 '19 at 15:13
  • In Xcode 10.3, I found that Xcode knew how to do most things. In fact, I went to do the step of renaming the source folder in the Finder, then in Xcode along with the re-pointing the virtual folder to the newly renamed one. This failed when I went to rename the virtual folder. I restored the original name and renamed the virtual folder, and Xcode renamed the actual folder in Finder. So while the answer is solid, this task has gotten a bit more automated. – cycollins Oct 18 '19 at 19:58
  • I've seen lots of instructions for copying projects, but what about workspaces including all its projects? I would think I need to copy all of its projects individually, then copy the workspace with all the copied projects in the same relative locations. Will the copied workspace point to the project copies? Thanks – ScottyB Jun 23 '20 at 12:55
32

I'm posting this since I have always been struggling when renaming a project in XCode.

Renaming the project is good and simple but this doesn't rename the source folder. Here is a step by step of what I have done that worked great in Xcode 4 and 5 thanks to the links below.

REF links:
Rename Project.
Rename Source Folder and other files.

1- Backup your project.

If you are using git, commit any changes, make a copy of the entire project folder and backup in time machine before making any changes (this step is not required but I highly recommended).

2- Open your project.

3- Slow double click or hit enter on the Project name (blue top icon) and rename it to whatever you like.

NOTE: After you rename the project and press ‘enter’ it will suggest to automatically change all project-name-related entries and will allow you to de-select some of them if you want. Select all of them and click ok.

4- Rename the Scheme

a) Click the menu right next to the stop button and select Manage Schemes.

b) Single-slow-click or hit enter on the old name scheme and rename it to whatever you like.

c) Click ok.

5 - Build and run to make sure it works.

NOTES: At this point all of the important project files should be renamed except the comments in the classes created when the project was created nor the source folder. Next we will rename the folder in the file system.

6- Close the project.

7- Rename the main and the source folder.

8- Right click the project bundle .xcodeproj file and select “Show Package Contents” from the context menu. Open the .pbxproj file with any text editor.

9- Search and replace any occurrence of the original folder name with the new folder name.

10- Save the file.

11- Open XCode project, test it.

12- Done.

EDIT 10/11/19:

There is a tool to rename projects in Xcode I haven't tried it enough to comment on it. https://github.com/appculture/xcode-project-renamer

fs_tigre
  • 9,459
  • 12
  • 53
  • 111
  • 2
    The question was for duplicating and then renaming the copied project. This answer is just for renaming an existing project. – martinjbaker Aug 31 '14 at 07:07
  • 1
    This, for me, is the simplest approach. Just copy the original project folder, e.g. MyProject, recursively to "MyNewProject". Then open MyNewProject and proceed with fs_tigre's instructions. I'm surprised that neither Android Studio nor Xcode provide a "File->Clone Project" option which would be so convenient. Maybe a nice plugin project for someone (http://nshipster.com/xcode-plugins/)? – Blisterpeanuts Feb 17 '16 at 20:21
  • Only method that worked for me - XCode 8.2 – GreenAsJade Jan 03 '17 at 03:59
  • 2
    Step 10.1 - If you have bridging header and/or entitlements files, rename those to reflect your new name. If using Cocoapods, edit the name in your pod file. – David Lari Apr 24 '18 at 12:40
6

As of XCode 7 this has become much easier.

Apple has documented the process on their site: https://developer.apple.com/library/ios/recipes/xcode_help-project_editor/RenamingaProject/RenamingaProject.html

Update: XCode 8 link: http://help.apple.com/xcode/mac/8.0/#/dev3db3afe4f

PBICS
  • 374
  • 2
  • 4
2

I am using this script after I rename my iOS Project. It helps to change the directories name and make the names in sync.

http://github.com/ytbryan/rename

NOTE: you will need to manually change the scheme's name.

Arsh Kaushal
  • 493
  • 1
  • 5
  • 17
ytbryan
  • 2,374
  • 24
  • 41
1

In the example code for the book "Instant OpenCV for iOS" I have found a bash script that copies a project from a folder to another.
Doing a little research I've found a blog post from what seems to be the original author of the script: http://mohrt.blogspot.it/2009/01/renaming-xcode-project-from-command.html, where you can download the script. I gave it a try and running it from terminal like this

    sh renameXcodeProject.sh <name-of-existing-folder> <name-of-folder-to-create>   

works fine.
Additional info can be found opening the file with a text editor. Hope that helps

Giorgio Tempesta
  • 928
  • 12
  • 21
0

I using Xcode 6+ and I just do:

  • Copy all files in project folders to new Folders (with new name).
  • Open *.xcodeproj or *.xcworkspace
  • Change name of Project.
  • Click on schema and delete current chema and add new one.

Here is done, but name of window Xcode and *.xcodeproj or *.xcworkspace still <old-name>. Then I do:

  • pop install
  • Open <new name>.xcworkspace
SLyHuy
  • 342
  • 3
  • 9
0

One more thing to try!

When I copied all of my files, opened the project, and renamed it, everything changed to my new project name except for the test target! I got a linker error that said I was missing a file called "myOldProjectname.app". Here's what fixed it:

  1. Click on your project settings and select your test target enter image description here

  2. Click on build settings and search for "test host" enter image description here

  3. Check those 2 file paths. Chances are that those 2 paths are still pointing at your old project name. enter image description here

Hope that helps!

erparker
  • 1,301
  • 10
  • 20
0

For anybody else having issues with storyboard crashes after copying your project, head over to Main.storyboard under Identity Inspector.

Next, check that your current module is the correct renamed module and not the old one.

Wraithseeker
  • 1,794
  • 1
  • 16
  • 33
0

I'm using simple BASH script for renaming.

Usage: ./rename.sh oldName newName

#!/bin/sh

OLDNAME=$1
NEWNAME=$2

export LC_CTYPE=C 
export LANG=C
find . -type f ! -path ".*/.*" -exec sed -i '' -e "s/${OLDNAME}/${NEWNAME}/g" {} +

mv "${OLDNAME}.xcodeproj" "${NEWNAME}.xcodeproj"
mv "${OLDNAME}" "${NEWNAME}"

Notes:

  1. This script will ignore all files like .git and .DS_Store
  2. Will not work if old name/new name contains spaces
  3. May not work if you use pods (not tested)
  4. Scheme name will not be changed (anyway project runs and compiles normally)
Anton Malmygin
  • 3,068
  • 1
  • 20
  • 28