12

I've been building iOS apps for > 2 years and never encountered this problem. I am attempting to archive an app for beta distribution. The build succeeds, but at the very end of the process, Xcode reports 'Archive Cancelled'.

enter image description here

The build logs show no warnings or errors of any kind.

enter image description here

Sometimes, by cleaning/cleaning build folder/wiping derived data, I can get the archive to succeed, but there seems to be no pattern to it. Has anyone encountered this issue? I don't even have anything to go on as far as an error message in this case.

Mark Struzinski
  • 32,945
  • 35
  • 107
  • 137
  • 1
    Never seen or heard of it. You are doing the right thing by cleaning project and etc. try reinstalling your xcode there might be an issue with the xcode. I know is a pain to do so but it might get rid of the issue once and for all. But that is the last resort. – Adrian P Jun 01 '13 at 05:00
  • Did you ever get this working? I'm having the exact same problem. Does your project use cocoapods? I'm wondering if that's the issue with mine. There is no other clue as to what's wrong :( – ConfusedNoob Jun 01 '13 at 17:06
  • I did think about reinstalling Xcode. Think I'm going to have to try it. I do use CocoaPods, but I have used CocoaPods on multiple projects in the past with absolutely no issues. – Mark Struzinski Jun 02 '13 at 02:11

8 Answers8

56

Do you have a custom Run Script that bumps the version with agvtool? I found this to cause this exact behavior. Almost 100% failure to archive, and common failure to build. Removing this fixed the issue for me.

ConfusedNoob
  • 9,826
  • 14
  • 64
  • 85
  • 5
    Wow, that was it. I was bumping my build # with a custom run script using AVGTool. Awesome catch! The killer was that there is no feedback in the logs telling you what is canceling the build. Thanks for the answer! – Mark Struzinski Jun 03 '13 at 18:26
  • 1
    aaaaahhhhhhhhhhh this one cost me hours. after automating builds and ipa packages and all the fancy stuff, the avgtool was killing the script. THX!!! – benjamin.ludwig Jul 16 '14 at 18:05
  • 3
    I believe what is happening is that the dependency system detects a change to source files (your plist file in this case) and aborts the build. – Buddhisthead Mar 12 '19 at 23:53
  • @Buddhisthead Then what do you suggest if I need the build number to only increase after successful build, script doesn't work when I add it in Edit Scheme as post action. – Ahmed Shendy Feb 20 '22 at 08:25
  • 1
    @AhmedShendy See my answer below - I finally got the archive post action to work. – Trev14 Mar 03 '22 at 21:18
34

First: +1 on ConfusedNoob's answer, because that was the problem (which led me to numerous experiments and finally this solution.) If my answer helps you, +1 his, too, because his hint was huge!

(Also, see my other answer, below, that bypasses agvtool altogether. I eventually settled-in to using that in all my projects.)

I've been messing with it a bit and the only thing I've found that works reliably to solve it is to use agvtool as a pre-action in the appropriate scheme(s), rather than as a run-script in the build-phases.

  • CMD-SHIFT-comma to edit schemes (XCode 6.x)
  • Twiddle-open whichever scheme you want this on. I did it on Run and Archive, but I probably only really need it on archive.
  • Select the "+" icon and "New run script action"
  • Add your agvtool script. If you care, mine is:

    cd ${PROJECT_DIR} ; xcrun agvtool next-version -all
    

(NOTE: pre-actions don't naturally run in ${PROJECT_DIR}, so you have to cd.)

Close & save and that's it.

The problem is that agvtool modifies the project file (unnecessarily, since all the build numbers we care about are elsewhere), and modifying the project file causes the build to cancel.

+1 one on the question, too -- cripes, that was a tough one!

Olie
  • 24,597
  • 18
  • 99
  • 131
  • 2
    I don't mind it but if added as pre-action script in `Archive` scheme, it seems to be "archived" with current version and incremented then (same result as post-action) in Xcode 7. – Stanislav Smida Aug 14 '15 at 12:46
  • 1
    Also I noticed I had to do it as a Post-action to prevent the cancellation warning when executing tests and had to select `Provide build settings from` and my framework. – Kudit May 08 '16 at 19:29
  • @Olie It seems to cancel build in scheme pre action too. Did you got it to work ? For me its always cancelling my build. – user1398615 Jun 14 '17 at 23:28
  • @user1398615 - I was having this too. I had to add this as a post-action in the Build. Which is not great - I'd like to have the build number incremented at the start of the build. But incrementing at the end at least ensures that all builds have a distinct number. – Matt Cline Aug 25 '17 at 15:31
  • "The problem is that agvtool modifies the project file ..., and modifying the project file causes the build to cancel." - that's the gist of it. – Alexander Revo Jan 28 '20 at 12:59
  • I found today that if I don't have CURRENT_PROJECT_VERSION defined at all in my pbxproj file (not even set to 0) then agvtool will not modify the pbxproj file. It's a fragile solution though, because I just know I will forget and I'll set a build number in my project and be back in trouble again. I'm very tempted to not use agvtool at all though, but to use PListBuddy instead. – Derek Knight May 17 '20 at 05:58
  • @AlexanderRevo then what do you suggest if I need the build number to only increase after successful build, script doesn't work when I add it in Edit Scheme as post action. – Ahmed Shendy Feb 20 '22 at 08:24
3

Another technique is to skip agvtool altogether. Just add this runtime script to the end of your build phases (after copy bundle resources.)

  • Select your project

Select your project

  • Select build phases and +

Select build phases and +

  • Add a new Run Script phase

Add new run-script phase

  • Enter the script

Enter the script

The script, for easy copy/paste:

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

Make sure your script runs after the copy-bundle phases of your build

Olie
  • 24,597
  • 18
  • 99
  • 131
  • 1
    A potentially major downside to this approach is that these three lines must be executed for both the iOS app and any additional app extension target(s) individually, as App Store Connect requires both the version and build number between an iOS app and any extension(s) to match. If/when targets are in flux, this solution adds a layer of manual maintenance which can get somewhat annoying. Of course, if your iOS app is the only target, this is a non-issue. – Jonathan Thornton Jul 19 '19 at 00:50
  • https://stackoverflow.com/a/60073040/208308 This answer helped me. Above answer was throwing me "Command PhaseScriptExecution failed with a nonzero exit code" error. – Pradeep Reddy Kypa Sep 22 '20 at 13:16
2

Xcode 13.2.1 - Super Simple

You may have a version bump script throwing off the build process (as mentioned above). If that's the case, remove it and put this code below in a post build action (inside the Edit Scheme dialogue). It will bump all version numbers inside your project without causing the issue.

cd "${PROJECT_DIR}" ; agvtool bump

Here's my short blog post on the subject

After a ton of testing I realized a couple of things:

  • A lot of answers leave off the quotes around ${PROJECT_DIR} which prevents the change of directory to the project folder.
  • We can access agvtool directly, instead of using xcrun (at least when I have Xcode command line tools installed, I haven't tried this without them)
  • The new build system prevents archives from completing successfully when a build phase script uses agvtool to modify the project file.
Trev14
  • 3,626
  • 2
  • 31
  • 40
  • 1
    Yeah, I ended using post build action inside Edit Scheme, it seems the proper way rather than doing it as a build phase since Xcode now uses "New Build System" which I read somewhere it runs builds phases in parallel. – Ahmed Shendy Mar 04 '22 at 04:06
  • It is not working when you use SwiftUI preview, because the project itself is reloading after the agvtool bump and the preview will begin the build process again, and the post action will be called again. – feca Feb 15 '23 at 15:54
1

For me the carthage update build phase was causing the issue.

Also check if you have any other build phases with scripts that modify files or code.

Foti Dim
  • 1,303
  • 13
  • 19
0

I had the same problem. But I fixed it while still using avgtool.

I was using a Build Phases Run Script not as the last step and used the following script which caused errors:

"${DEVELOPER_BIN_DIR}/agvtool" next-version -all

My solution was to use Build Phases Run Script as last phase and change the script to the folling

agvtool bump
Binarian
  • 12,296
  • 8
  • 53
  • 84
0

Just solved this issue by putting the script code into post actions in Build scheme.

Product -> Schemes -> Edit Scheme -> Build

Expand the build scheme and find Post actions there. Click "+" and add new run script.

Thats it.

-1

In my case, I started getting this issue a lot after updating my XCode version. This is what I did to fix it:

  1. Click on the "hammer" icon on the top bar in XCode. enter image description here

  2. Select "Legacy Build System" in the Build System tab.

  3. Click on done.

The application should build without this issue after this if you are working on an older project and everything seems to get compiled.

Arka Mukherjee
  • 2,083
  • 1
  • 13
  • 27