CodeSign error: a valid provisioning profile is required for product type 'Application' in SDK 'Device - iPhone OS 2.0'

Lately I've been working on adding some improvements to my Hiragana iPhone application, which I expect to submit to the App Store any day now.

This post, however, is not about amazing new features. Instead it's about a problem I ran into when compiling for Ad Hoc Distribution. Instead of the expected Succeeded message Xcode gave me the error message CodeSign error: a valid provisioning profile is required for product type 'Application' in SDK 'Device - iPhone OS 2.0'.

None of the classic tricks of restarting Xcode and cleaning the build had any effect. After banging my head against the screen for a while (that iMac screen is surprisingly sturdy) I was finally able to figure out that my project file wasn't set up to use the provisioning file I had installed on the system.
Since last compiling the application I have added a few new devices to my Ad Hoc provisioning profile. This appears to have surprised Xcode somewhat since it did not update the project file with the new provisioning identifier.

Here are the steps that worked for me, perhaps they'll come in handy for someone else as well:

1. Go to ~/Library/MobileDevices/Provisioning Profiles and open your Ad Hoc provisioning file in a texteditor, I used Smultron.
2. Find the section that looks like this
3. Copy the identifier string.
4. Go to you project directory and make a backup copy of your MyProject.xcodeproj file.
5. In the Finder right click and select Show Package Contents on your MyProject.xcodeproj file.
6. Open the project.pbxproj file with your text editor. 
7. Find all rows containing "PROVISIONING_PROFILE[sdk=iphoneos*]" = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; that appear in an Ad Hoc section.
8. Replace the assigned value with the provisioning file's identifier, which you copied earlier.
9. Save and launch Xcode to try to build.

This worked for me. I hope it works for you if you run into the same problem.