iOS集成Sentry崩溃收集日常操作

前序

这玩意我是真的不太喜欢,但是应领导需求,因为这玩意可以架在自己的服务器上,从某种程度上能够避免自己项目的信息外露吧,或许是个优势?无奈只能选择集成,集成的过程中遇到了不少问题,只怪官方文档写的渣渣,简单的不行,所以很多问题都需要自己解决了。

集成

按道理讲,这是个很简单的过程,也就三步:

  1. pods安装Sentry
  2. 在项目中启用Sentry(填入自己服务器的Sentry地址)
  3. 配置dsym文件的上传(官方推荐fastlane上传)

以上的第一二步按照文档来基本不费功夫,这里就不费口舌了,主要是第三步的内容,真是遇到了不少问题。这里的上传需要用sentry-cli这个工具来进行上传,安装如下:

#方法一
brew install getsentry/tools/sentry-cli
#方法二
curl -sL [https://sentry.io/get-cli/](https://sentry.io/get-cli/) | bash

安装完成之后,可以查看是否安装成功了,如下命令:

sentry-cli --version

好了,这个时候就当你已经安装成功之后了吧~

在上传的时候可以通过两种方法去进行上传:

方法一:通过fastlane 配置上传内容

fastlane的安装这里不在叙述,说一下插件的安装,进入项目文件夹后,可以通过命令fastlane add_plugin sentry 进行插件的安装,完成之后,我们可以通过查看fastlane 文件夹下的Pluginfiles来确定插件的安装情况,如下:

# Autogenerated by fastlane
#
# Ensure this file is checked in to source control!

gem 'fastlane-plugin-pgyer'
gem 'fastlane-plugin-sentry'

完成插件安装之后,我们需要配置一下sentrylane,打开Fastfile,创建一个新的lane,配置内容如下:

platform :ios do
lane :upload_symbols do

  #服务器地址
  sentry_api_host = "XXXX"
  #授权token
  sentry_auth_token = "XXXX"
  #服务器组织名称(存放项目的名称)
  sentry_org_slug = "XXXX"
  #项目工作空间
  sentry_workspace = "XXXX.xcworkspace"

  puts "scheme->#{schemeName} , workspace-> #{sentry_workspace}"
  sentry_upload_dsym(
    url: "#{sentry_api_host}",
    auth_token: "#{sentry_auth_token}",
    org_slug: "#{sentry_org_slug}",
    project_slug: "#{project_slug}",
    # 默认不用指定当前的dsym内容
    # dsym_path: './XXXX.app.dSYM.zip'
  )

end
end

这里sentry_upload_dsym动作参数可能发生改变,可以通过命令fastlane action sentry_upload_dsym 来查看相关的参数信息介绍:

+------------------------+---------+--------------------------+
|                        Used plugins                         |
+------------------------+---------+--------------------------+
| Plugin                 | Version | Action                   |
+------------------------+---------+--------------------------+
| fastlane-plugin-pgyer  | 0.2.2   | pgyer                    |
| fastlane-plugin-sentry | 1.5.0   | sentry_upload_dsym       |
|                        |         | sentry_upload_sourcemap  |
|                        |         | sentry_finalize_release  |
|                        |         | sentry_upload_file       |
|                        |         | sentry_create_release    |
+------------------------+---------+--------------------------+

Loading documentation for sentry_upload_dsym:

+------------------------------------------------------------------------------------------------------------------+
|                                                sentry_upload_dsym                                                |
+------------------------------------------------------------------------------------------------------------------+
| Upload dSYM symbolication files to Sentry                                                                        |
|                                                                                                                  |
| This action allows you to upload symbolication files to Sentry. It's extra useful if you use it to download the  |
| latest dSYM files from Apple when you use Bitcode                                                                |
|                                                                                                                  |
| Created by joshdholtz, HazAT                                                                                     |
+------------------------------------------------------------------------------------------------------------------+

+--------------+----------------------------+---------------------+---------+
|                        sentry_upload_dsym Options                         |
+--------------+----------------------------+---------------------+---------+
| Key          | Description                | Env Var             | Default |
+--------------+----------------------------+---------------------+---------+
| url          | Url for Sentry             | SENTRY_URL          |         |
| auth_token   | Authentication token for   | SENTRY_AUTH_TOKEN   |         |
|              | Sentry                     |                     |         |
| api_key      | API key for Sentry         | SENTRY_API_KEY      |         |
| org_slug     | Organization slug for      | SENTRY_ORG_SLUG     |         |
|              | Sentry project             |                     |         |
| project_slug | Project slug for Sentry    | SENTRY_PROJECT_SLUG |         |
| dsym_path    | Path to your symbols       | SENTRY_DSYM_PATH    |         |
|              | file. For iOS and Mac      |                     |         |
|              | provide path to            |                     |         |
|              | app.dSYM.zip               |                     |         |
| dsym_paths   | Path to an array of your   | SENTRY_DSYM_PATHS   |         |
|              | symbols file. For iOS and  |                     |         |
|              | Mac provide path to        |                     |         |
|              | app.dSYM.zip               |                     |         |
| symbol_maps  | Optional path to           | SENTRY_SYMBOL_MAPS  |         |
|              | bcsymbolmap files which    |                     |         |
|              | are used to resolve        |                     |         |
|              | hidden symbols in the      |                     |         |
|              | actual dsym files. This    |                     |         |
|              | requires the dsymutil      |                     |         |
|              | tool to be available       |                     |         |
| info_plist   | Optional path to           | SENTRY_INFO_PLIST   |         |
|              | Info.plist to add version  |                     |         |
|              | information when           |                     |         |
|              | uploading debug symbols    |                     |         |
+--------------+----------------------------+---------------------+---------+
* = default value is dependent on the user's system

More information can be found on https://docs.fastlane.tools/actions/sentry_upload_dsym

这样,fastlane上传的配置信息就此配置完毕。这一块上传可以结合fastlane自动打包来进行自动化上传,将此lane接在自动化打包之后即可。

方法二:通过shell脚本上传到服务器中

通常的做法就是在生成dsym文件之后,通过sentry-cli执行shell脚本来上传该文件,命令如下:

if which sentry-cli >/dev/null; then
#项目存放地儿的名称
export SENTRY_ORG=XXXX
#项目名称
export SENTRY_PROJECT=XXXX
#授权token
export SENTRY_AUTH_TOKEN=XXXX
#服务器地址
export SENTRY_URL=XXXX
#调试模式
export SENTRY_LOG_LEVEL=debug

ERROR=$(sentry-cli upload-dsym 2>&1 >/dev/null)
echo "success ~~"
if [ ! $? -eq 0 ]; then
echo "warning: sentry-cli - $ERROR"
fi
else
echo "warning: sentry-cli not installed, download from https://github.com/getsentry/sentry-cli/releases"
fi

可以存为.bash脚本,每次手动进行上传,但是这样难免会显得繁琐,所以,为了更加方便的去操作当前的上传工作并且解放自己的双手,所以我们可以将当前的操作集成在XCodebuild phases中已达到自动化的目的,在对应的target中选择build phases添加新的phase,如下:

iOS集成Sentry崩溃收集日常操作_第1张图片
配置

紧接着,在内容中添加如下配置内容:


# # 脚本默认配置的版本格式为CFBundleShortVersionString(CFBundleVersion),  如果你修改默认的版本格式, 请设置此变量, 如果不想修改, 请忽略此设置
#CUSTOMIZED_APP_VERSION=""
#
# # Debug模式编译是否上传,1=上传 0=不上传,默认不上传
UPLOAD_DEBUG_SYMBOLS=0
#
# # 模拟器编译是否上传,1=上传 0=不上传,默认不上传
UPLOAD_SIMULATOR_SYMBOLS=0
#
#只有Archive操作时上传, 1=支持Archive上传 0=所有Release模式编译都上传
UPLOAD_ARCHIVE_ONLY=0

# 打印错误信息
function exitWithMessage(){
echo "--------------------------------"
echo "${1}"
echo "--------------------------------"
exit ${2}
}

function sentryUpload(){

#Type a script or drag a script file from your workspace to insert its path.
if which sentry-cli >/dev/null; then
#项目存放地儿的名称
export SENTRY_ORG=XXXX
#项目名称
export SENTRY_PROJECT=XXXX
#授权token
export SENTRY_AUTH_TOKEN=XXXX
#服务器地址
export SENTRY_URL=XXXX
#调试模式
export SENTRY_LOG_LEVEL=debug

ERROR=$(sentry-cli upload-dsym 2>&1 >/dev/null)
echo "success ~~"
if [ ! $? -eq 0 ]; then
echo "warning: sentry-cli - $ERROR"
fi
else
echo "warning: sentry-cli not installed, download from https://github.com/getsentry/sentry-cli/releases"
fi

}

# 在Xcode工程中执行
function runInXcode(){

echo "Uploading dSYM to Sentry in Xcode ..."
echo "Info.Plist : ${INFOPLIST_FILE}"

BUNDLE_VERSION=$(/usr/libexec/PlistBuddy -c 'Print CFBundleVersion' "${INFOPLIST_FILE}")
BUNDLE_SHORT_VERSION=$(/usr/libexec/PlistBuddy -c 'Print CFBundleShortVersionString' "${INFOPLIST_FILE}")


echo "--------------------------------"
echo "Prepare application information."
echo "--------------------------------"

echo "Product Name: ${PRODUCT_NAME}"
echo "Bundle Identifier: ${BUNDLE_IDENTIFIER}"
echo "Version: ${BUNDLE_SHORT_VERSION}"
echo "Build: ${BUNDLE_VERSION}"


echo "--------------------------------"
echo "Check the arguments ..."

##检查模拟器编译是否允许上传符号
if [ "$EFFECTIVE_PLATFORM_NAME" == "-iphonesimulator" ]; then
if [ $UPLOAD_SIMULATOR_SYMBOLS -eq 0 ]; then
exitWithMessage "Warning: Build for simulator and skipping to upload. \nYou can modify 'UPLOAD_SIMULATOR_SYMBOLS' to 1 in the script." 0
fi
fi

##检查是否是Release模式编译
if [ "${CONFIGURATION=}" == "Debug" ]; then
if [ $UPLOAD_DEBUG_SYMBOLS -eq 0 ]; then
exitWithMessage "Warning: Build for debug mode and skipping to upload. \nYou can modify 'UPLOAD_DEBUG_SYMBOLS' to 1 in the script." 0
fi
fi

##检查是否Archive操作
if [ $UPLOAD_ARCHIVE_ONLY -eq 1 ]; then
if [[ "$TARGET_BUILD_DIR" == *"/Archive"* ]]; then
echo "Archive the package"
else
exitWithMessage "Warning: Build for NOT Archive mode and skipping to upload. \nYou can modify 'UPLOAD_ARCHIVE_ONLY' to 0 in the script." 0
fi
fi

#上传dSYM files
sentryUpload

}


# 根据Xcode的环境变量判断是否处于Xcode环境
INFO_PLIST_FILE="${INFOPLIST_FILE}"

BuildInXcode="F"
if [ -f "${INFO_PLIST_FILE}" ]; then
BuildInXcode="T"
fi

if [ $BuildInXcode = "T" ]; then
runInXcode
else
echo "echo Not in XCode environment "
fi

配置完成之后,这样就能够在每次打包release的时候自动将dsym文件上传到自己的服务器了。

注意

关于sentry环境的区分,一开始我一直以为需要创建两个项目来进行区分sentry的环境(测试环境与正式环境)。后来发现,在一个项目中,可以通过选择不同的release内容进行区分(实际上是以bundleID)进行区分的,由于公司项目的测试环境与正式环境使用了Target进行区分且bundleID也不一样,所以刚好能够通过选择该内容进行环境的区分。

假如bundleID是同一个的话,那么此时建议创建一个新的项目进行区分即可,这样在上传dsym的时候也能够独立上传,从而避免dsym上传覆盖问题的发生。

最后

关于dsym文件的上传,个人比较倾向于第二种方法吧,因为无论使用fastlane还是XCode打包,都会执行对应的phase脚本内容,也就是说文件dsym肯定会被上传到服务器的,而且不会有遗漏情况。

怎么说呢,这玩意用着不习惯,还是比较喜欢bugly。但是由于它可以自己搭建,所以对于一些保密的项目的崩溃收集还是比较不错的一种选择,各有优劣,重在选择,你觉得呢?

你可能感兴趣的:(iOS集成Sentry崩溃收集日常操作)