I am getting this error:
CodeSign error: Certificate identity \'iPhone Developer: XXX\' appears more than once in the keychain. The codesign too
Quoting an answer to a duplicate of this question, some of Xcode's bugs in this area can only be solved by restarting OS X.
In the end, I achieved success by deleting everything mentioned in any of the answers to this question, restarting, then manually installing the parts I needed (I did not trust/try the automatic profile/certificate download feature).