Skip to content

Commit 7b5c0f2

Browse files
author
Murat Yener
committed
Adds ProfileViewer to log the app's Baseline Profile Compilation Status
1 parent 89c4653 commit 7b5c0f2

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

app/src/main/java/com/google/samples/apps/nowinandroid/MainActivity.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.samples.apps.nowinandroid
1818

1919
import android.os.Bundle
20+
import android.util.Log
2021
import androidx.activity.ComponentActivity
2122
import androidx.activity.compose.setContent
2223
import androidx.activity.viewModels
@@ -35,6 +36,7 @@ import androidx.lifecycle.Lifecycle
3536
import androidx.lifecycle.lifecycleScope
3637
import androidx.lifecycle.repeatOnLifecycle
3738
import androidx.metrics.performance.JankStats
39+
import androidx.profileinstaller.ProfileVerifier
3840
import com.google.accompanist.systemuicontroller.rememberSystemUiController
3941
import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading
4042
import com.google.samples.apps.nowinandroid.MainActivityUiState.Success
@@ -47,11 +49,15 @@ import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
4749
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
4850
import com.google.samples.apps.nowinandroid.ui.NiaApp
4951
import dagger.hilt.android.AndroidEntryPoint
52+
import kotlinx.coroutines.Dispatchers
5053
import kotlinx.coroutines.flow.collect
5154
import kotlinx.coroutines.flow.onEach
5255
import kotlinx.coroutines.launch
56+
import kotlinx.coroutines.withContext
5357
import javax.inject.Inject
5458

59+
private const val TAG = "MainActivity"
60+
5561
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
5662
@AndroidEntryPoint
5763
class MainActivity : ComponentActivity() {
@@ -133,12 +139,48 @@ class MainActivity : ComponentActivity() {
133139
override fun onResume() {
134140
super.onResume()
135141
lazyStats.get().isTrackingEnabled = true
142+
lifecycleScope.launch {
143+
logCompilationStatus()
144+
}
136145
}
137146

138147
override fun onPause() {
139148
super.onPause()
140149
lazyStats.get().isTrackingEnabled = false
141150
}
151+
152+
/**
153+
* Logs the app's Baseline Profile Compilation Status using [ProfileVerifier].
154+
*/
155+
private suspend fun logCompilationStatus() {
156+
/*
157+
When delivering through Google Play, the baseline profile is compiled during installation.
158+
In this case you will see the correct state logged without any further action necessary.
159+
To verify baseline profile installation locally, you need to manually trigger baseline
160+
profile installation.
161+
For immediate compilation, call:
162+
`adb shell cmd package compile -f -m speed-profile com.example.macrobenchmark.target`
163+
You can also trigger background optimizations:
164+
`adb shell pm bg-dexopt-job`
165+
Both jobs run asynchronously and might take some time complete.
166+
To see quick turnaround of the ProfileVerifier, we recommend using `speed-profile`.
167+
If you don't do either of these steps, you might only see the profile status reported as
168+
"enqueued for compilation" when running the sample locally.
169+
*/
170+
withContext(Dispatchers.IO) {
171+
val status = ProfileVerifier.getCompilationStatusAsync().get()
172+
Log.d(TAG, "ProfileInstaller status code: ${status.profileInstallResultCode}")
173+
Log.d(
174+
TAG,
175+
when {
176+
status.isCompiledWithProfile -> "ProfileInstaller: is compiled with profile"
177+
status.hasProfileEnqueuedForCompilation() ->
178+
"ProfileInstaller: Enqueued for compilation"
179+
else -> "Profile not compiled or enqueued"
180+
}
181+
)
182+
}
183+
}
142184
}
143185

144186
/**

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ androidxLifecycle = "2.6.0-alpha05"
1818
androidxMacroBenchmark = "1.2.0-alpha16"
1919
androidxMetrics = "1.0.0-alpha03"
2020
androidxNavigation = "2.5.3"
21-
androidxProfileinstaller = "1.2.1"
21+
androidxProfileinstaller = "1.3.1"
2222
androidxStartup = "1.1.1"
2323
androidxTestCore = "1.5.0"
2424
androidxTestExt = "1.1.4"

0 commit comments

Comments
 (0)