Skip to content

Commit e4668f3

Browse files
authored
Merge pull request #821 from android/profile-verifier
Adds ProfileViewer to log the app's Baseline Profile Compilation Status
2 parents 89c4653 + e96dac3 commit e4668f3

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ dependencies {
119119
implementation(libs.androidx.navigation.compose)
120120
implementation(libs.androidx.window.manager)
121121
implementation(libs.androidx.profileinstaller)
122+
implementation(libs.kotlinx.coroutines.guava)
122123
implementation(libs.coil.kt)
123124
}
124125

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

Lines changed: 43 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,16 @@ 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
55+
import kotlinx.coroutines.guava.await
5256
import kotlinx.coroutines.launch
57+
import kotlinx.coroutines.withContext
5358
import javax.inject.Inject
5459

60+
private const val TAG = "MainActivity"
61+
5562
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
5663
@AndroidEntryPoint
5764
class 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
/**

gradle/libs.versions.toml

Lines changed: 2 additions & 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"
@@ -117,6 +117,7 @@ hilt-ext-work = { group = "androidx.hilt", name = "hilt-work", version.ref = "hi
117117
junit4 = { group = "junit", name = "junit", version.ref = "junit4" }
118118
kotlin-stdlib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-jdk8", version.ref = "kotlin" }
119119
kotlinx-coroutines-android = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "kotlinxCoroutines" }
120+
kotlinx-coroutines-guava = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-guava", version.ref = "kotlinxCoroutines" }
120121
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "kotlinxCoroutines" }
121122
kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinxDatetime" }
122123
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }

0 commit comments

Comments
 (0)