You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Firebase Crashlytics中追踪CodePush发布版本

如何将CodePush版本添加到Firebase Crashlytics的崩溃数据中

你可以通过设置Crashlytics自定义属性的方式,把CodePush的版本信息注入到崩溃追踪数据里。以下是React Native端和原生端的具体实现方案:

React Native 端实现(推荐,无需修改原生代码)

如果使用@react-native-firebase/crashlytics库,可以直接在JS层完成配置:

  1. 获取CodePush当前版本信息
    在App初始化的入口文件(比如App.js/App.tsx)中,调用CodePush的API获取当前部署的版本元数据:
import CodePush from 'react-native-code-push';
import crashlytics from '@react-native-firebase/crashlytics';

// 在组件挂载或App启动时执行
useEffect(() => {
  CodePush.getUpdateMetadata().then(update => {
    if (update) {
      // 组装CodePush版本信息,格式可自定义
      const codePushInfo = `Label: ${update.label} | App Version: ${update.appVersion} | Deployment: ${update.deploymentKey}`;
      // 将信息设置为Crashlytics自定义属性
      crashlytics().setAttribute('code_push_version', codePushInfo);
    }
  });
}, []);

原生端实现(适合未使用react-native-firebase的场景)

如果需要直接在原生层配置,可分别对Android和iOS进行处理:

Android 端

在MainApplication.java的onCreate方法中添加以下代码:

import com.microsoft.codepush.react.CodePush;
import com.google.firebase.crashlytics.FirebaseCrashlytics;

@Override
public void onCreate() {
  super.onCreate();
  // 获取CodePush实例
  CodePush codePush = CodePush.getInstance();
  if (codePush != null) {
    String updateLabel = codePush.getUpdateLabel();
    String packageVersion = codePush.getPackageVersion();
    
    if (updateLabel != null) {
      FirebaseCrashlytics.getInstance().setCustomKey("code_push_label", updateLabel);
    }
    if (packageVersion != null) {
      FirebaseCrashlytics.getInstance().setCustomKey("code_push_package_version", packageVersion);
    }
  }
}

iOS 端

Objective-C(AppDelegate.m)

#import <CodePush/CodePush.h>
#import <Firebase/Crashlytics.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // ... 其他初始化代码
  CodePushUpdate *currentUpdate = [CodePush currentUpdate];
  if (currentUpdate) {
    [[FIRCrashlytics crashlytics] setCustomValue:currentUpdate.label forKey:@"code_push_label"];
    [[FIRCrashlytics crashlytics] setCustomValue:currentUpdate.appVersion forKey:@"code_push_app_version"];
  }
  return YES;
}

Swift(AppDelegate.swift)

import CodePush
import FirebaseCrashlytics

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  // ... 其他初始化代码
  if let currentUpdate = CodePush.currentUpdate() {
    Crashlytics.crashlytics().setCustomValue(currentUpdate.label, forKey: "code_push_label")
    Crashlytics.crashlytics().setCustomValue(currentUpdate.appVersion, forKey: "code_push_app_version")
  }
  return true
}

验证配置

主动触发一个测试崩溃(比如调用crashlytics().crash()),然后在Firebase控制台的Crashlytics模块中查看崩溃详情,即可看到你设置的code_push_version等自定义属性。

注意事项

  • 确保CodePush和Firebase Crashlytics已经在项目中正确初始化
  • 自定义属性的设置要在崩溃发生前完成,建议放在App启动的早期阶段
  • 可根据需求调整版本信息的格式,比如加入部署环境名称(通过update.deploymentKey关联)

内容的提问来源于stack exchange,提问作者Alyx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 12:56:08