xref: /aosp_15_r20/external/leakcanary2/docs/uploading.md (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)
1# Uploading analysis results
2
3You can add an `EventListener` to upload the analysis result to a server of your choosing:
4
5```kotlin
6class DebugExampleApplication : ExampleApplication() {
7
8  override fun onCreate() {
9    super.onCreate()
10    val analysisUploadListener = EventListener { event ->
11      if (event is HeapAnalysisSucceeded) {
12        val heapAnalysis = event.heapAnalysis
13        TODO("Upload heap analysis to server")
14      }
15    }
16
17    LeakCanary.config = LeakCanary.config.run {
18      copy(eventListeners = eventListeners + analysisUploadListener)
19    }
20  }
21}
22```
23
24## Uploading to Bugsnag
25
26A leak trace has a lot in common with a stack trace, so if you lack the engineering resources to
27build a backend for LeakCanary, you can instead upload leak traces to a crash reporting backend.
28The client needs to support grouping via custom client-side hashing as well as custom metadata with
29support for newlines.
30
31!!! info
32    As of this writing, the only known library suitable for uploading leaks is the Bugsnag client.
33    If you managed to make it work with another library, please [file an issue](https://github.com/square/leakcanary/issues/new/choose).
34
35Create a [Bugsnag account](https://app.bugsnag.com/user/new/), create a new project for leak
36reporting and grab an **API key**. Make sure the app has the `android.permission.INTERNET`
37permission then add the [latest version](https://docs.bugsnag.com/platforms/android/) of the
38Bugsnag Android client library to `build.gradle`:
39
40```groovy
41dependencies {
42  // debugImplementation because LeakCanary should only run in debug builds.
43  debugImplementation 'com.squareup.leakcanary:leakcanary-android:{{ leak_canary.release }}'
44  debugImplementation "com.bugsnag:bugsnag-android:$bugsnagVersion"
45}
46```
47
48!!! info
49    If you're only using Bugsnag for uploading leaks, then you do not need to set up the Bugsnag
50    Gradle plugin or to configure the API key in your app manifest.
51
52Create a new `BugsnagLeakUploader`:
53
54--8<-- "docs/snippets/bugsnag-uploader.md"
55
56Then add an `EventListener` to upload the analysis result to Bugsnag:
57
58```kotlin
59class DebugExampleApplication : ExampleApplication() {
60
61  override fun onCreate() {
62    super.onCreate()
63    LeakCanary.config = LeakCanary.config.copy(
64        onHeapAnalyzedListener = BugsnagLeakUploader(applicationContext = this)
65    )
66  }
67}
68```
69
70You should start seeing leaks reported into Bugsnag, grouped by their leak signature:
71
72![list](images/bugsnag-list.png)
73
74The `LEAK` tab contains the leak trace:
75
76![leak](images/bugsnag-leak.png)
77