ManageEngine Mobile APM Insight SDK for Android 帮助您监控真实用户设备上原生 Android 应用的性能和稳定性。它提供可操作的应用行为洞察,并支持与 Java 和 Kotlin 应用的无缝集成。
深入了解应用的健康状况和性能:
集成后,SDK 可帮助您:
| 要求 | 最低要求 |
|---|---|
| Android SDK (compileSdk) | API 21(Lollipop)或更高版本 |
| Kotlin(如适用) | 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"
}
Kotlin DSL(build.gradle.kts)
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.
*/
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
MEAPMInsight.initMonitoring( context = this )
}
}
注意: 如果在应用启动后初始化 SDK,请传入活动实例作为上下文,确保屏幕及生命周期监控立即生效。
ManageEngine APM Insight SDK 会自动捕获所有 未捕获异常。每个崩溃报告包含发生时间戳和完整堆栈跟踪,便于根因分析。
MEAPMInsight.enableErrorReporting()
检测主线程长时间阻塞导致的应用无响应(ANR)事件,防止 Android 弹出“应用无响应”对话框。须在 initMonitoring()
// 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. 启用碎片跟踪
MEAPMInsight.enableFragmentSupport()
选项 1:全局启用
选项 2:仅针对特定活动启用
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager.registerFragmentLifecycleCallbacks(
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)
val client = OkHttpClient.Builder()
.addNetworkInterceptor(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 会记录平均执行时间。请使用清晰、一致的名称准确跟踪业务操作。
val transaction = Apm.startTransaction("List Articles")
// Start predefined HTTP component
val httpComponent = transaction.startComponent(Component.TYPE_HTTP)
// Start custom component
val articlesComponent = transaction.startComponent("Download Articles")
// Download articles
transaction.stopComponent(articlesComponent)
for (article in articles) {
val thumbnailComponent = transaction.startComponent("Download Thumbnail")
// Download thumbnail
transaction.stopComponent(thumbnailComponent)
}
// Stop HTTP component
transaction.stopComponent(httpComponent)
// Stop transaction
Apm.stopTransaction(transaction)
使用下列 API 立即上传已记录的遥测数据,避免等待默认的 60 秒上传周期。
MEAPMInsight.flush()
传入指定 URL 列表,排除这些请求的跟踪。
MEAPMInsightexcludeHttpCalls(listOf("example.com", "test.com"))
传入指定屏幕名称列表,排除这些屏幕的跟踪。
MEAPMInsight.excludeScreens(listOf("customScreenName"))
通过以下 API 停止所有数据采集(屏幕、HTTP 调用、事务、崩溃、ANR 监测)。
MEAPMInsight.stopMonitoring()
SDK 自动对已知敏感查询参数和头部的值进行脱敏处理,发送服务器前替换为 *****。自动脱敏的键包括:
如需添加自定义敏感键,请使用以下 API:
MEAPMInsight.addSensitiveParams(listOf("x-custom-token", "account_id", "ssn"))
若使用代码混淆,请将以下规则添加至 proguard-rules.pro 文件:
-keep class manageengine.mobileapm.android.apm.** { *; }
-dontwarn manageengine.mobileapm.android.apm.**