Skip to content

Fix FrameMetrics NPE #4284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion firebase-perf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased
*
* [fixed] Fixed a `NullPointerException` crash when instrumenting screen traces
on Android 7, 8, and 9.
(#4146)


# 20.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.firebase.perf.application;

import android.app.Activity;
import android.os.Build;
import android.util.SparseIntArray;
import androidx.core.app.FrameMetricsAggregator;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -107,11 +108,22 @@ public Optional<PerfFrameMetrics> stop() {
}
Optional<PerfFrameMetrics> data = this.snapshot();
try {
// No reliable way to check for hardware-acceleration, so we must catch retroactively (#2736).
frameMetricsAggregator.remove(activity);
} catch (IllegalArgumentException err) {
} catch (IllegalArgumentException | NullPointerException ex) {
// Both of these exceptions result from android.view.View.addFrameMetricsListener silently
// failing when the view is not hardware-accelerated. Successful addFrameMetricsListener
// stores an observer in a list, and initializes the list if it was uninitialized. Invoking
// View.removeFrameMetricsListener(listener) throws IAE if it doesn't exist in the list, or
// throws NPE if the list itself was never initialized (#4184).
if (ex instanceof NullPointerException && Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
// Re-throw above API 28, since the NPE is fixed in API 29:
// https://android.googlesource.com/platform/frameworks/base/+/140ff5ea8e2d99edc3fbe63a43239e459334c76b
throw ex;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the intent to notify the developers of the exception during development? Interesting to see a pattern where we throw and exception in a catch module.

}
logger.warn(
"View not hardware accelerated. Unable to collect FrameMetrics. %s", err.toString());
return Optional.absent();
"View not hardware accelerated. Unable to collect FrameMetrics. %s", ex.toString());
data = Optional.absent();
}
frameMetricsAggregator.reset();
isRecording = false;
Expand Down