1717package com.google.samples.apps.nowinandroid
1818
1919import android.os.Bundle
20+ import android.util.Log
2021import androidx.activity.ComponentActivity
2122import androidx.activity.compose.setContent
2223import androidx.activity.viewModels
@@ -35,6 +36,7 @@ import androidx.lifecycle.Lifecycle
3536import androidx.lifecycle.lifecycleScope
3637import androidx.lifecycle.repeatOnLifecycle
3738import androidx.metrics.performance.JankStats
39+ import androidx.profileinstaller.ProfileVerifier
3840import com.google.accompanist.systemuicontroller.rememberSystemUiController
3941import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading
4042import com.google.samples.apps.nowinandroid.MainActivityUiState.Success
@@ -47,11 +49,15 @@ import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
4749import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
4850import com.google.samples.apps.nowinandroid.ui.NiaApp
4951import dagger.hilt.android.AndroidEntryPoint
52+ import kotlinx.coroutines.Dispatchers
5053import kotlinx.coroutines.flow.collect
5154import kotlinx.coroutines.flow.onEach
5255import kotlinx.coroutines.launch
56+ import kotlinx.coroutines.withContext
5357import javax.inject.Inject
5458
59+ private const val TAG = " MainActivity"
60+
5561@OptIn(ExperimentalMaterial3WindowSizeClassApi ::class )
5662@AndroidEntryPoint
5763class 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/* *
0 commit comments