tencent cloud

Tencent Cloud Observability Platform

SDK Initialization

Download
Focus Mode
Font Size
Last updated: 2026-05-25 18:01:55
This article describes how to initialize the SDK.

Example Code

Refer to the following code to initialize the SDK. We recommend that you initialize the SDK as early as possible to catch exceptions early on.
public static void initBugly(Context context) {
// 1. Pre-build initialization parameters. These initialization parameters are required.
String appID = "a278f01047"; // [Required] Product APPID.
String appKey = "1e5ab6b3-b6fa-4f9b-a3c2-743d31dffe86"; // [Required] Product APPKEY.
BuglyBuilder builder = new BuglyBuilder(appID, appKey);
// 2. [Required] Configure the domain for reporting. Because Terminal Performance Monitoring Pro (RUM Pro) and Bugly use the same SDK, the domain for reporting is used to distinguish them.
buglyBuilder.setServerHostType(BuglyBuilder.ServerHostTypeBuglyCloud);
// 3. Basic initialization parameters. We recommend that you configure these initialization parameters.
builder.uniqueId = "unique_id"; // [Recommended] Configure the device ID, which must be unique. If this is not configured, RUM Pro will generate a unique ID. This affects the statistics of device exception rates and connected devices. It is recommended to store and reuse it via SharedPreferences (SP).
builder.userId = "user_id"; // [Recommended] Configure the user ID, which affects the statistics of user exception rates. It is recommended to store and reuse it via SP. Multiple settings within the same process lifecycle are not supported.
builder.deviceModel = Build.MODEL; // [Recommended] Configure the device type. After the device model is configured, RUM Pro SDK no longer reads the system's device model.
builder.appVersion = "1.0.0"; // [Recommended] Configure the app version number. If this is not configured, the value is read from packageManager. It is recommended to configure this manually according to the application's specifications. The value must be consistent with the application version parameter used for uploading the symbol table.
builder.buildNumber = "builderNum"; // [Recommended] Configure the app build number. This is used for Java stack trace deobfuscation and must be consistent with the build number parameter used for uploading the symbol table.
builder.appVersionType = BuglyAppVersionMode.Debug; // [Recommended] Configure the version type.
// 4. Additional initialization parameters. Configure these initialization parameters as needed.
builder.appChannel = "appChannel"; // Configure the app channel.
builder.logLevel = BuglyLogLevel.LEVEL_DEBUG; // Configure the log printing level. Levels can be obtained from Bugly LogLevel.
builder.enableAllThreadStackCrash = true; // Configure whether to capture all thread stacks on a crash. This is enabled by default.
builder.enableAllThreadStackAnr = true; // Configure whether to capture all thread stacks on an Application Not Responding (ANR) error. This is enabled by default.
builder.enableCrashProtect = true; // Configure whether to enable crash protection mode during performance monitoring. This is enabled by default.
builder.debugMode = false; // Configure the debug mode, which can be enabled during the debugging phase.
builder.initAppState = BuglyBuilder.APP_STATE_FOREGROUND; // Supported since version 4.4.3.7. This parameter is optional and can be used to specify the foreground and background state of the application during Bugly SDK initialization. If this is not specified, the SDK will determine the application's foreground and background state during initialization via getRunningAppProcesses. If this is specified, the SDK will use the assigned state without calling getRunningAppProcesses.

// 5. Configure the callback methods. Configure the initialization parameters as needed.
builder.setCrashHandleListener(crashHandleListener); // Configure the crash handling callback. For details, see the callback API.
builder.setUploadHandleListener(uploadhandleListener); // Configure the crash reporting callback. For details, see the callback API.
// 6. Initialization. This must be called.
Bugly.init(context, builder);
}
Note:
Context must be ApplicationContext.
The device ID is crucial. RUM Pro uses the device ID to calculate the device exception rate. We strongly recommend that you specify the correct device ID to ensure device uniqueness.
BuglyBuilder must be created before the init method, and repeated calls to the init method should be avoided.
The Bugly.init API must be called for initialization. After initialization, you can call other APIs for customized settings. Otherwise, the settings will not take effect.
The sampling of performance monitoring items can be modified in Application Configuration > SDK Configuration. You can modify the device sampling rate to enable or disable performance monitoring items.
We recommend that you initialize the RUM Pro SDK after the user agrees to the RUM Pro SDK Personal Information Protection Rules.
The domain for reporting must be configured via BuglyBuilder.setServerHostType during initialization.



Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback