How to clean project before each build? - xcode

Is there a way (possibly using schemes) in Xcode to specify that a clean is automatically done before doing a new build.?
I have a project that sometimes fails to build unless I do a clean first, currently I am doing it by hand.

Press ⌥⌘R, expand the selected scheme, select Pre-actions, click +, select New Run Script Action, set Provide Build Settings from to your target. In the box below type rm -rf ${BUILT_PRODUCTS_DIR}. Note: it is BUILT not BUILD as seen in the Xcode dialog. You can type echo ${BUILT_PRODUCTS_DIR} > ~/Desktop/log.txt to see what's going to be deleted.

The selected answer did not work for me, it caused my build to fail (Xcode 4.6.3) when trying to run on the simulator.
Based on Jano's answer and on this link in the Pre-action script instead of writing
rm -rf ${BUILT_PRODUCTS_DIR}
I wrote
touch ${BUILT_PRODUCTS_DIR}
This should have the same effect and it doesn't cause my build to fail

Related

SwiftUI: Automatic preview updating paused, always

I have an existing App, basically a shopping list app, to which I'm trying to add some sweet sweet SwiftUI lovin.
My issue is the real time preview updating doesn't work - the warning "Automatic preview updating paused" continually shows. I hit the resume button, it builds the app, it shows the current view, and that warning immediately shows again. I can never see changes to the code reflected in the canvas without using the resume button.
This is happening in Xcode 11.1, and 11.2 beta 2. I can find literally no other mention of this either here on SO, and there's one thread with no answers on Apple's Dev forums.
If you're having custom Run Script Phases in Build Phases and you don't want (or can't) remove them, then try to check checkbox "Run script only when installing".
The problem with all the given answers is that you need to check or uncheck your script in debug mode if you want to make the preview work.
Here is a convenient alternative using the environment variables.
This is really simple
Embed all the content of your script in an if statement that check if we're using the preview or not. If we're in preview, then don't run the content of your script, otherwise, let's run it. And you don't have to sacrifice your script for release versions only.
Here is the template :
if [ $ENABLE_PREVIEWS == "NO" ]
then
# your code to execute here
else
echo "Skipping the script because of preview mode"
fi
And below a full example that I use to bump my build version number
# xcode-build-bump.sh
# #desc Auto-increment the build number every time the project is run.
# #usage
# 1. Select: your Target in Xcode
# 2. Select: Build Phases Tab
# 3. Select: Add Build Phase -> Add Run Script
# 4. Paste code below in to new "Run Script" section
# 5. Drag the "Run Script" below "Link Binaries With Libraries"
# 6. Insure that your starting build number is set to a whole integer and not a float (e.g. 1, not 1.0)
if [ $ENABLE_PREVIEWS == "NO" ]
then
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"
else
echo "Skipping Bump of version"
echo $ENABLE_PREVIEWS
fi
I ended up sending in feedback to Apple, and they responded with a fix. I have a build script in the target that auto-increments the build number. If I remove that script then previewing works as intended.
So if you're having this issue remove anything in Target -> Build Phases -> Run Script and try again. The canvas preview should update as you would expect.
For me, Canvas did not work when I had Legacy Build System.
You can change it via,
File -> Project Settings (or Workspace Settings) -> Build System -> Choose "New Build System(Default).
As it says, it is the default option. If for any reason Legacy build system was chosen, Canvas won't work.
Edit on June 30, 2020:
We no longer have Legacy Build System in Xcode 12 beta.
If you use Xcode 13 or 14 with custom scripts, you must ensure that the script for install builds only is checked in.
What worked for me was to "clean" Xcode
On the Mac
Open Xcode
command+k (Clean console)
command+option+k (Reload console)
command+option+shift+k (Clean build folder)
Exit Xcode
From a terminal window, clean the derived data. I run the following based on where my Xcode is installed. I believe its the base location
rm -rf ~/Library/Developer/Xcode/DerivedData
Re-open xcode and it worked great!
In my experiements I found that ENABLE_PREVIEWS is always set to YES in a SwiftUI project. Instead I found that in normal builds Xcode sets TARGET_DEVICE_MODEL and in SwiftUI it does not.
So the solution is like the one described in this answer: https://stackoverflow.com/a/62216533/833197 but using a different variable.
On another note, setting anything in the Info.plist in a build script seems to be "too late" in recent Xcode versions. It will not be used until next build. Also you end up with a modified version control working copy of your files which might not be what you want.
To resolve this I have
Used a pre-build script in the build scheme instead
Generated a xcconfig with the build number and made it ignored by the version control system (git in my case).
The variables set in a xcconfig file can be referenced in the Info.plist file.
It's strange. But for me automatic preview always fails when I name projects using digits only (i. e. "111"). When naming using letters (with or without digits), everything is ok. 12.3 beta (12C5020f), Big Sur beta 11.1 (20C5048k).
For me it was because i had a pre-actions script in the Build section in the Edit Scheme screen, removing this script got the preview to work as intended

How can I see log of commands Xcode executed when I press `build` on Xcode?

I want to see the commands Xcode executed when I press "Build" on Xcode.
Those commands should be like xcodebuild and such.
I tried to find it at Xcode > View > Navigators > Show Report Navigator (related post), but I can not find the keyword xcodebuild inside. I want to see the parameters Xcode used for xcodebuild.
Am I able to do that?
Apparently, Xcode doesn't use xcodebuild in the build process. (If you think about it, the xcodebuild command is user-facing and part of the Xcode command-line tools, but those aren't required to be present to build and/or run something in it.) Here's the entirety of this very short answer:
You can't. Xcode itself doesn't invoke xcodebuild during the build process. This post has more information on executing xcodebuild to build for the simulator.

