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,16 @@ 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
55+ import kotlinx.coroutines.guava.await
5256import kotlinx.coroutines.launch
57+ import kotlinx.coroutines.withContext
5358import javax.inject.Inject
5459
60+ private const val TAG = " MainActivity"
61+
5562@OptIn(ExperimentalMaterial3WindowSizeClassApi ::class )
5663@AndroidEntryPoint
5764class MainActivity : ComponentActivity () {
@@ -133,12 +140,48 @@ class MainActivity : ComponentActivity() {
133140 override fun onResume () {
134141 super .onResume()
135142 lazyStats.get().isTrackingEnabled = true
143+ lifecycleScope.launch {
144+ logCompilationStatus()
145+ }
136146 }
137147
138148 override fun onPause () {
139149 super .onPause()
140150 lazyStats.get().isTrackingEnabled = false
141151 }
152+
153+ /* *
154+ * Logs the app's Baseline Profile Compilation Status using [ProfileVerifier].
155+ */
156+ private suspend fun logCompilationStatus () {
157+ /*
158+ When delivering through Google Play, the baseline profile is compiled during installation.
159+ In this case you will see the correct state logged without any further action necessary.
160+ To verify baseline profile installation locally, you need to manually trigger baseline
161+ profile installation.
162+ For immediate compilation, call:
163+ `adb shell cmd package compile -f -m speed-profile com.example.macrobenchmark.target`
164+ You can also trigger background optimizations:
165+ `adb shell pm bg-dexopt-job`
166+ Both jobs run asynchronously and might take some time complete.
167+ To see quick turnaround of the ProfileVerifier, we recommend using `speed-profile`.
168+ If you don't do either of these steps, you might only see the profile status reported as
169+ "enqueued for compilation" when running the sample locally.
170+ */
171+ withContext(Dispatchers .IO ) {
172+ val status = ProfileVerifier .getCompilationStatusAsync().await()
173+ Log .d(TAG , " ProfileInstaller status code: ${status.profileInstallResultCode} " )
174+ Log .d(
175+ TAG ,
176+ when {
177+ status.isCompiledWithProfile -> " ProfileInstaller: is compiled with profile"
178+ status.hasProfileEnqueuedForCompilation() ->
179+ " ProfileInstaller: Enqueued for compilation"
180+ else -> " Profile not compiled or enqueued"
181+ },
182+ )
183+ }
184+ }
142185}
143186
144187/* *
0 commit comments