Sunday, May 31, 2015

Building iOS apps in comand line

Building iOS apps in command line.
1 Using xcodebuild command
Navigate to the directory of the Xcode in the terminal and issue the command xcodebuild. Then you will be able to build. In addition you can specify the build configuration using the –configuration option. For example, this is how to build with debug configuration.
xcodebuild –configuration Debug
2 Building using xctool
Also we can build using the command xctool. In fact xctool can be considered as a replacement for xcodebuild tool. Also xctool is created and maintained by the facebook. In order to install the xctool you need to clone it to some convenient location as follows
git clone git@github.com:facebook/xctool.git

In cloned project, you will see a script called xctool.sh. That is what we should be concentrating.Next you have to build the tool. Fortunately, when you call xctool for the first time, it builds itself. Invoke the the script with with option version as follows. This will give you the current version of the tool. Also it will build the tool.
./xctool.sh -version
Now you need to include xctool installation path to the installation path. So you can easily invoke the command from anywhere else. Now you have the xctool installed and path also correctly set. Now navigate to the folder where your project is residing and issue the xctool command as follows.
xctool.sh -workspace /Your workspace name/ -scheme /Your project name/
There are lot of advantages of using xctool over xcodebuild. One is, output of xctool is better readable. In addition xctool provides different reporters making it easier to integrate CI systems such as jenkins. Also it supports cofiguration file format. In order to use it, simply create a “.xctool-args” file with the following format
[
        "-workspace", "/workspace name/.xcworkspace",
        "-scheme", "/project name/",
        "-configuration", "Release"
]
Because of this file, now you can invoke xctool without other options. But since the xctool is created and maintained by a third party company, xctool may fail with newer xcode versions.
3 Building IPA files with command line
Now you know how to build a xcode project in command line. But in order to run it, you need to create an IPA file. It can be done in command line. It can be done using xcrun tool. xcrun is a xcode tool. It is impacted by the location of the developer directory and the value of $DEVELOPER_DIR environment variable. This tool does two tasks. That is finding development tools and executing them. If you want to find the location of you git installation, you can call this command as follows
xcrun --find git
As I mentioned earlier, this tool can execute tools as well. This is how to execute a git command via xcrun
xcrun git --version
Here you need to be bit carefull. Because xcrun can bypass the traditional $PATH enviroment variable to locate executable files. Now lets get back to our original topic. That is generating the IPA file. It can be achieved with PackageApplication tool. This tool can be invoked by xcrun tool. This is how to invoke it
xcrun PackageApplication
But unfortunately, you will get the following error message.
xcrun: error: unable to find utility "PackageApplication", not a developer tool or in PATH
This error can be caused by either of this issues.
  • It is simply not in your PATH
  • PackageApplication is an executable specific to the iOS SDK. Since we did not specify the SDK we are using and the SDKROOT environment variable was not set, the xcrun was unable to find the tool
We can specify the iOS sdk we are using with the SDK argument.
xcrun --sdk iphoneos --f PackageApplication
Package application only needs the path to the .app package generated at the end of the xcodebuild command line tool. In addition it will perform tasks such as resigning the application before packaging it into the IPA file. Path to the generated package can be found at the end of the command's output. This is how to use it
/usr/bin/xcrun -sdk iphoneos PackageApplication -v "${RELEASE_BUILDDIR}/${APPLICATION_NAME}.app" -o "${BUILD_HISTORY_DIR}/${APPLICATION_NAME}.ipa" --sign "${DEVELOPER_NAME}" --embed "${PROVISONING_PROFILE}”
Putting everything in a single script
Now you have scene how to build and generate IPA files in command line. In a CI environment it is better to have them in a single script. This is how to do it.
#!/bin/bash
WORKSPACE="/Your workspace name/"
SCHEME="/Your project name/"
BUILD_DIR="$(pwd)/build"
CONFIGURATION="Release"
ARCHIVE_FILENAME="/Your project name/.app"
IPA_FILENAME="/Your project name/.ipa"
security unlock-keychain -p $PASSWORD $HOME/Library/Keychains/login.keychain

xcodebuild -workspace "$WORKSPACE" -scheme "$SCHEME" -configuration
CONFIGURATION_BUILD_DIR="$BUILD_DIR" clean build | xcpretty -c
xcrun -sdk iphoneos PackageApplication "$BUILD_DIR/$ARCHIVE_FILENAME" -o "$BUILD_DIR/$IPA_FILENAME"

For any query, feel free to contact me via my linkedin profile.

No comments:

Post a Comment