ManageEngine Mobile APM Insight SDK for Android 帮助您监控原生 Android 应用在真实用户设备上的性能和稳定性。它提供可操作的应用行为洞察,并支持与 Java 和 Kotlin 应用的无缝集成。
深入了解您的应用健康状况和性能:
集成后,SDK 使您能够:
| 需求 | 最低要求 |
|---|---|
| Android SDK(compileSdk) | API 21(Lollipop)或更高版本 |
| Java | 1.8+ |
| AndroidX | 必须启用(gradle.properties 中 android.useAndroidX=true) |
| ManageEngine Applications Manager - | 一个带有效 AppKey 的活动监控器 |
在将 ManageEngine APM Insight Android SDK 添加到您的 Android 应用之前,您需要创建一个移动应用监控器,并从 Applications Manager web 下载配置(apm_config.json)文件。
Groovy(build.gradle)
dependencies {
implementation "com.manageengine.apminsight:mobileapm:3.0.0"
}
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven {
url = uri("https://maven.zohodl.com/")
content {
includeGroup("com.manageengine.apminsight")
}
}
}
}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
/**
* Initializes the ME APM Insight agent.
* @param context The application context.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
MEAPMInsight.initMonitoring( this );
}
}
1. SDK 初始化 注意:
2. 启用崩溃报告 ManageEngine APM Insight SDK 会自动捕获所有未捕获异常
MEAPMInsight.enableErrorReporting();
3. 启用 ANR 监控
// Default timeout (5 seconds)
MEAPMInsight.startANRWatching();
// Custom timeout// Report ANR if the main thread is blocked for more than 3 seconds
MEAPMInsight.startANRWatching(3000);
//Stop ANR Monitoring
MEAPMInsight.stopANRWatching();
}
4. 启用 Fragment 跟踪
MEAPMInsight.enableFragmentSupport();
选项 1:全局启用
选项 2:仅针对特定活动启用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getSupportFragmentManager();
.registerFragmentLifecycleCallbacks(
new MEAPMInsightFragmentLifecycleCallbacks(),
true
);
}
5. 设置环境
/**
*@param {String} environment
Custom environment types like debug, release, or beta release.
*/
MEAPMInsight.setEnvironment("release");
6. 设置自定义用户 ID
将遥测数据与特定用户关联。SDK 会在存储或传输前自动对用户 ID 进行哈希处理(SHA-256)。默认情况下,SDK 会生成唯一用户 ID。若需自定义用户 ID,可通过以下语法实现。此功能适用于跟踪特定用户的指标或调试问题。 重要提示:
/**
*@param {String} userId
*/
MEAPMInsight.setUserId("user_12345");
7. HTTP 调用跟踪
java.net.HttpURLConnection
javax.net.ssl.HttpsURLConnection
OkHttp (when the SDK interceptor is added)
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(new MEAPMInsightInterceptor())
.build();
SDK 默认自动监控通过以下库发出的网络请求,无需额外配置:
/**
* @param {String} url
* The complete request URL.
*
* @param {String} requestMethod
* The HTTP method used (e.g., GET, POST, PUT, DELETE).
*
* @param {long} startTime
* The request start time in milliseconds.
* You can obtain this using System.currentTimeMillis().
*
* @param {long} loadTime
* The total time taken to complete the request, measured in milliseconds.
*
* @param {int} responseCode
* The HTTP response status code returned by the server.
*
* @param {String} screen
* The name of the screen from which the HTTP call was initiated.
*/
Apm.addHttpCalls(
"https://www.example.com",
"GET",
System.currentTimeMillis(),
526,
200,
"PaymentScreen"
);
8. 屏幕跟踪
/**
*@param {String} screenName
*@param {String} orientation
*@param {long} loadTime measured in millis
*@param {long} startTime measured in millis
*/
Apm.addScreen("DetailScreen", "portrait", 50, 1642743078700);
Start a transaction using
Apm.startTransaction("Transaction_Name")
before a long-running operation and call stop() when it completes.
1. 事务
同名事务被归为一组。
当同一操作多次使用相同事务名称执行时,SDK 会记录这些执行的平均耗时。
/**
* Transaction
*
* @param {String} transactionName
* A meaningful name representing the overall business operation.
*
* Component
*
* @param {String} componentName
* A descriptive name for the sub-operation being measured.
*
* @param {int} componentType
* Predefined component type (e.g., Component.TYPE_HTTP).
*/
Transaction transaction = Apm.startTransaction("List Articles");
// Start predefined HTTP component
Component httpComponent = transaction.startComponent(Component.TYPE_HTTP);
// Start custom component
Component articlesComponent = transaction.startComponent("Download Articles");
// Download articles
transaction.stopComponent(articlesComponent);
for (Article article : articles) {
Component thumbnailComponent =
transaction.startComponent("Download Thumbnail");
// Download thumbnail
transaction.stopComponent(thumbnailComponent);
}
// Stop HTTP component
transaction.stopComponent(httpComponent);
// Stop transaction
Apm.stopTransaction(transaction);
1. 刷新数据
MEAPMInsight.flush();
2. 排除 HTTP 调用
/**
@param {Lis<String>} listOfUrls
*/
MEAPMInsight.excludeHttpCalls(Arrays.asList("example.com"));
3. 排除屏幕
/*
* param
* List<String> listOfScreens
*/
MEAPMInsight.excludeScreens(Arrays.asList("customScreenName"));
4. 停止监控
MEAPMInsight.stopMonitoring();
5. 隐藏敏感数据
sessionid, session_id, session_token
List<String> customSensitiveKeys = Arrays.asList("x-custom-token", "account_id", "ssn");
MEAPMInsight.addSensitiveParams(customSensitiveKeys);
6. ProGuard / R8 规则 若启用代码混淆,请将以下规则添加至 AndroidManifest.xml
-keep class manageengine.mobileapm.android.apm.** { *; }
-dontwarn manageengine.mobileapm.android.apm.**