xcode 6 beta 7:A signed resource has been added modified or deleted.

When I'm running the application on device aftercleaning, removing derived data, first time it runs without any issues.
Second time when I'm trying to run, it say's "A signed resource has been added modified or deleted."
On the simulator app run's without problem.
I think it's because of extensions, I have editing, share and today extensions, when I'm deleting this extensions, then I can run second time without this message.
The problem has been since from xcode 6 beta 5.
Maybe issue related to provisioning profiles and signing ? I has created different provisioning profiles for each extension target.
So my question is, how can I fix this ? How can I run on the device second time without cleaning project?
It is still not fixed in XCode 6 RTM. But I found an easy walk around. Simply delete the ShareExtension.appex folder under your build. Then rebuild again. You don't need to clean project, so rebuild is quite fast.
A actually alias the command to do the cleaning
rm -rf ~/Library/Developer/Xcode/DerivedData/<your_app>-*/Build/Products/Debug-iphoneos/ShareExtension.appex/
I used a slight modification of Cloud Xu's script to delete both the .appex and .appex.dSYM
rm -rf ~/Library/Developer/Xcode/DerivedData/YourAppName-*/Build/Products/Debug-iphoneos/com.yourcompany.Name.extension.*
You can put this in your scheme so that it executes with every build:
Edit scheme... > Expand the Run mode in the sidebar > Pre-actions > Click '+' > New Run Script Action.
Edit:
There is an another workaround: for each extension target containing .swift file, add build pre-action in project running scheme configuration:
touch "${PROJECT_DIR}/SOME SWIFT FILE IN EXTENSION.SWIFT"
So I've found a workaround for this issue. For now we can't have swift code in extensions. As mentioned in the comment
When removed all extension targets that contains swift code it's started working normally. I think it's a bug in xcode, for now if we have extension with swift code, don't know why, but second time run gives "A signed resource has been added modified or deleted." error.

Fixing file 'project.pch' has been modified since the precompiled header was built error in Xcode

