Skip to content

Commit 2df6b56

Browse files
markushicodex
andcommitted
perf(android): Use generated manifest metadata (#5976)
* perf(android): Use build-time manifest metadata Allow the Android Gradle plugin to provide authoritative manifest metadata so SDK initialization can skip PackageManager and Bundle unparceling. Read the injected map directly to avoid conversion overhead. Refs JAVA-531 Co-Authored-By: Codex <noreply@openai.com> * changelog * ref(android): Store metadata in manifest reader Use ManifestMetadataReader directly as the Gradle plugin injection target and remove the dedicated holder class. Co-Authored-By: OpenAI Codex <noreply@openai.com> * docs: Generalize performance changelog entry Remove the device-specific benchmark percentage from the release note. Co-Authored-By: Codex <noreply@openai.com> * docs: Update replacement PR changelog link Point the performance entry at the replacement pull request. Co-Authored-By: Codex <noreply@openai.com> --------- Co-authored-by: Codex <noreply@openai.com>
1 parent 58f0ede commit 2df6b56

3 files changed

Lines changed: 129 additions & 22 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
- Users can now attach a screenshot when submitting feedback. Enabled by default; can be disabled via `SentryFeedbackOptions.setEnableAttachScreenshot(false)` or the `io.sentry.feedback.enable-attach-screenshot` manifest flag.
99
- Requires the `androidx.activity` `>=1.8.2` dependency
1010

11+
### Performance
12+
13+
- Use manifest metadata resolved at build time to reduce Android SDK initialization overhead ([#5976](https://github.com/getsentry/sentry-java/pull/5976))
14+
1115
### Dependencies
1216

1317
- Bump Native SDK from v0.16.2 to v0.16.3 ([#5962](https://github.com/getsentry/sentry-java/pull/5962))

‎sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java‎

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
import java.util.Collections;
1919
import java.util.List;
2020
import java.util.Locale;
21+
import java.util.Map;
2122
import org.jetbrains.annotations.ApiStatus;
2223
import org.jetbrains.annotations.NotNull;
2324
import org.jetbrains.annotations.Nullable;
2425

2526
/** Class responsible for reading values from manifest and setting them to the options */
2627
final class ManifestMetadataReader {
2728

29+
// Populated by the Sentry Android Gradle plugin from the merged manifest at build time.
30+
static @Nullable Map<String, Object> manifestMetadata;
31+
2832
static final String DSN = "io.sentry.dsn";
2933
static final String DEBUG = "io.sentry.debug";
3034
static final String DEBUG_LEVEL = "io.sentry.debug.level";
@@ -220,7 +224,7 @@ static void applyMetadata(
220224
Objects.requireNonNull(options, "The options object is required.");
221225

222226
try {
223-
final Bundle metadata = getMetadata(context, options.getLogger(), buildInfoProvider);
227+
final Object metadata = getMetadata(context, options.getLogger(), buildInfoProvider);
224228
final ILogger logger = options.getLogger();
225229

226230
if (metadata != null) {
@@ -494,7 +498,7 @@ static void applyMetadata(
494498
List<String> tracePropagationTargets =
495499
readList(metadata, logger, TRACE_PROPAGATION_TARGETS);
496500

497-
if (metadata.containsKey(TRACE_PROPAGATION_TARGETS) && tracePropagationTargets == null) {
501+
if (containsKey(metadata, TRACE_PROPAGATION_TARGETS) && tracePropagationTargets == null) {
498502
options.setTracePropagationTargets(Collections.emptyList());
499503
} else if (tracePropagationTargets != null) {
500504
options.setTracePropagationTargets(tracePropagationTargets);
@@ -788,44 +792,68 @@ static void applyMetadata(
788792
}
789793

790794
private static boolean readBool(
791-
final @NotNull Bundle metadata,
795+
final @NotNull Object metadata,
792796
final @NotNull ILogger logger,
793797
final @NotNull String key,
794798
final boolean defaultValue) {
795-
final boolean value = metadata.getBoolean(key, defaultValue);
799+
final boolean value;
800+
if (metadata instanceof Bundle) {
801+
value = ((Bundle) metadata).getBoolean(key, defaultValue);
802+
} else {
803+
final Object raw = ((Map<?, ?>) metadata).get(key);
804+
value = raw instanceof Boolean ? (Boolean) raw : defaultValue;
805+
}
796806
if (logger.isEnabled(SentryLevel.DEBUG)) {
797807
logger.log(SentryLevel.DEBUG, key + " read: " + value);
798808
}
799809
return value;
800810
}
801811

802812
private static @Nullable String readString(
803-
final @NotNull Bundle metadata,
813+
final @NotNull Object metadata,
804814
final @NotNull ILogger logger,
805815
final @NotNull String key,
806816
final @Nullable String defaultValue) {
807-
final String value = metadata.getString(key, defaultValue);
817+
final String value;
818+
if (metadata instanceof Bundle) {
819+
value = ((Bundle) metadata).getString(key, defaultValue);
820+
} else {
821+
final Object raw = ((Map<?, ?>) metadata).get(key);
822+
value = raw instanceof String ? (String) raw : defaultValue;
823+
}
808824
if (logger.isEnabled(SentryLevel.DEBUG)) {
809825
logger.log(SentryLevel.DEBUG, key + " read: " + value);
810826
}
811827
return value;
812828
}
813829

814830
private static @NotNull String readStringNotNull(
815-
final @NotNull Bundle metadata,
831+
final @NotNull Object metadata,
816832
final @NotNull ILogger logger,
817833
final @NotNull String key,
818834
final @NotNull String defaultValue) {
819-
final String value = metadata.getString(key, defaultValue);
835+
final String value;
836+
if (metadata instanceof Bundle) {
837+
value = ((Bundle) metadata).getString(key, defaultValue);
838+
} else {
839+
final Object raw = ((Map<?, ?>) metadata).get(key);
840+
value = raw instanceof String ? (String) raw : defaultValue;
841+
}
820842
if (logger.isEnabled(SentryLevel.DEBUG)) {
821843
logger.log(SentryLevel.DEBUG, key + " read: " + value);
822844
}
823845
return value;
824846
}
825847

826848
private static @Nullable List<String> readList(
827-
final @NotNull Bundle metadata, final @NotNull ILogger logger, final @NotNull String key) {
828-
final String value = metadata.getString(key);
849+
final @NotNull Object metadata, final @NotNull ILogger logger, final @NotNull String key) {
850+
final String value;
851+
if (metadata instanceof Bundle) {
852+
value = ((Bundle) metadata).getString(key);
853+
} else {
854+
final Object raw = ((Map<?, ?>) metadata).get(key);
855+
value = raw instanceof String ? (String) raw : null;
856+
}
829857
if (logger.isEnabled(SentryLevel.DEBUG)) {
830858
logger.log(SentryLevel.DEBUG, key + " read: " + value);
831859
}
@@ -837,11 +865,21 @@ private static boolean readBool(
837865
}
838866

839867
private static double readDouble(
840-
final @NotNull Bundle metadata, final @NotNull ILogger logger, final @NotNull String key) {
841-
// manifest meta-data only reads float
842-
double value = ((Float) metadata.getFloat(key, -1)).doubleValue();
843-
if (value == -1) {
844-
value = ((Integer) metadata.getInt(key, -1)).doubleValue();
868+
final @NotNull Object metadata, final @NotNull ILogger logger, final @NotNull String key) {
869+
final double value;
870+
if (metadata instanceof Bundle) {
871+
// manifest meta-data only reads float
872+
double bundleValue = ((Float) ((Bundle) metadata).getFloat(key, -1)).doubleValue();
873+
if (bundleValue == -1) {
874+
bundleValue = ((Integer) ((Bundle) metadata).getInt(key, -1)).doubleValue();
875+
}
876+
value = bundleValue;
877+
} else {
878+
final Object raw = ((Map<?, ?>) metadata).get(key);
879+
value =
880+
raw instanceof Float
881+
? ((Float) raw).doubleValue()
882+
: raw instanceof Integer ? ((Integer) raw).doubleValue() : -1;
845883
}
846884
if (logger.isEnabled(SentryLevel.DEBUG)) {
847885
logger.log(SentryLevel.DEBUG, key + " read: " + value);
@@ -850,12 +888,18 @@ private static double readDouble(
850888
}
851889

852890
private static long readLong(
853-
final @NotNull Bundle metadata,
891+
final @NotNull Object metadata,
854892
final @NotNull ILogger logger,
855893
final @NotNull String key,
856894
final long defaultValue) {
857-
// manifest meta-data only reads int if the value is not big enough
858-
final long value = metadata.getInt(key, (int) defaultValue);
895+
final long value;
896+
if (metadata instanceof Bundle) {
897+
// manifest meta-data only reads int if the value is not big enough
898+
value = ((Bundle) metadata).getInt(key, (int) defaultValue);
899+
} else {
900+
final Object raw = ((Map<?, ?>) metadata).get(key);
901+
value = raw instanceof Integer ? (Integer) raw : defaultValue;
902+
}
859903
if (logger.isEnabled(SentryLevel.DEBUG)) {
860904
logger.log(SentryLevel.DEBUG, key + " read: " + value);
861905
}
@@ -874,7 +918,7 @@ static boolean isAutoInit(final @NotNull Context context, final @NotNull ILogger
874918

875919
boolean autoInit = true;
876920
try {
877-
final Bundle metadata = getMetadata(context, logger, null);
921+
final Object metadata = getMetadata(context, logger, null);
878922
if (metadata != null) {
879923
autoInit = readBool(metadata, logger, AUTO_INIT, true);
880924
}
@@ -885,18 +929,28 @@ static boolean isAutoInit(final @NotNull Context context, final @NotNull ILogger
885929
}
886930

887931
/**
888-
* Returns the Bundle attached from the given Context
932+
* Returns build-time metadata when available, otherwise metadata attached to the given Context.
889933
*
890934
* @param context the application context
891-
* @return the Bundle attached to the PackageManager
935+
* @return metadata as a Map or PackageManager Bundle
892936
*/
893-
private static @Nullable Bundle getMetadata(
937+
private static @Nullable Object getMetadata(
894938
final @NotNull Context context,
895939
final @NotNull ILogger logger,
896940
final @Nullable BuildInfoProvider buildInfoProvider) {
941+
final @Nullable Map<String, Object> injected = manifestMetadata;
942+
if (injected != null) {
943+
return injected;
944+
}
897945
final ApplicationInfo app =
898946
ContextUtils.getApplicationInfo(
899947
context, buildInfoProvider != null ? buildInfoProvider : new BuildInfoProvider(logger));
900948
return app != null ? app.metaData : null;
901949
}
950+
951+
private static boolean containsKey(final @NotNull Object metadata, final @NotNull String key) {
952+
return metadata instanceof Bundle
953+
? ((Bundle) metadata).containsKey(key)
954+
: ((Map<?, ?>) metadata).containsKey(key);
955+
}
902956
}

‎sentry-android-core/src/test/java/io/sentry/android/core/ManifestMetadataReaderTest.kt‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import android.content.Context
44
import android.os.Bundle
55
import androidx.core.os.bundleOf
66
import androidx.test.ext.junit.runners.AndroidJUnit4
7+
import com.google.common.truth.Truth.assertThat
78
import io.sentry.FilterString
89
import io.sentry.ILogger
910
import io.sentry.ProfileLifecycle
1011
import io.sentry.SentryLevel
1112
import io.sentry.SentryReplayOptions
1213
import io.sentry.TransactionOptions
14+
import kotlin.test.AfterTest
1315
import kotlin.test.BeforeTest
1416
import kotlin.test.Test
1517
import kotlin.test.assertEquals
@@ -23,6 +25,7 @@ import org.mockito.kotlin.eq
2325
import org.mockito.kotlin.mock
2426
import org.mockito.kotlin.never
2527
import org.mockito.kotlin.verify
28+
import org.mockito.kotlin.verifyNoInteractions
2629

2730
@RunWith(AndroidJUnit4::class)
2831
class ManifestMetadataReaderTest {
@@ -42,6 +45,52 @@ class ManifestMetadataReaderTest {
4245
ContextUtils.resetInstance()
4346
}
4447

48+
@AfterTest
49+
fun `tear down`() {
50+
ManifestMetadataReader.manifestMetadata = null
51+
}
52+
53+
@Test
54+
fun `applyMetadata reads typed build-time metadata without querying context`() {
55+
val context = mock<Context>()
56+
ManifestMetadataReader.manifestMetadata =
57+
mapOf(
58+
ManifestMetadataReader.DEBUG to true,
59+
ManifestMetadataReader.DIST to "dist",
60+
ManifestMetadataReader.SAMPLE_RATE to 0.5f,
61+
ManifestMetadataReader.MAX_BREADCRUMBS to 42,
62+
)
63+
64+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
65+
66+
assertThat(fixture.options.isDebug).isTrue()
67+
assertThat(fixture.options.dist).isEqualTo("dist")
68+
assertThat(fixture.options.sampleRate).isEqualTo(0.5)
69+
assertThat(fixture.options.maxBreadcrumbs).isEqualTo(42)
70+
verifyNoInteractions(context)
71+
}
72+
73+
@Test
74+
fun `build-time metadata is authoritative when a key is absent`() {
75+
val context = mock<Context>()
76+
fixture.options.dist = "configured"
77+
ManifestMetadataReader.manifestMetadata = emptyMap()
78+
79+
ManifestMetadataReader.applyMetadata(context, fixture.options, fixture.buildInfoProvider)
80+
81+
assertThat(fixture.options.dist).isEqualTo("configured")
82+
verifyNoInteractions(context)
83+
}
84+
85+
@Test
86+
fun `isAutoInit reads build-time metadata without querying context`() {
87+
val context = mock<Context>()
88+
ManifestMetadataReader.manifestMetadata = mapOf(ManifestMetadataReader.AUTO_INIT to false)
89+
90+
assertThat(ManifestMetadataReader.isAutoInit(context, fixture.logger)).isFalse()
91+
verifyNoInteractions(context)
92+
}
93+
4594
@Test
4695
fun `isAutoInit won't throw exception and is enabled by default`() {
4796
fixture.options.setDebug(true)

0 commit comments

Comments
 (0)