Skip to content

Commit f5b3ae5

Browse files
authored
Merge pull request #951 from SimonMarquis/issue-945
Extract `ProfileVerifier` logs from `MainActivity` to `NiaApplication`
2 parents 691bf2a + 0699efc commit f5b3ae5

File tree

3 files changed

+75
-41
lines changed

3 files changed

+75
-41
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.nowinandroid.util
18+
19+
import android.util.Log
20+
import androidx.profileinstaller.ProfileVerifier
21+
import com.google.samples.apps.nowinandroid.core.network.di.ApplicationScope
22+
import kotlinx.coroutines.CoroutineScope
23+
import kotlinx.coroutines.guava.await
24+
import kotlinx.coroutines.launch
25+
import javax.inject.Inject
26+
27+
/**
28+
* Logs the app's Baseline Profile Compilation Status using [ProfileVerifier].
29+
*
30+
* When delivering through Google Play, the baseline profile is compiled during installation.
31+
* In this case you will see the correct state logged without any further action necessary.
32+
* To verify baseline profile installation locally, you need to manually trigger baseline
33+
* profile installation.
34+
*
35+
* For immediate compilation, call:
36+
* ```bash
37+
* adb shell cmd package compile -f -m speed-profile com.example.macrobenchmark.target
38+
* ```
39+
* You can also trigger background optimizations:
40+
* ```bash
41+
* adb shell pm bg-dexopt-job
42+
* ```
43+
* Both jobs run asynchronously and might take some time complete.
44+
*
45+
* To see quick turnaround of the ProfileVerifier, we recommend using `speed-profile`.
46+
* If you don't do either of these steps, you might only see the profile status reported as
47+
* "enqueued for compilation" when running the sample locally.
48+
*
49+
* @see androidx.profileinstaller.ProfileVerifier.CompilationStatus.ResultCode
50+
*/
51+
class ProfileVerifierLogger @Inject constructor(
52+
@ApplicationScope private val scope: CoroutineScope,
53+
) {
54+
companion object {
55+
private const val TAG = "ProfileInstaller"
56+
}
57+
58+
operator fun invoke() = scope.launch {
59+
val status = ProfileVerifier.getCompilationStatusAsync().await()
60+
Log.d(TAG, "Status code: ${status.profileInstallResultCode}")
61+
Log.d(
62+
TAG,
63+
when {
64+
status.isCompiledWithProfile -> "App compiled with profile"
65+
status.hasProfileEnqueuedForCompilation() -> "Profile enqueued for compilation"
66+
else -> "Profile not compiled nor enqueued"
67+
},
68+
)
69+
}
70+
}

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

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

1919
import android.os.Bundle
20-
import android.util.Log
2120
import androidx.activity.ComponentActivity
2221
import androidx.activity.SystemBarStyle
2322
import androidx.activity.compose.setContent
@@ -37,7 +36,6 @@ import androidx.lifecycle.Lifecycle
3736
import androidx.lifecycle.lifecycleScope
3837
import androidx.lifecycle.repeatOnLifecycle
3938
import androidx.metrics.performance.JankStats
40-
import androidx.profileinstaller.ProfileVerifier
4139
import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading
4240
import com.google.samples.apps.nowinandroid.MainActivityUiState.Success
4341
import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper
@@ -49,12 +47,9 @@ import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
4947
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
5048
import com.google.samples.apps.nowinandroid.ui.NiaApp
5149
import dagger.hilt.android.AndroidEntryPoint
52-
import kotlinx.coroutines.Dispatchers
5350
import kotlinx.coroutines.flow.collect
5451
import kotlinx.coroutines.flow.onEach
55-
import kotlinx.coroutines.guava.await
5652
import kotlinx.coroutines.launch
57-
import kotlinx.coroutines.withContext
5853
import javax.inject.Inject
5954

6055
private const val TAG = "MainActivity"
@@ -150,48 +145,12 @@ class MainActivity : ComponentActivity() {
150145
override fun onResume() {
151146
super.onResume()
152147
lazyStats.get().isTrackingEnabled = true
153-
lifecycleScope.launch {
154-
logCompilationStatus()
155-
}
156148
}
157149

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

197156
/**

app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.app.Application
2020
import coil.ImageLoader
2121
import coil.ImageLoaderFactory
2222
import com.google.samples.apps.nowinandroid.sync.initializers.Sync
23+
import com.google.samples.apps.nowinandroid.util.ProfileVerifierLogger
2324
import dagger.hilt.android.HiltAndroidApp
2425
import javax.inject.Inject
2526
import javax.inject.Provider
@@ -32,10 +33,14 @@ class NiaApplication : Application(), ImageLoaderFactory {
3233
@Inject
3334
lateinit var imageLoader: Provider<ImageLoader>
3435

36+
@Inject
37+
lateinit var profileVerifierLogger: ProfileVerifierLogger
38+
3539
override fun onCreate() {
3640
super.onCreate()
3741
// Initialize Sync; the system responsible for keeping data in the app up to date.
3842
Sync.initialize(context = this)
43+
profileVerifierLogger()
3944
}
4045

4146
override fun newImageLoader(): ImageLoader = imageLoader.get()

0 commit comments

Comments
 (0)