As a developer, you have to package your testers for testing. Packaging is very boring, especially in the first day or two of each new release, there will always be some inexplicable bug, and then the packaging activity is particularly frequent in these two days. Having to do the same thing several times a day is totally unacceptable as a programmer. Automated packaging, thanks to you. So today I’m going to talk about iOS app automation packaging, but this article is about building a single test package, not continuous integration.

xcodebuild & xcrun

Xcode provides a set of build package commands called XcodeBuild and xcRun. Xcodebuild packages the corresponding project into an.app file, and xcRun converts the corresponding.app file into the corresponding.ipa file. To learn about these two sets of commands, you can view the corresponding official xcodebuild and xcrun documents, or enter man xcodebuild and man xcrun on the terminal to view the corresponding MAN Page. Some of these commands are selected as examples to describe how to use them.

xcodebuild

All xcodeBuild commands
NAME xcodebuild -- build Xcode projects and workspaces SYNOPSIS xcodebuild [-project name.xcodeproj] [[-target targetname] ... | -alltargets] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [action ...]  [buildsetting=value ...]  [-userdefault=value ...]  xcodebuild [-project name.xcodeproj] -scheme schemename [[-destination destinationspecifier] ...]  [-destination-timeout value] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [action ...]  [buildsetting=value ...]  [-userdefault=value ...]  xcodebuild -workspace name.xcworkspace -scheme schemename [[-destination destinationspecifier] ...]  [-destination-timeout value] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [action ...]  [buildsetting=value ...]  [-userdefault=value ...]  xcodebuild -version [-sdk [sdkfullpath | sdkname]] [infoitem] xcodebuild -showsdks xcodebuild -showBuildSettings [-project name.xcodeproj | [-workspace name.xcworkspace -scheme schemename]] xcodebuild -list [-project name.xcodeproj |  -workspace name.xcworkspace] xcodebuild -exportArchive -archivePath xcarchivepath -exportPath destinationpath -exportOptionsPlist path xcodebuild -exportLocalizations -project name.xcodeproj -localizationPath path [[-exportLanguage language] ...]  xcodebuild -importLocalizations -project name.xcodeproj -localizationPath pathCopy the code

The most commonly used commands are the first three lines of SYNOPSIS, which correspond to projects of the. Project and. Xcworkspace types respectively. In addition to these three, I’ll start with the xcodebuild-list command on line 7.

Description of the xcodebuild-list command

Take our own project as an example. Enter the project folder on the terminal and use the command xcodebuild-list to output the following information:

Information about project "yeemiao":
    Targets:
        yeemiao
        yeemiao-inhouse
        yeemiaoTests
        yeemiaoInhouseTests

    Build Configurations:
        Debug
        Release

    If no build configuration is specified and -scheme is not passed then "Release" is used.

    Schemes:
        yeemiao
        yeemiao-inhouseCopy the code
  • Targets:We have four projectsTargets.yeemiaoIs releasedApp StoreThe,yeemiao-inhouseIs the corresponding test version, and the following two are the corresponding performance tests.
  • Build Configurations:Since we distinguish the beta version from the official version in Targets, we are not adding one hereBuild Configurations“, but the default twoDebugandReleaseVersion.
  • Schemes:The two shown aboveSchemesThe default is.xcodeprojIf you usexcodebuild -list -workspace yeemiao.xcworkspaceCheck it out and you’ll see the corresponding third-party librarySchemes.
The first three lines of xcodeBuild are described

With the above three knowledge, the first three commands in SYNOPSIS are easy to understand

  • -project -workspace: These two correspond to the name of the project, i.e. which project should be packaged. If there are multiple projects and they are not specified here, the default is the first project.
  • -target: package corresponding targets. If not specified, this defaults to the first one.
  • -configuration: If this configuration is not modified, the default is Debug and Release. The default is not Release.
  • buildsetting=value ...: Use this command to modify project configurations. But in practice, I chose to read a file to modify a configuration instead of applying this method.
  • -scheme: Specifies the package scheme.

A simple use of Demo

Before we get into actual use, let’s use a simple Demo. First create a project named Toyun, and then make sure that the project can be debugged on real machines. Open the terminal to enter the Toyun project and run the following command:

xcodebuild -project Toyun.xcodeproj -target Toyun -configuration Release

This command is used to package the targets version of Toyun as Release version. If ** BUILD SUCCEEDED ** is displayed on the terminal, it indicates success. Under the Toyun project folder, you will find a new folder named Build, under which there are two sub-folders, relex-iphoneOS and toyun. build. The toyun. app file is in the relex-iphoneOS folder. This is what the Xcodebuild command does, and ultimately generates the.app file. But what we need is an.ipA file, so execute this command from the terminal

