Automating a cordova ios build

One of our customers needed an android and iOS mobile application.  We identified cordova as the technology to be used to develop it.

We had done a cordova/phonegap application earlier for another customer.  When we did that, it was only for android.  Subsequently, we got it running in iOS also (with minimal efforts), but the exercise of building the iOS project was not automated.

This time, I wanted to automate the iOS build process too. 

One of the best practices for cordova projects is not to check-in the contents of platform folder.  Cordova copies over the contents of www folder to the corresponding subfolders of the platform folder during each build.  We wanted to follow this practice.

It is good to create the cordova project right.  Previously, we had used phonegap CLI to create the project and subsequently used cordova commands to build it.  While this works, it is better to stick with one - either phonegap or cordova.   

We chose cordova for the following reasons:

  • config.xml created with cordova is simpler than that created with phonegap.  
  • phonegap CLI creates resources folders/files with default icons for every supported platform.  
  • phonegap CLI build is less flexible compared to cordova (though internally I suspect phonegap uses cordova commands)

[Note:  at the time of writing this, version of cordova is 3.4.1, xcode-5.1.  Needless to say, things change across versions]

Anyway, this is how we created our project

cordova create <folder-name> <package-name> <app-name>

Thereafter all the development happened in www folder.  Here is how we had automated the android build.  The idea was to do a similar exercise for iOS.

iOS differs from android in two ways.

a) iOS app can only be built on a Mac system.  

So the linux system on the cloud running jenkins was not sufficient.  We needed to set up an ios-slave jenkins instance.

b) the creation and distribution of an iOS app is different from android

For android, we could create a release build using cordova CLI and deploy it to Google Play store in alpha/beta testing for testers to play with.

In case of iOS

  • cordova does not provide a mechanism to generate a distribution (ipa) file.  We need to take recourse to some additional native commands (details below).
  • Also, the distribution file thus created cannot be installed through email.  Testflight is the recommended way to do this.
This is how we went about this

Add ios platform

cordova platform add ios

Do a release build for ios

This isn't straightforward as you can see.

One would think, the following would work

cordova build ios --release

But no, this did a release build for the emulator.  The one which did the required code-signing is the following:

cordova build ios --device

After doing a build, the above command attempted to sign the build.  To do this, it needed to pick an appropriate certificate.  It did this based on the information in the file cordova/build.xcconfig inside platforms/ios.

There seemed to be a bug in this file, due to which failed to pick an appropriate certificate.  The fix was to alter the word "Developer" with "Distribution".  

Since we did not want to check-in platform specific file, the way to do this was to add a hook in the hooks folder.  

Now, could we not have used xcodebuild to build the generated xcode project, instead of cordova CLI?  This did not seem to work - at least with xcode 5.1 - without opening the project in xcode and possibly checking in changes to version control.

Essentially xcode project generated by cordova did not contain any scheme.

If xcodebuild had worked, then jenkins xcode plugin would have taken care of automating the build from jenkins.  Now, it was a two-step process, the first step done by cordova and the second, to be done by xcrun.

xcrun -sdk iphoneos PackageApplication -v ${WORKSPACE}/platforms/ios/build/device/my.app -o ${WORKSPACE}/platforms/ios/build/myapp-1.0.${BUILD_NUMBER}.ipa --embed my.mobileprovision --sign "my code signing identity"

This generated an ipa file with the specified provisioning information that allows the devices with UDIDs therein to install the app.

The next step was to upload this to testflight.  This was quite easy, thanks to a combination of testflight API and jenkins testflight plugin.  All it needed was configuring the API and team keys.

Since I did not want all the ios builds to be made available to testflight, I separated the testflight deployment from ios build.  The testflight deployment was a manual item (as a jenkins job is called now), which picked the "latest" ios artifact (using copy artifact plugin) and uploaded to testflight.

[Note:  Artifacts copied using copy artifact accumulate and all of them end up getting uploaded to testflight for each run.  So it makes sense to add a script to delete existing artifacts as the first build step.]

And hey, this is better than the manual upload of artifact that is required to test android app.  Testflight supports android apps also (for now) - maybe I should use that!

My cordova ios project build and release was automated.  And in the entire process, I did not open xcode once.

[Update 3 Feb 2015:  Upload to TestFlight is no longer possible since TestFlight is now Apple and there is no programmatic upload.  As well no android support]

Comments

  1. Hi Raghuram,

    I have tried your command and I was able to create the ipa file. But when I try to install on device it fails. I can confirm that the device uuid is added to the provisioning profile I am using herewith.

    My command is as follows :

    xcrun -sdk iphoneos PackageApplication -v "/Users/ramesh/Downloads/CordovaPrj/platforms/ios/build/device/App Name.app" -o "/Users/ramesh/Downloads/CordovaPrj/release/ios/App_Name_1.0.0.ipa" --embed "/Users/ramesh/Downloads/CordovaPrj/build.tools/my.mobileprovision" --sign "iPhone Distribution: Company Name (B46QVD65GU)"

    could you please help me ?.

    ReplyDelete
  2. Hi, Can you check if you get any error message running the command?

    ReplyDelete
    Replies
    1. actually I don't. I have uploaded my build log here :
      https://drive.google.com/file/d/0B85vUqBPKLOxUGwzdXpPSE90WkE/edit?usp=sharing

      could you please help me out ?.

      Delete
    2. the only error I get is the one when I install the ipa on the iphone. this error was captured using the iphone configuration utility during the ipa installation.

      Jul 9 11:08:44 Rameshs-iPhone librariand[102] : ubiquity account is not configured (or is disabled for this client), not creating collection
      Jul 9 11:08:44 Rameshs-iPhone librariand[102] : error in _handle_client_request: LibrarianErrorDomain/10/Unable to configure the collection.

      Delete
    3. Hi, Stackoverflow is the right place to find a solution for this.

      I see a couple of lines in the log indicating error in codesigning with the message "a sealed resource is missing or invalid", though subsequently it seems to have worked.

      Googling also reveals issues people have had related to "librariand".

      Sorry I can't be of better help. When you do solve the problem, do update this link for others. Thanks.

      Delete
  3. Nice article!

    Can't wait to test this out...!

    ReplyDelete
  4. Thanks for sharing informative article on java application development. Your post helped to understand the career in Java. JAVA Training in Chennai

    ReplyDelete

  5. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    Regards,
    ccna course in Chennai|ccna training in Chennai

    ReplyDelete
  6. The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers. Best software testing training institute in Chennai | Software Testing Training in Chennai | Software testing course in Chennai

    ReplyDelete
  7. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    cloud computing training in chennai | cloud computing courses in chennai

    ReplyDelete
  8. Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Great work admin.Keep update more blog.
    Mobile App Development Company
    Android app Development Company
    ios app development Company
    Mobile App Development Companies

    ReplyDelete
  9. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in IOS development, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on IOS development . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete
  10. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information&its very useful to me...
    Android training in chennai
    Ios training in chennai

    ReplyDelete
  11. Thanks for the Amazing blog... Thanks for sharing
    you can have a look at my blog and suggest some changes if required..

    http://aptronnoida.in/best-erp-sap-training-in-noida.html

    ReplyDelete

Post a Comment

Popular posts from this blog

Opening a safe deposit locker in SBI

Opening a Kannada Word document

Switching to Tatasky make my pack