I am trying to learn Jetpack benchmark. By reviewing this google sample : https://github.com/android/performance-samples/tree/main/MacrobenchmarkSample
One of classes includes following method :
private fun MacrobenchmarkScope.clickOnId(resourceId: String) {
val selector = By.res(packageName, resourceId)
if (!device.wait(Until.hasObject(selector), 2_500)) {
fail("Did not find object with id $resourceId")
}
device
.findObject(selector)
.click()
// Chill to ensure we capture the end of the click span in the trace.
Thread.sleep(100)
}
And it has been called like : clickOnId("launchRecyclerActivity")
The string is a resource :
<string name="launchRecyclerActivity">RecyclerView</string>
And RecyclerView is an item in MainActivity :
BenchmarkActivityButton(name = "RecyclerView") {
launchActivityWithTrace<NonExportedRecyclerActivity>()
}
@Composable
fun BenchmarkActivityButton(name: String, onClick: () -> Unit) {
TextButton(
modifier = Modifier.padding(8.dp),
onClick = onClick,
border = BorderStroke(1.dp, MaterialTheme.colors.primary)
) {
Text(name)
}
}
The problem is this line returns null : val selector = By.res(packageName, resourceId)
And therefore I receive following exception :
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.test.uiautomator.UiObject2.click()' on a null object reference
at com.example.macrobenchmark.clickslatency.ClickLatencyBenchmark.clickOnId(ClickLatencyBenchmark.kt:189)
at com.example.macrobenchmark.clickslatency.ClickLatencyBenchmark.access$clickOnId(ClickLatencyBenchmark.kt:40)
at com.example.macrobenchmark.clickslatency.ClickLatencyBenchmark$simpleViewClick$2.invoke(ClickLatencyBenchmark.kt:64)
at com.example.macrobenchmark.clickslatency.ClickLatencyBenchmark$simpleViewClick$2.invoke(ClickLatencyBenchmark.kt:51)
at androidx.benchmark.macro.MacrobenchmarkKt$macrobenchmark$measurements$1$tracePath$1.invoke(Macrobenchmark.kt:193)
at androidx.benchmark.macro.MacrobenchmarkKt$macrobenchmark$measurements$1$tracePath$1.invoke(Macrobenchmark.kt:167)
at androidx.benchmark.perfetto.PerfettoCaptureWrapper.record(PerfettoCaptureWrapper.kt:90)
at androidx.benchmark.macro.MacrobenchmarkKt.macrobenchmark(Macrobenchmark.kt:167)
at androidx.benchmark.macro.MacrobenchmarkKt.macrobenchmarkWithStartupMode(Macrobenchmark.kt:301)
at androidx.benchmark.macro.junit4.MacrobenchmarkRule.measureRepeated(MacrobenchmarkRule.kt:106)
at com.example.macrobenchmark.clickslatency.ClickLatencyBenchmark.simpleViewClick(ClickLatencyBenchmark.kt:51)
How it can be resolved?