From a8395f43de518c07c0a81747d2089612bb2f03d2 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ricau Date: Tue, 2 Jan 2024 14:27:01 +0100 Subject: [PATCH] Improve error message when android.os.Build class missing Related to #2580 --- .../src/main/java/shark/AndroidBuildMirror.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/shark/shark-android/src/main/java/shark/AndroidBuildMirror.kt b/shark/shark-android/src/main/java/shark/AndroidBuildMirror.kt index 70fd38c0f0..99a56f5fb6 100644 --- a/shark/shark-android/src/main/java/shark/AndroidBuildMirror.kt +++ b/shark/shark-android/src/main/java/shark/AndroidBuildMirror.kt @@ -25,7 +25,12 @@ class AndroidBuildMirror( */ fun fromHeapGraph(graph: HeapGraph): AndroidBuildMirror { return graph.context.getOrPut(AndroidBuildMirror::class.java.name) { - val buildClass = graph.findClassByName("android.os.Build")!! + // checkNotNull for more helpful message than NPE if this isn't an Android hprof. + // Not doing checkNotNull for other classes, if android.os.Build is there it's definitely + // an Android heap dump. + val buildClass = checkNotNull(graph.findClassByName("android.os.Build")) { + "android.os.Build class missing from heap dump, is this an Android heap dump?" + } val versionClass = graph.findClassByName("android.os.Build\$VERSION")!! val manufacturer = buildClass["MANUFACTURER"]!!.value.readAsJavaString()!! val sdkInt = versionClass["SDK_INT"]!!.value.asInt!!