iPhone Application Post Build Script

很多平台软件开发发布前, 有个很重要的事情就是保留好符号表(symbol)文件. 因为发布的软件一般来说都去除了符号表, 而符号表对于维护是很重要的信息, 没有它, 在拿到call stack后会很难对应到代码中. iPhone开发也是一样, 每次做完Build, 都会有一个.dSYM的目录, 就是符号表. 有了它, 在用户提交crash log后会很容易定位问题.

手工保存这些符号表文件很容易产生疏漏, 恢复起来又麻烦. 我做了一个脚本, 添加到Target的Run Script Build Phase中, 可以在Build类型是Distribution时, 将build出的binary和符号表分别打包为:

  • [application].app.[version].zip
  • [application].app.dSYM.[version].[git hash].zip

如果你用的版本管理系统不是git,需要对脚本做相应改动.
以下是脚本代码

#!/usr/bin/env ruby
if ENV["BUILD_STYLE"] == "Distribution" && ENV["ARCHS"] == 'armv6'
  common_git_paths = %w[/usr/local/bin/git /usr/local/git/bin/git /opt/local/bin/git]
  git_path = ""

  common_git_paths.each do |p|
    if File.exist?(p)
      git_path = p
      break
    end
  end

  if git_path == ""
    puts "Path to git not found"
    exit
  end

  command_line = git_path + " rev-parse --short HEAD"
  sha = `#{command_line}`.chomp

  info_file = ENV['INFOPLIST_FILE']

  f = File.open(info_file, "r").read
  re = /([\t ]+CFBundleVersion<\/key>\n[\t ]+)(.*?)(<\/string>)/
  f =~ re
  puts $1

  # Get the version info from the source Info.plist file
  # If the script has already been run we need to remove the git sha
  # from the bundle’s Info.plist.
  version = $2.sub(/ \([\w]+\)/, "")

  cmdline = "cd #{ENV['BUILT_PRODUCTS_DIR']};zip -r #{ENV['CONTENTS_FOLDER_PATH']}.#{version}.zip #{ENV['CONTENTS_FOLDER_PATH']};zip -r #{ENV['CONTENTS_FOLDER_PATH']}.dSYM.#{version}.#{sha}.zip #{ENV['CONTENTS_FOLDER_PATH']}.dSYM"
  `#{cmdline}`
end

添加方法为:

  1. 在XCode左边栏选中相应的Target
  2. 在菜单中选择Project -> New Build Phase -> New Run Script Build Phase .
  3. 弹出的对话框中,Shell里填写/usr/bin/ruby, Script中填写上面的脚本代码.