From 3fb7ad2ff5ce5e8075b6e70cc41817096441bce3 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 2 Dec 2026 15:30:22 +0100 Subject: [PATCH 1/3] feat(android)!: Disable legacy profiling by default Require applications to explicitly opt into the crash-prone Debug-based profiler while keeping the legacy implementation available for version 9. Deprecate the option for removal in version 10 and preserve Perfetto continuous profiling on API 35 and newer. Refs GH-5094 Co-Authored-By: Claude --- develop-docs/feature/profiling/perfetto.md | 13 +- .../core/AndroidOptionsInitializer.java | 144 +++++++++--------- .../core/SentryPerformanceProvider.java | 9 +- .../core/AndroidOptionsInitializerTest.kt | 68 ++++++--- .../core/ManifestMetadataReaderTest.kt | 6 +- .../core/SentryPerformanceProviderTest.kt | 17 +++ .../src/main/AndroidManifest.xml | 4 + .../samples/android/ProfilingActivity.kt | 4 +- sentry/src/main/java/io/sentry/Sentry.java | 10 +- .../SentryAppStartProfilingOptions.java | 3 +- .../main/java/io/sentry/SentryOptions.java | 34 +++-- .../test/java/io/sentry/JsonSerializerTest.kt | 2 +- .../test/java/io/sentry/SentryOptionsTest.kt | 8 +- sentry/src/test/java/io/sentry/SentryTest.kt | 24 ++- 14 files changed, 217 insertions(+), 129 deletions(-) diff --git a/develop-docs/feature/profiling/perfetto.md b/develop-docs/feature/profiling/perfetto.md index d4168f8f2a4..7a7ad4a40da 100644 --- a/develop-docs/feature/profiling/perfetto.md +++ b/develop-docs/feature/profiling/perfetto.md @@ -84,12 +84,13 @@ flowchart TD ## SDK (getsentry/sentry-java) On API 35+, [`AndroidOptionsInitializer`](../../../sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java) -wires up `PerfettoContinuousProfiler` automatically. On older devices the SDK falls back -to the legacy `Debug`-based [`AndroidContinuousProfiler`](../../../sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java), -gated by the `enableLegacyProfiling` option (manifest key -`io.sentry.profiling.enable-legacy-profiling`, defaults to `true`). Only **continuous -profiling** is supported on the Perfetto path — transaction-based and app-start profiling -are not. +wires up `PerfettoContinuousProfiler` automatically. On older devices profiling is disabled +by default. Applications can temporarily opt into the legacy `Debug`-based +[`AndroidContinuousProfiler`](../../../sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java) +with the deprecated `enableLegacyProfiling` option (manifest key +`io.sentry.profiling.enable-legacy-profiling`). The legacy profiler can cause Android Runtime +crashes and will be removed in version 10. Only **continuous profiling** is supported on the +Perfetto path — transaction-based and app-start profiling are not. ### Capturing chunks diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java index 6adca10a272..5eaec55d1d2 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java @@ -304,6 +304,7 @@ static void initializeIntegrationsAndProcessors( /** Setup the correct profiler (transaction or continuous) based on the options. */ @SuppressLint("NewApi") + @SuppressWarnings("deprecation") private static void setupProfiler( final @NotNull SentryAndroidOptions options, final @NotNull Context context, @@ -311,30 +312,16 @@ private static void setupProfiler( final @Nullable ITransactionProfiler appStartTransactionProfiler, final @Nullable IContinuousProfiler appStartContinuousProfiler, final @NotNull CompositePerformanceCollector performanceCollector) { - if (options.isProfilingEnabled() || options.getProfilesSampleRate() != null) { + final boolean isTransactionProfilingRequested = + options.isProfilingEnabled() || options.getProfilesSampleRate() != null; + if (isTransactionProfilingRequested && options.isEnableLegacyProfiling()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "The legacy Android profiler is enabled. It is deprecated and will be removed " + + "in version 10."); options.setContinuousProfiler(NoOpContinuousProfiler.getInstance()); - // Transaction-based profiling always relies on the legacy Debug-based profiler, so it is - // disabled together with legacy profiling. Perfetto profiling only supports continuous - // profiling. - if (!options.isEnableLegacyProfiling()) { - options - .getLogger() - .log( - SentryLevel.WARNING, - "Transaction-based profiling (profilesSampleRate/profilesSampler) is disabled " - + "because enableLegacyProfiling is false. Transaction-based profiling always " - + "uses the legacy profiler and is not supported by Perfetto. No profiling " - + "data will be collected. Use profileSessionSampleRate for continuous " - + "profiling instead."); - options.setTransactionProfiler(NoOpTransactionProfiler.getInstance()); - if (appStartTransactionProfiler != null) { - appStartTransactionProfiler.close(); - } - if (appStartContinuousProfiler != null) { - appStartContinuousProfiler.close(true); - } - return; - } // This is a safeguard, but it should never happen, as the app start profiler should be the // continuous one. if (appStartContinuousProfiler != null) { @@ -352,54 +339,71 @@ private static void setupProfiler( options.getFrameMetricsCollector(), "options.getFrameMetricsCollector is required"))); } - } else { - options.setTransactionProfiler(NoOpTransactionProfiler.getInstance()); - // This is a safeguard, but it should never happen, as the app start profiler should be the - // transaction one. - if (appStartTransactionProfiler != null) { - appStartTransactionProfiler.close(); + return; + } + + options.setTransactionProfiler(NoOpTransactionProfiler.getInstance()); + if (appStartTransactionProfiler != null) { + appStartTransactionProfiler.close(); + } + if (isTransactionProfilingRequested) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Transaction-based profiling (profilesSampleRate/profilesSampler) is disabled " + + "because enableLegacyProfiling is false. Transaction-based profiling always " + + "uses the legacy profiler and is not supported by Perfetto. To continue " + + "using it, explicitly set enableLegacyProfiling to true. The legacy profiler " + + "is deprecated and will be removed in version 10."); + } + + if (appStartContinuousProfiler != null) { + options.setContinuousProfiler(appStartContinuousProfiler); + // If the profiler is running, we start the performance collector too, otherwise we'd miss + // measurements in app launch profiles + final @NotNull SentryId chunkId = appStartContinuousProfiler.getChunkId(); + if (appStartContinuousProfiler.isRunning() && !chunkId.equals(SentryId.EMPTY_ID)) { + performanceCollector.start(chunkId.toString()); } - if (appStartContinuousProfiler != null) { - options.setContinuousProfiler(appStartContinuousProfiler); - // If the profiler is running, we start the performance collector too, otherwise we'd miss - // measurements in app launch profiles - final @NotNull SentryId chunkId = appStartContinuousProfiler.getChunkId(); - if (appStartContinuousProfiler.isRunning() && !chunkId.equals(SentryId.EMPTY_ID)) { - performanceCollector.start(chunkId.toString()); - } - } else { - final @NotNull SentryFrameMetricsCollector frameMetricsCollector = - Objects.requireNonNull( - options.getFrameMetricsCollector(), "options.getFrameMetricsCollector is required"); - if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { - final @NotNull Context appContext = ContextUtils.getApplicationContext(context); - options.setContinuousProfiler( - new PerfettoContinuousProfiler( - options.getLogger(), - frameMetricsCollector, - () -> options.getExecutorService(), - () -> - new PerfettoProfiler( - appContext, options.getLogger(), options.getExecutorService()))); - // Report adoption of the Perfetto continuous profiling backend (API 35+). - addIntegrationToSdkVersion("PerfettoContinuousProfiling"); - } else if (options.isEnableLegacyProfiling()) { - options.setContinuousProfiler( - new AndroidContinuousProfiler( - buildInfoProvider, - frameMetricsCollector, - options.getLogger(), - options.getProfilingTracesDirPath(), - options.getProfilingTracesHz(), - () -> options.getExecutorService())); - } else { - options - .getLogger() - .log( - SentryLevel.WARNING, - "enableLegacyProfiling is disabled and device is below API 35. " - + "No profiling data will be collected."); - } + } else { + final @NotNull SentryFrameMetricsCollector frameMetricsCollector = + Objects.requireNonNull( + options.getFrameMetricsCollector(), "options.getFrameMetricsCollector is required"); + if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { + final @NotNull Context appContext = ContextUtils.getApplicationContext(context); + options.setContinuousProfiler( + new PerfettoContinuousProfiler( + options.getLogger(), + frameMetricsCollector, + () -> options.getExecutorService(), + () -> + new PerfettoProfiler( + appContext, options.getLogger(), options.getExecutorService()))); + // Report adoption of the Perfetto continuous profiling backend (API 35+). + addIntegrationToSdkVersion("PerfettoContinuousProfiling"); + } else if (options.isEnableLegacyProfiling()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "The legacy Android profiler is enabled. It is deprecated and will be removed " + + "in version 10."); + options.setContinuousProfiler( + new AndroidContinuousProfiler( + buildInfoProvider, + frameMetricsCollector, + options.getLogger(), + options.getProfilingTracesDirPath(), + options.getProfilingTracesHz(), + () -> options.getExecutorService())); + } else if (options.isContinuousProfilingEnabled()) { + options + .getLogger() + .log( + SentryLevel.WARNING, + "Continuous profiling is unavailable below API 35 unless the deprecated " + + "enableLegacyProfiling option is explicitly set to true."); } } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SentryPerformanceProvider.java b/sentry-android-core/src/main/java/io/sentry/android/core/SentryPerformanceProvider.java index c9c72f9d8d2..52b132448b4 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SentryPerformanceProvider.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SentryPerformanceProvider.java @@ -138,11 +138,16 @@ private void launchAppStartProfiler(final @NotNull AppStartMetrics appStartMetri if (!profilingOptions.isEnableLegacyProfiling()) { logger.log( SentryLevel.WARNING, - "enableLegacyProfiling is disabled and device is below API 35. " - + "App start profiling will not start."); + "App start profiling is unavailable below API 35 unless the deprecated " + + "enableLegacyProfiling option is explicitly set to true."); return; } + logger.log( + SentryLevel.WARNING, + "The legacy Android profiler is enabled. It is deprecated and will be removed in " + + "version 10."); + if (profilingOptions.isContinuousProfilingEnabled() && profilingOptions.isStartProfilerOnAppStart()) { createAndStartContinuousProfiler(context, profilingOptions, appStartMetrics); diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt index 8fe3a9cb0da..3ad330e66f5 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt @@ -409,12 +409,11 @@ class AndroidOptionsInitializerTest { } @Test - fun `init should set Android continuous profiler`() { + fun `init below API 35 uses no-op profiler by default`() { fixture.initSut() - assertNotNull(fixture.sentryOptions.transactionProfiler) - assertEquals(fixture.sentryOptions.transactionProfiler, NoOpTransactionProfiler.getInstance()) - assertTrue(fixture.sentryOptions.continuousProfiler is AndroidContinuousProfiler) + assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler) + assertEquals(NoOpContinuousProfiler.getInstance(), fixture.sentryOptions.continuousProfiler) } @Config(sdk = [35]) @@ -424,6 +423,15 @@ class AndroidOptionsInitializerTest { assertTrue(fixture.sentryOptions.continuousProfiler is PerfettoContinuousProfiler) } + @Config(sdk = [35]) + @Test + fun `init on API 35+ uses Perfetto when transaction profiling is configured by default`() { + fixture.initSut(configureOptions = { profilesSampleRate = 1.0 }) + + assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler) + assertTrue(fixture.sentryOptions.continuousProfiler is PerfettoContinuousProfiler) + } + @Config(sdk = [35]) @Test fun `init on API 35+ reports PerfettoContinuousProfiling integration`() { @@ -465,8 +473,13 @@ class AndroidOptionsInitializerTest { } @Test - fun `init with profilesSampleRate should set Android transaction profiler`() { - fixture.initSut(configureOptions = { profilesSampleRate = 1.0 }) + fun `init with profilesSampleRate and legacy profiling enabled sets transaction profiler`() { + fixture.initSut( + configureOptions = { + profilesSampleRate = 1.0 + isEnableLegacyProfiling = true + }, + ) assertNotNull(fixture.sentryOptions.transactionProfiler) assertTrue(fixture.sentryOptions.transactionProfiler is AndroidTransactionProfiler) @@ -474,8 +487,13 @@ class AndroidOptionsInitializerTest { } @Test - fun `init with profilesSampleRate 0 should set Android transaction profiler`() { - fixture.initSut(configureOptions = { profilesSampleRate = 0.0 }) + fun `init with profilesSampleRate 0 and legacy profiling enabled sets transaction profiler`() { + fixture.initSut( + configureOptions = { + profilesSampleRate = 0.0 + isEnableLegacyProfiling = true + }, + ) assertNotNull(fixture.sentryOptions.transactionProfiler) assertTrue(fixture.sentryOptions.transactionProfiler is AndroidTransactionProfiler) @@ -483,8 +501,13 @@ class AndroidOptionsInitializerTest { } @Test - fun `init with profilesSampler should set Android transaction profiler`() { - fixture.initSut(configureOptions = { profilesSampler = mock() }) + fun `init with profilesSampler and legacy profiling enabled sets transaction profiler`() { + fixture.initSut( + configureOptions = { + profilesSampler = mock() + isEnableLegacyProfiling = true + }, + ) assertNotNull(fixture.sentryOptions.transactionProfiler) assertTrue(fixture.sentryOptions.transactionProfiler is AndroidTransactionProfiler) @@ -492,13 +515,8 @@ class AndroidOptionsInitializerTest { } @Test - fun `init with profilesSampleRate and enableLegacyProfiling false noops both profilers`() { - fixture.initSut( - configureOptions = { - profilesSampleRate = 1.0 - isEnableLegacyProfiling = false - }, - ) + fun `init with profilesSampleRate noops both profilers by default`() { + fixture.initSut(configureOptions = { profilesSampleRate = 1.0 }) assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler) assertEquals(NoOpContinuousProfiler.getInstance(), fixture.sentryOptions.continuousProfiler) @@ -540,7 +558,12 @@ class AndroidOptionsInitializerTest { fun `init reuses transaction profiler of appStartMetrics, if exists`() { val appStartProfiler = mock() AppStartMetrics.getInstance().appStartProfiler = appStartProfiler - fixture.initSut(configureOptions = { profilesSampler = mock() }) + fixture.initSut( + configureOptions = { + profilesSampler = mock() + isEnableLegacyProfiling = true + }, + ) assertEquals(appStartProfiler, fixture.sentryOptions.transactionProfiler) assertEquals(fixture.sentryOptions.continuousProfiler, NoOpContinuousProfiler.getInstance()) @@ -595,7 +618,12 @@ class AndroidOptionsInitializerTest { fun `init with transaction profiling closes continuous profiler of appStartMetrics`() { val appStartContinuousProfiler = mock() AppStartMetrics.getInstance().appStartContinuousProfiler = appStartContinuousProfiler - fixture.initSut(configureOptions = { profilesSampler = mock() }) + fixture.initSut( + configureOptions = { + profilesSampler = mock() + isEnableLegacyProfiling = true + }, + ) assertNotNull(fixture.sentryOptions.transactionProfiler) assertNotEquals( @@ -616,7 +644,7 @@ class AndroidOptionsInitializerTest { fun `init with continuous profiling closes transaction profiler of appStartMetrics`() { val appStartProfiler = mock() AppStartMetrics.getInstance().appStartProfiler = appStartProfiler - fixture.initSut() + fixture.initSut(configureOptions = { isEnableLegacyProfiling = true }) assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler) assertNotNull(fixture.sentryOptions.continuousProfiler) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt index 8bc4fbf9f0f..db27d70fb2a 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt @@ -1820,14 +1820,14 @@ class ManifestMetadataReaderTest { @Test fun `applyMetadata reads enableLegacyProfiling flag to options`() { // Arrange - val bundle = bundleOf(ManifestMetadataReader.ENABLE_LEGACY_PROFILING to false) + val bundle = bundleOf(ManifestMetadataReader.ENABLE_LEGACY_PROFILING to true) val context = fixture.getContext(metaData = bundle) // Act ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider) // Assert - assertFalse(fixture.options.isEnableLegacyProfiling) + assertTrue(fixture.options.isEnableLegacyProfiling) } @Test @@ -1839,7 +1839,7 @@ class ManifestMetadataReaderTest { ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider) // Assert - assertTrue(fixture.options.isEnableLegacyProfiling) + assertFalse(fixture.options.isEnableLegacyProfiling) } @Test diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/SentryPerformanceProviderTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/SentryPerformanceProviderTest.kt index f5a6bfcd919..624573761bd 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/SentryPerformanceProviderTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/SentryPerformanceProviderTest.kt @@ -168,6 +168,21 @@ class SentryPerformanceProviderTest { ) } + @Test + fun `when legacy profiling is disabled, profiler is not started`() { + fixture.getSut { config -> writeConfig(config, enableLegacyProfiling = false) } + assertNull(AppStartMetrics.getInstance().appStartProfiler) + assertNull(AppStartMetrics.getInstance().appStartContinuousProfiler) + verify(fixture.logger) + .log( + eq(SentryLevel.WARNING), + eq( + "App start profiling is unavailable below API 35 unless the deprecated " + + "enableLegacyProfiling option is explicitly set to true.", + ), + ) + } + @Test fun `when profiling is disabled, profiler is not started`() { fixture.getSut { config -> @@ -325,6 +340,7 @@ class SentryPerformanceProviderTest { continuousProfileSampled: Boolean = true, isEnableAppStartProfiling: Boolean = true, isStartProfilerOnAppStart: Boolean = true, + enableLegacyProfiling: Boolean = true, profilingTracesDirPath: String = traceDir.absolutePath, ) { val appStartProfilingOptions = SentryAppStartProfilingOptions() @@ -339,6 +355,7 @@ class SentryPerformanceProviderTest { appStartProfilingOptions.profilingTracesHz = 101 appStartProfilingOptions.isEnableAppStartProfiling = isEnableAppStartProfiling appStartProfilingOptions.isStartProfilerOnAppStart = isStartProfilerOnAppStart + appStartProfilingOptions.isEnableLegacyProfiling = enableLegacyProfiling JsonSerializer(SentryOptions.empty()) .serialize(appStartProfilingOptions, FileWriter(configFile)) } diff --git a/sentry-android-integration-tests/sentry-samples-android/src/main/AndroidManifest.xml b/sentry-android-integration-tests/sentry-samples-android/src/main/AndroidManifest.xml index 861d5b4e67f..59360404fa8 100644 --- a/sentry-android-integration-tests/sentry-samples-android/src/main/AndroidManifest.xml +++ b/sentry-android-integration-tests/sentry-samples-android/src/main/AndroidManifest.xml @@ -260,6 +260,10 @@ + + = 35 } val isContinuousEnabled = remember { options.isContinuousProfilingEnabled } + val isLegacyEnabled = remember { options.isEnableLegacyProfiling } var showProgress by remember { mutableStateOf(false) } var manualActive by remember { mutableStateOf(false) } @@ -55,7 +56,8 @@ class ProfilingActivity : ComponentActivity() { when { !isContinuousEnabled -> stringResource(R.string.profiling_status_none) isPerfetto -> stringResource(R.string.profiling_status_perfetto) - else -> stringResource(R.string.profiling_status_legacy) + isLegacyEnabled -> stringResource(R.string.profiling_status_legacy) + else -> stringResource(R.string.profiling_status_none) } Scaffold(topBar = { TopAppBar(title = { Text("Profiling") }) }) { innerPadding -> diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index ce286600477..27318b389ec 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -72,9 +72,12 @@ private Sentry() {} /** whether to use a single (global) Scopes as opposed to one per thread. */ private static volatile boolean globalHubMode = GLOBAL_HUB_DEFAULT_MODE; + private static final @NotNull String PRE_V9_APP_START_PROFILING_CONFIG_FILE_NAME = + "app_start_profiling_config"; + @ApiStatus.Internal public static final @NotNull String APP_START_PROFILING_CONFIG_FILE_NAME = - "app_start_profiling_config"; + "app_start_profiling_config_v2"; @SuppressWarnings("CharsetObjectCanBeUsed") private static final Charset UTF_8 = Charset.forName("UTF-8"); @@ -449,7 +452,10 @@ private static void handleAppStartProfilingConfig( final @NotNull File appStartProfilingConfigFile = new File(cacheDir, APP_START_PROFILING_CONFIG_FILE_NAME); try { - // We always delete the config file for app start profiling + // We always delete the config file for app start profiling. Pre-v9 files enabled the + // legacy profiler by default, so they must not be used after that default changed. + FileUtils.deleteRecursively( + new File(cacheDir, PRE_V9_APP_START_PROFILING_CONFIG_FILE_NAME)); FileUtils.deleteRecursively(appStartProfilingConfigFile); if (!options.isEnableAppStartProfiling() && !options.isStartProfilerOnAppStart()) { return; diff --git a/sentry/src/main/java/io/sentry/SentryAppStartProfilingOptions.java b/sentry/src/main/java/io/sentry/SentryAppStartProfilingOptions.java index a59591fdab3..684d9bd1f85 100644 --- a/sentry/src/main/java/io/sentry/SentryAppStartProfilingOptions.java +++ b/sentry/src/main/java/io/sentry/SentryAppStartProfilingOptions.java @@ -43,9 +43,10 @@ public SentryAppStartProfilingOptions() { profilingTracesHz = 0; isEnableAppStartProfiling = true; isStartProfilerOnAppStart = false; - enableLegacyProfiling = true; + enableLegacyProfiling = false; } + @SuppressWarnings("deprecation") SentryAppStartProfilingOptions( final @NotNull SentryOptions options, final @NotNull TracesSamplingDecision samplingDecision) { diff --git a/sentry/src/main/java/io/sentry/SentryOptions.java b/sentry/src/main/java/io/sentry/SentryOptions.java index 84723a9d581..9c2f5f2b981 100644 --- a/sentry/src/main/java/io/sentry/SentryOptions.java +++ b/sentry/src/main/java/io/sentry/SentryOptions.java @@ -690,12 +690,11 @@ public class SentryOptions implements RateLimiterConfig { private boolean startProfilerOnAppStart = false; /** - * When false, the legacy {@code Debug}-based profiler is disabled on API < 35 devices. On API + * When true, the legacy {@code Debug}-based profiler is enabled on API < 35 devices. On API * 35+ devices, Android's {@code ProfilingManager} (Perfetto-based stack sampling) is always used - * regardless of this setting. This option will be deprecated in the next major release and - * removed in the one after. + * regardless of this setting. */ - private boolean enableLegacyProfiling = true; + private boolean enableLegacyProfiling = false; /** * Controls the deadline timeout in milliseconds for automatic transactions. When set to a @@ -2399,26 +2398,31 @@ public void setStartProfilerOnAppStart(final boolean startProfilerOnAppStart) { * used for continuous profiling regardless of this setting) as well as transaction-based * profiling ({@code profilesSampleRate}/{@code profilesSampler}) on all devices, since * transaction-based profiling always relies on the legacy profiler and is not supported by - * Perfetto. This option will be deprecated in the next major release and removed in the one - * after. + * Perfetto. * - * @return true if legacy profiling is enabled (default). + * @return true if legacy profiling is enabled. The default is false. + * @deprecated The legacy profiler can cause Android Runtime crashes and will be removed in + * version 10. Transaction-based profiling on all API levels and continuous profiling below + * API 35 will no longer be supported after its removal. */ + @Deprecated public boolean isEnableLegacyProfiling() { return enableLegacyProfiling; } /** - * Set whether the legacy {@code Debug}-based profiler is enabled. Set to {@code false} to disable - * continuous profiling on devices below API 35 (on API 35+ devices, Android's {@code - * ProfilingManager} / Perfetto is always used for continuous profiling and this setting has no - * effect) as well as transaction-based profiling ({@code profilesSampleRate}/{@code - * profilesSampler}) on all devices, since transaction-based profiling always relies on the legacy - * profiler and is not supported by Perfetto. This option will be deprecated in the next major - * release and removed in the one after. + * Set whether the legacy {@code Debug}-based profiler is enabled. Set to {@code true} to enable + * continuous profiling on devices below API 35 as well as transaction-based profiling ({@code + * profilesSampleRate}/{@code profilesSampler}) on all devices. On API 35+ devices, Android's + * {@code ProfilingManager} / Perfetto is always used for continuous profiling and this setting + * has no effect on it. * - * @param enableLegacyProfiling false to disable legacy profiling. + * @param enableLegacyProfiling true to enable legacy profiling. + * @deprecated The legacy profiler can cause Android Runtime crashes and will be removed in + * version 10. Transaction-based profiling on all API levels and continuous profiling below + * API 35 will no longer be supported after its removal. */ + @Deprecated public void setEnableLegacyProfiling(final boolean enableLegacyProfiling) { this.enableLegacyProfiling = enableLegacyProfiling; } diff --git a/sentry/src/test/java/io/sentry/JsonSerializerTest.kt b/sentry/src/test/java/io/sentry/JsonSerializerTest.kt index 1b33fba6b75..f6c8e08afb1 100644 --- a/sentry/src/test/java/io/sentry/JsonSerializerTest.kt +++ b/sentry/src/test/java/io/sentry/JsonSerializerTest.kt @@ -1346,7 +1346,7 @@ class JsonSerializerTest { "\"trace_sampled\":false,\"trace_sample_rate\":0.1,\"profiling_traces_dir_path\":null,\"is_profiling_enabled\":false," + "\"is_continuous_profiling_enabled\":false,\"profile_lifecycle\":\"TRACE\",\"profiling_traces_hz\":65," + "\"is_enable_app_start_profiling\":false,\"is_start_profiler_on_app_start\":true," + - "\"enable_legacy_profiling\":true}" + "\"enable_legacy_profiling\":false}" assertEquals(expected, actual) } diff --git a/sentry/src/test/java/io/sentry/SentryOptionsTest.kt b/sentry/src/test/java/io/sentry/SentryOptionsTest.kt index 33bbe019543..dce5bdb47e3 100644 --- a/sentry/src/test/java/io/sentry/SentryOptionsTest.kt +++ b/sentry/src/test/java/io/sentry/SentryOptionsTest.kt @@ -913,14 +913,14 @@ class SentryOptionsTest { } @Test - fun `when options are initialized, enableLegacyProfiling is set to true by default`() { - assertTrue(SentryOptions().isEnableLegacyProfiling) + fun `when options are initialized, enableLegacyProfiling is set to false by default`() { + assertFalse(SentryOptions().isEnableLegacyProfiling) } @Test fun `when setEnableLegacyProfiling is called, value is set`() { - val options = SentryOptions().apply { isEnableLegacyProfiling = false } - assertFalse(options.isEnableLegacyProfiling) + val options = SentryOptions().apply { isEnableLegacyProfiling = true } + assertTrue(options.isEnableLegacyProfiling) } @Test diff --git a/sentry/src/test/java/io/sentry/SentryTest.kt b/sentry/src/test/java/io/sentry/SentryTest.kt index 9fb60f6922f..3c91f9aedea 100644 --- a/sentry/src/test/java/io/sentry/SentryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryTest.kt @@ -1307,6 +1307,21 @@ class SentryTest { @Test fun `init deletes app start profiling config`() { + val path = getTempPath() + File(path).mkdirs() + val appStartProfilingConfigFile = File(path, Sentry.APP_START_PROFILING_CONFIG_FILE_NAME) + appStartProfilingConfigFile.createNewFile() + assertTrue(appStartProfilingConfigFile.exists()) + initForTest { + it.dsn = dsn + it.executorService = ImmediateExecutorService() + it.cacheDirPath = path + } + assertFalse(appStartProfilingConfigFile.exists()) + } + + @Test + fun `init deletes pre-v9 app start profiling config`() { val path = getTempPath() File(path).mkdirs() val appStartProfilingConfigFile = File(path, "app_start_profiling_config") @@ -1324,7 +1339,7 @@ class SentryTest { fun `init creates app start profiling config if isEnableAppStartProfiling and performance is enabled`() { val path = getTempPath() File(path).mkdirs() - val appStartProfilingConfigFile = File(path, "app_start_profiling_config") + val appStartProfilingConfigFile = File(path, Sentry.APP_START_PROFILING_CONFIG_FILE_NAME) appStartProfilingConfigFile.createNewFile() assertTrue(appStartProfilingConfigFile.exists()) initForTest { @@ -1342,7 +1357,7 @@ class SentryTest { fun `init creates app start profiling config if isStartProfilerOnAppStart, even with performance disabled`() { val path = getTempPath() File(path).mkdirs() - val appStartProfilingConfigFile = File(path, "app_start_profiling_config") + val appStartProfilingConfigFile = File(path, Sentry.APP_START_PROFILING_CONFIG_FILE_NAME) appStartProfilingConfigFile.createNewFile() assertTrue(appStartProfilingConfigFile.exists()) initForTest { @@ -1370,7 +1385,7 @@ class SentryTest { it.profilesSampleRate = null it.executorService = ImmediateExecutorService() } - assertTrue(File(path, "app_start_profiling_config").exists()) + assertTrue(File(path, Sentry.APP_START_PROFILING_CONFIG_FILE_NAME).exists()) } @Test @@ -1387,7 +1402,7 @@ class SentryTest { it.executorService = ImmediateExecutorService() options = it } - val appStartProfilingConfigFile = File(path, "app_start_profiling_config") + val appStartProfilingConfigFile = File(path, Sentry.APP_START_PROFILING_CONFIG_FILE_NAME) assertTrue(appStartProfilingConfigFile.exists()) val appStartOption = JsonSerializer(options) @@ -1401,6 +1416,7 @@ class SentryTest { assertTrue(appStartOption.isProfilingEnabled) assertTrue(appStartOption.isEnableAppStartProfiling) assertTrue(appStartOption.isStartProfilerOnAppStart) + assertFalse(appStartOption.isEnableLegacyProfiling) } @Test From 6326105748cb980dbad713ab6b5756cdfe3eb9cd Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 2 Dec 2026 15:42:48 +0100 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1ad234766c..d88c9db5b3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ ### Breaking Changes +- Android: Disable the legacy `Debug`-based profiler by default. Set `enableLegacyProfiling` or the `io.sentry.profiling.enable-legacy-profiling` manifest option to `true` to continue profiling below API 35 or using transaction-based profiling ([#6837](https://github.com/getsentry/sentry-java/pull/6837)) + - `enableLegacyProfiling` is deprecated and will be removed in version 10. Perfetto continuous profiling on API 35 and newer is unaffected. - Android: Narrow the SDK's consumer keep rules, so R8 can optimize more of your app ([#6787](https://github.com/getsentry/sentry-java/pull/6787)) - Members of `io.sentry` enums are no longer kept. Enum constants can be renamed or removed. - Only the name and no-arg constructor of `SentryAndroidOptions` are kept, instead of every member of every `SentryOptions` subclass. From b4efbc6695b75512d70d344e018bc1978a486f7e Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 2 Dec 2026 16:49:15 +0100 Subject: [PATCH 3/3] fix(android): Stop stale legacy app-start profiler Close a continuous profiler started from previous-launch configuration when the current options disable legacy profiling. This prevents the current opt-out from retaining the crash-prone legacy backend. Co-Authored-By: Claude --- .../core/AndroidOptionsInitializer.java | 5 +++- .../core/AndroidOptionsInitializerTest.kt | 28 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java index 5eaec55d1d2..d0a151ea5f7 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java @@ -358,7 +358,7 @@ private static void setupProfiler( + "is deprecated and will be removed in version 10."); } - if (appStartContinuousProfiler != null) { + if (appStartContinuousProfiler != null && options.isEnableLegacyProfiling()) { options.setContinuousProfiler(appStartContinuousProfiler); // If the profiler is running, we start the performance collector too, otherwise we'd miss // measurements in app launch profiles @@ -367,6 +367,9 @@ private static void setupProfiler( performanceCollector.start(chunkId.toString()); } } else { + if (appStartContinuousProfiler != null) { + appStartContinuousProfiler.close(true); + } final @NotNull SentryFrameMetricsCollector frameMetricsCollector = Objects.requireNonNull( options.getFrameMetricsCollector(), "options.getFrameMetricsCollector is required"); diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt index 3ad330e66f5..8291c524610 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt @@ -577,7 +577,7 @@ class AndroidOptionsInitializerTest { fun `init reuses continuous profiler of appStartMetrics, if exists`() { val appStartContinuousProfiler = mock() AppStartMetrics.getInstance().appStartContinuousProfiler = appStartContinuousProfiler - fixture.initSut() + fixture.initSut(configureOptions = { isEnableLegacyProfiling = true }) assertEquals(fixture.sentryOptions.transactionProfiler, NoOpTransactionProfiler.getInstance()) assertEquals(appStartContinuousProfiler, fixture.sentryOptions.continuousProfiler) @@ -587,6 +587,25 @@ class AndroidOptionsInitializerTest { assertNull(AppStartMetrics.getInstance().appStartContinuousProfiler) } + @Test + fun `init with transaction profiling and legacy disabled closes app start continuous profiler`() { + val appStartContinuousProfiler = mock() + AppStartMetrics.getInstance().appStartContinuousProfiler = appStartContinuousProfiler + fixture.initSut( + configureOptions = { + profilesSampler = mock() + isEnableLegacyProfiling = false + }, + ) + + assertEquals(NoOpTransactionProfiler.getInstance(), fixture.sentryOptions.transactionProfiler) + assertEquals(NoOpContinuousProfiler.getInstance(), fixture.sentryOptions.continuousProfiler) + verify(appStartContinuousProfiler).close(eq(true)) + + assertNull(AppStartMetrics.getInstance().appStartProfiler) + assertNull(AppStartMetrics.getInstance().appStartContinuousProfiler) + } + @Test fun `init starts performance collector if continuous profiler of appStartMetrics is running`() { val appStartContinuousProfiler = mock() @@ -596,7 +615,12 @@ class AndroidOptionsInitializerTest { whenever(appStartContinuousProfiler.chunkId).thenReturn(chunkId) AppStartMetrics.getInstance().appStartContinuousProfiler = appStartContinuousProfiler - fixture.initSut(configureOptions = { compositePerformanceCollector = mockPerformanceCollector }) + fixture.initSut( + configureOptions = { + compositePerformanceCollector = mockPerformanceCollector + isEnableLegacyProfiling = true + }, + ) verify(mockPerformanceCollector).start(eq(chunkId.toString())) }