This topic describes how to use the client diagnosis function on Android clients. In the console, you can send diagnosis tasks through two channels: Message Push Service (MPS) and Mobile Sync Service (MSS).
To diagnose an Android client, follow these steps:
Prerequisites
You have integrated the Moblie Analysis Service component, and the log reporting feature works correctly. For more information, see Integrate Mobile Analysis Service into an Android client.
You have integrated the Message Push Service or Mobile Sync Service component.
If you use the Message Push Service channel, follow the steps in Getting Started to Integrate Message Push Service to an Android Client.
If you use the Mobile Sync Service channel, see Integrate with Android Integrate Mobile Sync Service into an Android client for the integration steps.
Initialize the diagnosis service
The diagnosis service supports pulling diagnostic logs through the Mobile Sync Service or Message Push Service channel. The initialization method is different for each channel.
Use the Message Push Service channel
If you use the Message Push Service channel, perform the following operations after the app starts:
Initialize the push service to establish a persistent connection between the client and the Message Push Service gateway. The push software development kit (SDK) maintains this self-built channel. Initialize the push SDK
Bind a user ID. This ID is defined by the developer. It can be a user identity from your user system or another parameter that can be mapped to a specific user, such as an account or phone number. Report a user ID
Use the Mobile Sync Service channel
If you use the Mobile Sync Service channel, call the following methods after the app starts to initialize the channel.
Trigger a diagnostic task by user
// Set the userId MPLogger.setUserId(String userId); // Initialize the Sync channel. You must set the userId first, or the initialization will fail. MPDiagnose.initSyncChannel(Context context);Trigger a diagnostic task by device
NoteTriggering diagnostic tasks by device is supported only in baseline 10.2.3.73 and later.
// Set the userId MPLogger.setUserId(String userId); // Initialize the Sync channel. You must set the userId first, or the initialization will fail. MPDiagnose.initSyncDeviceChannel(Context context);
Write diagnostic logs
When the client receives a diagnostic task, it only uploads logs that were written using the mPaaS logging tool, MPLogger. Therefore, you must use MPLogger instead of android.util.Log to write logs. MPLogger provides log level methods that are similar to the native Log class:
void verbose(String tag, String msg);
void debug(String tag, String msg);
void info(String tag, String msg);
void warn(String tag, String msg);
void warn(String tag, Throwable t);
void warn(String tag, String msg, Throwable tr);
void error(String tag, String msg);
void error(String tag, Throwable t);
void error(String tag, String msg, Throwable t);
void print(String tag, String msg);
void print(String tag, Throwable t);Logs written by MPLogger are displayed in logcat for debug packages but not for release packages. The storage directories for diagnostic logs on the device are:
Debug package:
/sdcard/[PackageName]/applog. If this directory is not writable, logs are written to the release package directory.ImportantIf targetSdkVersion is 30 or later and the phone's operating system is 11 or later, the storage directory is:
/storage/emulated/0/Android/data/com.mpaas.demo/cache/[PackageName]/applog/.Release package:
/data/data/[PackageName]/files/applog.
Customize diagnostic log storage parameters
By default, local log storage retains data for 7 days with a file size limit of 15 MB. If this limit is exceeded, one-quarter of the log files are deleted.
Configure a <meta-data> element in the manifest.
// Maximum size of log files in MB
<meta-data android:name="category_applog_max_size" android:value="15" />
// Log storage duration in days
<meta-data android:name="category_applog_expires_time" android:value="7" />Customizing diagnostic log storage parameters is supported only in baseline 10.2.3.73 and later.
Pull logs from the console
You can pull logs written by the mPaaS logging tool from the console. This feature helps you quickly analyze crashes or exceptions that occur on specific device models or for specific users.
Step 1: Create a log pull task
Log on to the mPaaS console and select your target application.
In the navigation pane on the left, click Mobile Analysis Service > Log Management.
On the Pull real-time logs tab, click the Add.
Fill in the task information. The User ID is the identifier for a user in your application's logon system. You can set it using
MPLogger.setUserId(String userId)or the method for reporting user IDs through Message Push Service.Click the OK to create the log pull task.
Step 2: Trigger the log pull task
In the list of log pull tasks, find the task you just created, select a Trigger Channel, and then click Trigger in the Operations column.
Wait for a moment and then refresh the page to check the task status:
Task processed: Click the View to download the diagnostic log.
Push/Sync service successfully called: This status indicates that the message to upload the diagnostic log has been sent, but the client has not yet received it or uploaded the log. In this case, confirm that your app process is still running. If it is not, restart the app. If the task status does not change after you restart the app, see the troubleshooting section below.
Troubleshooting
If you cannot pull logs, follow the troubleshooting steps that correspond to the channel used to send the diagnostic log task.
Use the Message Push Service channel
Follow these steps to troubleshoot:
Go to the mPaaS console > Message Push Service page. Push a regular message to your app based on the user ID to confirm that the Message Push Service channel is working correctly.
If the Message Push Service channel is working correctly, clear the logcat logs, switch to the push process, and then click Trigger again for the diagnostic task in the console. Monitor the logcat logs.
Filter for
mPush14and check if the diagnostic message is received. If not, confirm whether the push channel is disconnected.
After you confirm that the diagnostic message is received, check for the following log to confirm that the message was forwarded to
MonitorService. If an error indicates thatClientMonitorServicecannot be found, use the mPaaS plugin to update the SDK:For baseline 10.1.32, upgrade to 10.1.68 and update the component to the latest version.
For baseline 10.1.60 or 10.1.68, update the component to the latest version.

After you confirm that
MonitorServicehas started, filter forAlipayLogUploaderto check if a diagnostic log exists locally. The following log indicates that a local diagnostic log is available for upload.
The following log indicates that no diagnostic log exists.

If no diagnostic log exists, check the following items:
The time period selected for the real-time log pull task in the console must be at least 1 hour. The app must be running and writing diagnostic logs during this period.
The app must declare and dynamically request the following permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
After you confirm that a local diagnostic log exists, filter for
HttpUploadto check if the log upload was successful. AresponseCodeof 200 indicates a successful upload.
Use the Data Synchronization channel
Follow these steps to troubleshoot:
Clear the logcat logs, switch to the main process, and initialize the synchronization channel. Then, filter for
isConnectedto check if the synchronization channel connection was established successfully.
In the mPaaS console, click Trigger again for the diagnostic task. Filter for
MPDiagnoseto check if the diagnostic message was received and ifMonitorServicewas started.If no message is received, confirm that the set user ID matches the user ID entered in the diagnostic task. If an error indicates that
ClientMonitorServicecannot be found, use the mPaaS plugin to update the SDK:For baseline 10.1.32, upgrade to 10.1.68 and update the component to the latest version.
For baseline 10.1.60 or 10.1.68, update the component to the latest version.

After you confirm that
MonitorServicehas started, switch to the push process and filter forAlipayLogUploaderto check if a diagnostic log exists locally. The following log indicates that a local diagnostic log is available for upload.
The following log indicates that no diagnostic log exists.
Verify the following items:
The time period selected for the real-time log pull task in the console must be at least 1 hour. The app must be running and writing diagnostic logs during this period.
The app must declare and dynamically request the following permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
After you confirm that a local diagnostic log exists, filter for
HttpUploadto check if the log upload was successful. AresponseCodeof 200 indicates a successful upload.