Skip to content

Commit a2d96b1

Browse files
committed
copy PR#4184
1 parent 62cfd00 commit a2d96b1

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

firebase-perf/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
2-
*
2+
* [fixed] Fixed a `NullPointerException` crash when instrumenting screen traces
3+
on Android 7, 8, and 9.
4+
(#4146)
35

46

57
# 20.2.0

firebase-perf/src/main/java/com/google/firebase/perf/application/FrameMetricsRecorder.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.perf.application;
1616

1717
import android.app.Activity;
18+
import android.os.Build;
1819
import android.util.SparseIntArray;
1920
import androidx.core.app.FrameMetricsAggregator;
2021
import androidx.fragment.app.Fragment;
@@ -107,11 +108,22 @@ public Optional<PerfFrameMetrics> stop() {
107108
}
108109
Optional<PerfFrameMetrics> data = this.snapshot();
109110
try {
111+
// No reliable way to check for hardware-acceleration, so we must catch retroactively (#2736).
110112
frameMetricsAggregator.remove(activity);
111-
} catch (IllegalArgumentException err) {
113+
} catch (IllegalArgumentException | NullPointerException ex) {
114+
// Both of these exceptions result from android.view.View.addFrameMetricsListener silently
115+
// failing when the view is not hardware-accelerated. Successful addFrameMetricsListener
116+
// stores an observer in a list, and initializes the list if it was uninitialized. Invoking
117+
// View.removeFrameMetricsListener(listener) throws IAE if it doesn't exist in the list, or
118+
// throws NPE if the list itself was never initialized (#4184).
119+
if (ex instanceof NullPointerException && Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
120+
// Re-throw above API 28, since the NPE is fixed in API 29:
121+
// https://android.googlesource.com/platform/frameworks/base/+/140ff5ea8e2d99edc3fbe63a43239e459334c76b
122+
throw ex;
123+
}
112124
logger.warn(
113-
"View not hardware accelerated. Unable to collect FrameMetrics. %s", err.toString());
114-
return Optional.absent();
125+
"View not hardware accelerated. Unable to collect FrameMetrics. %s", ex.toString());
126+
data = Optional.absent();
115127
}
116128
frameMetricsAggregator.reset();
117129
isRecording = false;

0 commit comments

Comments
 (0)