Expo React Native中Sentry无法自动上传Source Maps求助
解决Expo React Native预构建项目Sentry Source Maps未上传问题
1. 修复预构建后的原生端Source Map上传配置
由于你已经预构建了ios和android文件夹,@sentry/react-native/expo插件无法自动处理原生端的工件上传,需要手动配置:
- iOS端:
打开Xcode,进入项目的Build Phases,添加一个新的Run Script Phase,内容如下:
确保脚本在export SENTRY_AUTH_TOKEN="$SENTRY_AUTH_TOKEN" export SENTRY_ORG="你的Sentry组织名" export SENTRY_PROJECT="你的Sentry项目名" ../node_modules/@sentry/cli/bin/sentry-cli upload-dsymEmbed Frameworks之后执行。 - Android端:
在android/app/build.gradle中添加Sentry Gradle插件配置:
同时在项目根目录的apply plugin: "io.sentry.android.gradle" sentry { autoUploadProguardMapping = true uploadNativeSymbols = true includeNativeSources = true }android/build.gradle中确认已引入Sentry插件依赖:buildscript { dependencies { classpath "io.sentry:sentry-android-gradle-plugin:3.+" } }
2. 验证EAS环境变量有效性
- 确认
SENTRY_AUTH_TOKEN是项目级环境变量,而非账户级,且构建时能被读取。可以在eas.json的build配置中添加预构建钩子验证:
触发EAS构建后查看日志,若能输出令牌前缀,说明变量生效;若为空,重新检查EAS环境变量的添加方式。"build": { "production": { "hooks": { "pre-build": { "command": "echo $SENTRY_AUTH_TOKEN" } } } } - 检查令牌权限:确保SENTRY_AUTH_TOKEN拥有
project:releases、org:read权限,前往Sentry令牌管理页重新生成符合要求的令牌。
3. 手动上传Source Map测试(本地构建)
本地打包后,手动执行Sentry CLI命令上传Source Map,确认是否能成功上传:
npx sentry-cli react-native upload-sourcemaps \ --bundle ./android/app/build/generated/assets/react/production/index.android.bundle \ --sourcemap ./android/app/build/generated/sourcemaps/react/production/index.android.bundle.map \ --release "你的App版本号" \ --dist "你的构建号"
iOS端替换对应的bundle和sourcemap路径即可,成功上传后在Sentry的Releases页面查看是否有对应的工件。
4. 修正Sentry.init配置
在RootLayout的Sentry.init中,明确指定release和dist参数,确保事件与上传的工件关联:
Sentry.init({ dsn: '你的Sentry DSN', release: `${process.env.EXPO_PUBLIC_APP_VERSION}@${process.env.EXPO_PUBLIC_SENTRY_RELEASE}`, dist: `${process.env.EXPO_PUBLIC_BUILD_NUMBER}`, debug: __DEV__, });
可以通过Expo的环境变量传递版本号和构建号,确保与EAS构建时的版本信息一致。
5. 检查预构建后的Sentry配置文件
预构建后,ios/sentry.properties和android/sentry.properties文件需存在并配置正确:
defaults.url=https://sentry.io/ defaults.org=你的组织名 defaults.project=你的项目名 auth.token=$SENTRY_AUTH_TOKEN
确保文件中的变量能正确读取环境变量中的令牌。
内容的提问来源于stack exchange,提问作者SVG
相关产品推荐
相关产品推荐

