Skip to content

Commit 57e33ce

Browse files
authored
Add snippet for enabling diagnostic stack traces (#712)
* Add snippet for enabling diagnostic stack traces Demo how to enable diagnostic stack traces in Jetpack Compose for improved debugging. The snippet shows how to conditionally enable `ComposeStackTraceMode.Auto` in the `Application.onCreate` method for debug builds, while keeping it disabled in release builds to avoid performance overhead. * Apply Spotless * Set to use Auto, no debug toggle * Apply Spotless * For debug, use SourceInformation, otherwise Auto * Apply Spotless * Remove duplicate copyright * Switch back from alpha BOM * Update to new recommendations * Add super call * Apply Spotless * Add comment about enabling stacktraces at onCreate level --------- Co-authored-by: riggaroo <9973046+riggaroo@users.noreply.github.com>
1 parent 3f4078e commit 57e33ce

File tree

1 file changed

+37
-0
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/performance

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.performance
18+
19+
import android.app.Application
20+
import androidx.compose.runtime.Composer
21+
import androidx.compose.runtime.tooling.ComposeStackTraceMode
22+
23+
// [START android_compose_stacktraces_enable]
24+
// Enable stack traces at application level: onCreate
25+
class SampleStackTracesEnabledApp : Application() {
26+
27+
override fun onCreate() {
28+
super.onCreate()
29+
// Enable Compose stack traces for minified builds only.
30+
Composer.setDiagnosticStackTraceMode(ComposeStackTraceMode.Auto)
31+
32+
// Alternatively:
33+
// Enable verbose Compose stack traces for local debugging
34+
Composer.setDiagnosticStackTraceMode(ComposeStackTraceMode.SourceInformation)
35+
}
36+
}
37+
// [END android_compose_stacktraces_enable]

0 commit comments

Comments
 (0)