Duplicate and rename Xcode project & associated folders
Asked Answered
S

9

206

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.

Sixtyfourmo answered 19/7, 2013 at 10:50 Comment(0)
S
392

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

  1. 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.

  2. 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.

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

  4. 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:

  1. 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. Note: You may have to do Step 2 first, then come back to this step.

  2. 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.

  3. 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

  1. 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.

  2. 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.

  3. 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.

Sixtyfourmo answered 19/7, 2013 at 10:50 Comment(26)
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)Lacquer
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.Posy
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!Posy
Note: Deleting the Derived Data 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)Posy
As a curiosity, here's kind of an old school way to do this ... #10006982 (Daniel's answer)Edda
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". https://mcmap.net/q/45592/-how-can-i-change-the-name-of-an-ios-app-in-xcodeNature
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 ?Overestimate
[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)Sixtyfourmo
I am having a problem when running. Getting an 'NSInternalInconsistencyException', reason:'Could not load NIB in bundle 'NSBundle </var/mobile/Applications/66201934-AFC6-488C-8361-0FB1547A79D3/newname.app> (loaded)' with name 'newname'' Has anyone else seen this? I renamed my storyboard to match the new project name.Oina
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.Oina
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.Implication
This was helpful, but setting a new scheme also helped me (found in comments on this answer) #19443367Phytogeography
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.3Toffey
Xcode 7.1.1 crashes for me at the first hurdle - renaming the project. Will investigate, but has anyone else found this?Lorsung
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.Shammy
"Manage Schemes" is in: Menu bar > Project > Scheme > Manage Schemes (Xcode 7.2.1)Shammy
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 @LorsungEduardo
It's missing how to handle when you have Cocoapods integration in the project. I found a helpful link to handle that as well: h4ckish.com/2015/01/22/renaming-your-cocoapods-ios-projectWhitebook
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 #25338928Widera
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.Fantastically
I got stuck on the first step - 'rename the project' where is that functionality? Couldn't find it in the left pane.Frown
changing 'Framework Search Paths' helped me in Xcode 9.3Hamite
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)Sielen
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 perfectlyBergeron
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.Lycanthropy
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? ThanksMenstruate
S
33

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

Slew answered 18/3, 2014 at 14:51 Comment(4)
The question was for duplicating and then renaming the copied project. This answer is just for renaming an existing project.Spitsbergen
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 (nshipster.com/xcode-plugins)?Bankroll
Only method that worked for me - XCode 8.2Fantastically
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.Internee
S
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

Sixteenth answered 6/11, 2015 at 16:35 Comment(2)
This only works to rename the internals of the app, but externally the folders for the app remain unchangedViscosity
xCode 11 link: help.apple.com/xcode/mac/11.4/#/dev3db3afe4fRowe
D
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.

Deepseated answered 14/9, 2014 at 12:35 Comment(0)
M
2

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)
Milewski answered 4/3, 2017 at 23:57 Comment(1)
Year 2021 AD. Big Sur 11.6. Xcode 13.0 beta 5. Works like a charm.Inextricable
T
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

Tollhouse answered 2/6, 2016 at 14:13 Comment(0)
G
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
Gamo answered 5/5, 2015 at 12:19 Comment(0)
J
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!

Josefina answered 2/6, 2015 at 22:20 Comment(0)
S
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.

Sauveur answered 1/10, 2015 at 3:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.