I was recently working on my application messing around in the info.plist section, and since that my application will not run on my test device:
file 'project.pch' has been modified since the precompiled header was built
Something to note is that the app runs fine in the simulator.
Edit: Now I am getting this error instead of the other one:
No such file or directory (/Users/Me/Library/Developer/Xcode/DerivedData/MyProject-abcdefghijklmnopqrstuvwxyz/Build/Products/Debug-iphoneos/MyApp./MyApp)
How to regenerate the info.plist file?
You could try a deep clean (not the same as Product > Clean) - Option+Command+Shift+K
Note: this means the clean the build folder (by pressing Option + Product -> clean folder)
Close your project or workspace.
In Finder: ⇧shift+⌘cmd+G
Paste: ~/Library/Developer/Xcode/DerivedData/
Delete the ModuleCache folder and empty trash.
Open up your project.
Clean: ⇧shift+⌘cmd+K
Build: ⌘cmd+B
In my case the error message had a small hint:
note: after modifying system headers, please delete the module cache
at
'~/Library/Developer/Xcode/DerivedData/ModuleCache/5CYAJ91AZCB7'
I tried and it worked.
Simply touching the project's pch file solved this issue it for me :
touch Test.pch
Product -> Clean
Product + Option -> Clean Build Folder
Close Project or Workspace
Clean 'ModuleCache' path in Finder.
Worked for me in Xcode6 GM and iOS 8.0.
When the .h file of a Library Provided with Xcode is changed (even a space), this error could occur.
The following maybe tried in order to fix this issue
In, Project -> Build Settings, change Precompile Prefix Header to NO
OR
Run rm -rf ~/Library/Developer/Xcode/DerivedData and Clean Build
I constantly had this since upgrading from Xcode 4.6 to Xcode 5 with iOS projects when using command line build (xcodebuild).
What worked for me is to use this as command line build command:
xcodebuild -project path/to/my.xcodeproj -configuration Debug clean build
Before this, I've tried:
Clean the project in Xcode GUI.
Delete the offending pch file.
Delete the offending pch file's parent folder.
Delete the DerivedData project folder.
None of the above worked for me under Xcode 5.0.2, Mavericks, iOS7.
Note that the problem didn't happen to me when using Xcode GUI.
I just go to directory "/$HOME/Library/Developer/Xcode/DerivedData/", delete everything there
Just Click Product from the menu and select Clean
Product>Clean
Worked fine for me.
I got the same problem. My error said like I need to delete the cache in the below locaition,
Users/myusername/Library/Developer/Xcode/DerivedData/ModuleCache/3FGETKFCU0N0W
3FGETKFCU0N0W was a folder, when I deleted it and then clean build the project. Then the error disappears.
rm -rf ~/Library/Developer/Xcode/DerivedData/
If you use AppCode you can try remove content form:
/Users/[Username]/Library/Caches/appCode30/DerivedData/
Worked for me :)
Also, just open terminal, and delete the .pcm file that has been generated (Xcode 6 onwards)
rm <path-to-pcm-file>
If you get this when switching between git branches, try adding this post-checkout hook which will clean your project every time you change branch.
!/bin/sh
xcodebuild clean -configuration Debug
Save as .git/hooks/post-checkout relative to your project root and don't forget chmod +x .git/hooks/post-checkout
For me, this problem is only occurring in Xcode 6 beta version, So I switch back to Xcode 5, and now problem is solved.
I tried all the steps suggested by all users here, but no one worked for me.
So, if you have both versions of Xcode and have issue only in Xcode 6 beta, then switch to Xcode 5 and clean the project, problem will be solved.
I tried these one by one but failed to build and run my project. So I changed Precompile Prefix Header to NO as mentioned by #Abhilash Gopal, then I reset the simulator,deleted the derived data, removed the corresponding .pcm file(In my case it was the UIImage.h from UIKit framework, so removed the UIKit.pcm).Then I cleaned the project and only then I was able to build it successfully. In earlier versions of Xcode these files were locked basically and were not allowed to edit. But its not the case now. Hope this helps someone who face the same situation.
The only solution there worked for me was running the following command:
sudo rm -R /var/folders/
But that gave me a lot of troubles in OS X.
I thought because /var/folders/ is for temporary files it should be okay to just empty anything in it.
But I was wrong see the following post: link
And the problem you described occurred every-time I tried to build my Xcode project - I'm building it from the CLI though Jenkins.
Which means I couldn't run the rm command every-time. So I found some inspirations and wrote the following ruby script which solved my problem:
#!/usr/bin/env ruby
require 'fileutils'
cache = Dir.glob("/var/folders/**/com.apple.DeveloperTools*")
FileUtils.rm_rf(cache)
I hope it would be helpful to someone.
Deleting project.xcworkspace did a trick for me.
project.xcworkspace is a directory which describes schema of current Xcode workspace state. Each time Xcode is launched it will regenerate project.xcworkspace if not exist already.
In order to delete project.xcworkspace:
Right click on your project file yourProject.xcodeproj
From drop-down menu choose Show Package Content
Purge xcworkspace file
If you are building on command line using xcodebuild remove '-parallelizeTargets' option passed to the command. That fixed the issue for me
After going through a lot of answers and helpful suggestions, I found out the solution to be: Uninstall Xcode 6 and reinstall it through the available dmg file and then install all the components for Xcode.
Product + Option -> Clean Build Folder, Clean Build Project, Deleting Module Cache files and Derived data files, using touch .pch, deleting pcm file, using command line to remove var/folders (Surprisingly, all of this did not help in my case)
Was stuck at it for more than 3 hours before finding the solution. Hope this helps someone.
Thanks.
Just open terminal and fire below command:
rm /Users/pratik/Library/Developer/Xcode/DerivedData/ModuleCache/LKL48GNAN6S7/UIKit-2X95M2Q1NPNPL.pcm
give your file path which return .pcm error. and then Clean Project and Run...Enjoy!!!

How to run an xcode project from bash?

I have a build script calling xcodebuild. that works, but I want to also run the project from bash as well. Effectively I want to negate the need to click "Build and Run" button from the GUI. I'm looking at xcrun but it's not too obvious to me what to do
It sounds like you want to run the product of the build, not the project itself. If you want to do that, you just need to use the bash invocation for the product. If you're building a command-line program, then there will be an executable with the product's name in the project's built products directory after a succesful build. The project's built products directory depends on your preferences for Xcode and the project, but can be determined from the $BUILT_PRODUCTS_DIR environment variable within an Xcode build phase.
I do this for one of my projects in a shell script. It let's me remotely build over SSH.
xcodebuild clean
xcodebuild
cp -rp ~/Projects/VSM/Mac/iCar/build/Release/iCar.app ~/Desktop/
open ~/Desktop/iCar.app
I chose to copy the app to the Desktop on purpose but you wouldn't have to.
try open xcode

Resources