xcrun -sdk iphoneos -v PackageApplication ./build/Release-iphoneos/Toyun.app -o ~/Desktop/Toyun.ipa

This command converts the toyun. app file in the release-iphoneOS folder to toyun. ipa and places it on the desktop. Iphoneos is the SDK used, PackageApplication is the developer tool used. If you return to the desktop and see the toyun. ipa file, it is successful.

Actual use of automated packaging

From the simple Demo above, we were able to type the.ipA file we needed with just two lines of command. However, this is still a distance from our actual use, and the situation is complicated and changeable in practical application. Here is an example of our own testing process to illustrate some of the situations in practice. I wrote this automatic package using Shell script, of course you can choose to write in another language.

Packaging process

First, let’s talk about our packaging process. First, pull the latest code from SVN, then modify the corresponding test version number, select the corresponding certificate and description file, and start archive packaging. After packaging, export the corresponding IPA file, and download the installation package using ITMS-Services protocol. This time to modify the exported test package name and protocol related. Plist file. Then upload it to the server. Then the test downloads the corresponding test package according to the link generated by the protocol and starts the test. The whole process takes about 10 minutes to go through.

Switch the package stream to a scripting language

#! # build_number=$(date) # build_number=$(date) # build_number=$(date +%Y%m%d%H) # mkdir ~/Desktop/project # If you are using Git or other version control, modify the corresponding pull code command SVN export SVN :// # Workspace_path = ~ / Desktop/project/yeemiao2016 / yeemiao # packaging project name scheme_name = yeemiao - inhouse # packing certificate CODE_SIGN_IDENTITY=" Replace the name of your certificate" Instead, the corresponding 8b11AC11-XXXX-XXXX-XXXX-b022665DB452 name PROVISIONING_PROFILE=" replace the name of your description file "# specifies the output location of yeemiao.app Build_path =~/Desktop/project/build # Specify yeemiao.ipa output location ipa_path=~/Desktop/project # info.plist file location Info_plist = ~ / Desktop/project/yeemiao2016 yeemiao/yeemiaoInhouse - Info. Plist # below is read. The location of the file and then modify the version number and the build number, This is done without using the commands provided by XcodeBuild, Have described in the above # modified version number/usr/libexec/PlistBuddy - c "Set: CFBundleShortVersionString $version_string" ${info_plist} # modify the build number /usr/libexec/plistbuddy -c "Set :CFBundleVersion $build_number" ${info_plist} Xcodebuild-workspace ${workspace_path}. Xcworkspace -scheme ${scheme_name} -configuration Release clean -sdk iphoneos build CODE_SIGN_IDENTITY="${CODE_SIGN_IDENTITY}" PROVISIONING_PROFILE="${PROVISIONING_PROFILE}" SYMROOT="${build_path}" # generate yeemiao.ipa, Xcrun -sdk iphoneOS -v PackageApplication ${build_path}/ release-iphoneos /yeemiao.app -o ${iPA_path}/ yeemiAO_ios_ ${version_string}. Ipa # Location of the. Plist file required by the ITMs-services protocol Plist_path =~/Desktop/project/yeemiao_ios_${version_string}. Plist # Generate the plist file and replace it with your path cat << EOF > $plist_path in the corresponding path The items assets kind software - package to replace the corresponding url path metadata bundle - identifier com. Threegene. Yeemiao. The inhouse bundle - version Ipa # ipa_file_path=~/Desktop/project/yeemiao_ios_${version_string}.ipa # Plist_file_path =~/Desktop/project/yeemiao_ios_${version_string}. Plist # Of course, this step can also be done by command. # Depending on where you want to upload the test package, Fir, Dandelion, seven Cow, etc., generally provide the command to upload files. $curl = $curl = $curl = $curl = $curl = $curl = $curl = $curl It is also possible to add some judgment (which requires the existence of the last file) and some Log output to make the script clearer and more accurate.Copy the code

When the script is finally ready, you can press Enter and the packaging process is complete. I named this program autoarchive.sh. Then I just need to type in the command at the terminal when PACKING:

~. / autoArchive. Sh 3.0.0000

other

There were a few problems with writing this script, one of which was that our xcSharedData was not committed to SVN, and the script froze on the line pointing to the package command. The xcSharedData was later committed, and the problem was resolved.

There are also various packages for automated packaging on the web, most notably facebook’s XcTool. This tool normalizes the output log, and some error messages are clearer. You can still install it and try it out.

reference

  • IOS automatically packages and publishes scripts