Skip to content

Commit 1bdd114

Browse files
markushiclaude
andcommitted
ref(feedback): Guard the androidx.activity calls against LinkageErrors
androidx.activity is a compileOnly dependency, so the version in the app's apk is outside our control and may be missing the photo picker APIs this was compiled against. Catches LinkageError locally at the two call sites that link against it, then falls back to the sanctioned ExceptionUtils.rethrowIfFatal pattern, as ExceptionUtils documents and SentrySQLiteDriver.hasConnectionPool demonstrates. Registration is now guarded too, so a version mismatch hides the screenshot button instead of breaking the whole form. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 5edad4a commit 1bdd114

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.app.Activity;
44
import android.app.AlertDialog;
55
import android.app.Application;
6-
import android.content.ActivityNotFoundException;
76
import android.content.ContentResolver;
87
import android.content.Context;
98
import android.content.ContextWrapper;
@@ -31,6 +30,7 @@
3130
import io.sentry.protocol.Feedback;
3231
import io.sentry.protocol.SentryId;
3332
import io.sentry.protocol.User;
33+
import io.sentry.util.ExceptionUtils;
3434
import io.sentry.util.FileUtils;
3535
import io.sentry.util.LoadClass;
3636
import java.io.IOException;
@@ -229,12 +229,20 @@ protected void onCreate(Bundle savedInstanceState) {
229229
if (screenshotPicker != null) {
230230
try {
231231
screenshotPicker.launch();
232-
} catch (ActivityNotFoundException | IllegalStateException e) {
233-
// No photo picker on the device, or the launcher is no longer registered
232+
} catch (LinkageError e) {
233+
// androidx.activity is compileOnly, so the version in the app's apk may not have
234+
// the photo picker APIs this was compiled against
234235
Sentry.getCurrentScopes()
235236
.getOptions()
236237
.getLogger()
237238
.log(SentryLevel.ERROR, "Failed to launch the screenshot picker.", e);
239+
} catch (Throwable t) {
240+
ExceptionUtils.rethrowIfFatal(t);
241+
// e.g. no photo picker on the device, or the launcher is no longer registered
242+
Sentry.getCurrentScopes()
243+
.getOptions()
244+
.getLogger()
245+
.log(SentryLevel.ERROR, "Failed to launch the screenshot picker.", t);
238246
}
239247
}
240248
} else {
@@ -452,9 +460,21 @@ private void maybeRegisterScreenshotPicker(final @NotNull SentryOptions options)
452460
}
453461
final @Nullable Activity activity = getActivity(getContext());
454462
if (activity != null && isScreenshotPickerAvailable(options)) {
455-
screenshotPicker =
456-
SentryFeedbackScreenshotPicker.register(
457-
activity, uri -> onScreenshotPicked(options, btnAddScreenshot, uri));
463+
try {
464+
screenshotPicker =
465+
SentryFeedbackScreenshotPicker.register(
466+
activity, uri -> onScreenshotPicked(options, btnAddScreenshot, uri));
467+
} catch (LinkageError e) {
468+
// This is where androidx.activity is linked for the first time. It is a compileOnly
469+
// dependency, so the version in the app's apk may be older than the one we compiled
470+
// against, or missing the photo picker APIs entirely.
471+
options
472+
.getLogger()
473+
.log(SentryLevel.INFO, "androidx.activity is too old for the screenshot picker.", e);
474+
} catch (Throwable t) {
475+
ExceptionUtils.rethrowIfFatal(t);
476+
options.getLogger().log(SentryLevel.ERROR, "Failed to register the screenshot picker.", t);
477+
}
458478
}
459479
if (screenshotPicker != null) {
460480
btnAddScreenshot.setVisibility(View.VISIBLE);
@@ -526,14 +546,14 @@ private void maybeAddImageAttachment(final @NotNull Hint hint) {
526546
mime,
527547
"event.attachment",
528548
false));
529-
} catch (RuntimeException e) {
530-
// The ContentResolver call crosses into the provider's process, so any failure there arrives
531-
// as one of the exceptions Binder can marshal, e.g. a SecurityException once the read
532-
// permission for the picked Uri was revoked
549+
} catch (Throwable t) {
550+
ExceptionUtils.rethrowIfFatal(t);
551+
// The ContentResolver call crosses into the provider's process, which can fail in any number
552+
// of ways, e.g. a SecurityException once the read permission for the picked Uri was revoked
533553
Sentry.getCurrentScopes()
534554
.getOptions()
535555
.getLogger()
536-
.log(SentryLevel.ERROR, "Failed to attach image to feedback.", e);
556+
.log(SentryLevel.ERROR, "Failed to attach image to feedback.", t);
537557
}
538558
}
539559

@@ -548,14 +568,15 @@ private static long getUriSize(
548568
return cursor.getLong(sizeIndex);
549569
}
550570
}
551-
} catch (RuntimeException e) {
571+
} catch (Throwable t) {
572+
ExceptionUtils.rethrowIfFatal(t);
552573
options
553574
.getLogger()
554575
.log(
555576
SentryLevel.WARNING,
556577
"Unable to determine the size of the selected screenshot, the attachment size limit "
557578
+ "is applied when the feedback is captured.",
558-
e);
579+
t);
559580
}
560581
return -1;
561582
}

0 commit comments

Comments
 (0)