Skip to content

Commit a116356

Browse files
authored
Merge 5b73955 into 9762f09
2 parents 9762f09 + 5b73955 commit a116356

28 files changed

+121
-29
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Fix ensure Application Context is used even when SDK is initialized via Activity Context ([#3669](https://github.com/getsentry/sentry-java/pull/3669))
8+
39
## 7.14.0
410

511
### Features

buildSrc/src/main/java/Config.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ object Config {
5353
val appCompat = "androidx.appcompat:appcompat:1.3.0"
5454
val timber = "com.jakewharton.timber:timber:4.7.1"
5555
val okhttp = "com.squareup.okhttp3:okhttp:$okHttpVersion"
56-
val leakCanary = "com.squareup.leakcanary:leakcanary-android:2.8.1"
56+
val leakCanary = "com.squareup.leakcanary:leakcanary-android:2.14"
5757
val constraintLayout = "androidx.constraintlayout:constraintlayout:2.1.3"
5858

5959
private val lifecycleVersion = "2.2.0"
@@ -197,6 +197,7 @@ object Config {
197197
val hsqldb = "org.hsqldb:hsqldb:2.6.1"
198198
val javaFaker = "com.github.javafaker:javafaker:1.0.2"
199199
val msgpack = "org.msgpack:msgpack-core:0.9.8"
200+
val leakCanaryInstrumentation = "com.squareup.leakcanary:leakcanary-android-instrumentation:2.14"
200201
}
201202

202203
object QualityPlugins {

sentry-android-core/api/sentry-android-core.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public final class io/sentry/android/core/BuildInfoProvider {
158158
}
159159

160160
public final class io/sentry/android/core/ContextUtils {
161+
public static fun getApplicationContext (Landroid/content/Context;)Landroid/content/Context;
161162
public static fun isForegroundImportance ()Z
162163
}
163164

sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ static void loadDefaultAndMetadataOptions(
9090
final @NotNull BuildInfoProvider buildInfoProvider) {
9191
Objects.requireNonNull(context, "The context is required.");
9292

93-
// it returns null if ContextImpl, so let's check for nullability
94-
if (context.getApplicationContext() != null) {
95-
context = context.getApplicationContext();
96-
}
93+
context = ContextUtils.getApplicationContext(context);
9794

9895
Objects.requireNonNull(options, "The options object is required.");
9996
Objects.requireNonNull(logger, "The ILogger object is required.");

sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ public AndroidTransactionProfiler(
8787
final boolean isProfilingEnabled,
8888
final int profilingTracesHz,
8989
final @NotNull ISentryExecutorService executorService) {
90-
this.context = Objects.requireNonNull(context, "The application context is required");
90+
this.context =
91+
Objects.requireNonNull(
92+
ContextUtils.getApplicationContext(context), "The application context is required");
9193
this.logger = Objects.requireNonNull(logger, "ILogger is required");
9294
this.frameMetricsCollector =
9395
Objects.requireNonNull(frameMetricsCollector, "SentryFrameMetricsCollector is required");

sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public final class AnrIntegration implements Integration, Closeable {
3333
private final @NotNull Object startLock = new Object();
3434

3535
public AnrIntegration(final @NotNull Context context) {
36-
this.context = context;
36+
this.context = ContextUtils.getApplicationContext(context);
3737
}
3838

3939
/**

sentry-android-core/src/main/java/io/sentry/android/core/AnrV2EventProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public AnrV2EventProcessor(
9797
final @NotNull SentryAndroidOptions options,
9898
final @NotNull BuildInfoProvider buildInfoProvider,
9999
final @Nullable SecureRandom random) {
100-
this.context = context;
100+
this.context = ContextUtils.getApplicationContext(context);
101101
this.options = options;
102102
this.buildInfoProvider = buildInfoProvider;
103103
this.random = random;

sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public AnrV2Integration(final @NotNull Context context) {
6363

6464
AnrV2Integration(
6565
final @NotNull Context context, final @NotNull ICurrentDateProvider dateProvider) {
66-
this.context = context;
66+
this.context = ContextUtils.getApplicationContext(context);
6767
this.dateProvider = dateProvider;
6868
}
6969

sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public final class AppComponentsBreadcrumbsIntegration
2929
private @Nullable SentryAndroidOptions options;
3030

3131
public AppComponentsBreadcrumbsIntegration(final @NotNull Context context) {
32-
this.context = Objects.requireNonNull(context, "Context is required");
32+
this.context =
33+
Objects.requireNonNull(ContextUtils.getApplicationContext(context), "Context is required");
3334
}
3435

3536
@Override

sentry-android-core/src/main/java/io/sentry/android/core/ContextUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,19 @@ static void setAppPackageInfo(
383383
}
384384
app.setPermissions(permissions);
385385
}
386+
387+
/**
388+
* Get the app context
389+
*
390+
* @return the app context, or if not available, the provided context
391+
*/
392+
@NotNull
393+
public static Context getApplicationContext(final @NotNull Context context) {
394+
// it returns null if ContextImpl, so let's check for nullability
395+
final @Nullable Context appContext = context.getApplicationContext();
396+
if (appContext != null) {
397+
return appContext;
398+
}
399+
return context;
400+
}
386401
}

0 commit comments

Comments
 (0)