A fully functional Android app built entirely with Kotlin and Jetpack Compose
Extract `ProfileVerifier` logs from `MainActivity` to `NiaApplication`
Simon Marquis03ba943632bf
diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/MainActivity.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/MainActivity.ktindex 7fe1bc674..42468ca6f 100644--- a/app/src/main/java/com/google/samples/apps/nowinandroid/MainActivity.kt+++ b/app/src/main/java/com/google/samples/apps/nowinandroid/MainActivity.kt@@ -17,7 +17,6 @@package com.google.samples.apps.nowinandroidimport android.os.Bundle-import android.util.Logimport androidx.activity.ComponentActivityimport androidx.activity.SystemBarStyleimport androidx.activity.compose.setContent@@ -37,7 +36,6 @@ import androidx.lifecycle.Lifecycleimport androidx.lifecycle.lifecycleScopeimport androidx.lifecycle.repeatOnLifecycleimport androidx.metrics.performance.JankStats-import androidx.profileinstaller.ProfileVerifierimport com.google.samples.apps.nowinandroid.MainActivityUiState.Loadingimport com.google.samples.apps.nowinandroid.MainActivityUiState.Successimport com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper@@ -49,12 +47,9 @@ import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfigimport com.google.samples.apps.nowinandroid.core.model.data.ThemeBrandimport com.google.samples.apps.nowinandroid.ui.NiaAppimport dagger.hilt.android.AndroidEntryPoint-import kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.flow.collectimport kotlinx.coroutines.flow.onEach-import kotlinx.coroutines.guava.awaitimport kotlinx.coroutines.launch-import kotlinx.coroutines.withContextimport javax.inject.Injectprivate const val TAG = "MainActivity"@@ -152,48 +147,12 @@ class MainActivity : ComponentActivity() {override fun onResume() {super.onResume()lazyStats.get().isTrackingEnabled = true- lifecycleScope.launch {- logCompilationStatus()- }}override fun onPause() {super.onPause()lazyStats.get().isTrackingEnabled = false}-- /**- * Logs the app's Baseline Profile Compilation Status using [ProfileVerifier].- */- private suspend fun logCompilationStatus() {- /*- When delivering through Google Play, the baseline profile is compiled during installation.- In this case you will see the correct state logged without any further action necessary.- To verify baseline profile installation locally, you need to manually trigger baseline- profile installation.- For immediate compilation, call:- `adb shell cmd package compile -f -m speed-profile com.example.macrobenchmark.target`- You can also trigger background optimizations:- `adb shell pm bg-dexopt-job`- Both jobs run asynchronously and might take some time complete.- To see quick turnaround of the ProfileVerifier, we recommend using `speed-profile`.- If you don't do either of these steps, you might only see the profile status reported as- "enqueued for compilation" when running the sample locally.- */- withContext(Dispatchers.IO) {- val status = ProfileVerifier.getCompilationStatusAsync().await()- Log.d(TAG, "ProfileInstaller status code: ${status.profileInstallResultCode}")- Log.d(- TAG,- when {- status.isCompiledWithProfile -> "ProfileInstaller: is compiled with profile"- status.hasProfileEnqueuedForCompilation() ->- "ProfileInstaller: Enqueued for compilation"- else -> "Profile not compiled or enqueued"- },- )- }- }}/**diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/NiaApplication.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/NiaApplication.ktindex 699f52575..9f0bb2ef7 100644--- a/app/src/main/java/com/google/samples/apps/nowinandroid/NiaApplication.kt+++ b/app/src/main/java/com/google/samples/apps/nowinandroid/NiaApplication.kt@@ -20,6 +20,7 @@ import android.app.Applicationimport coil.ImageLoaderimport coil.ImageLoaderFactoryimport com.google.samples.apps.nowinandroid.sync.initializers.Sync+import com.google.samples.apps.nowinandroid.util.ProfileVerifierLoggerimport dagger.hilt.android.HiltAndroidAppimport javax.inject.Injectimport javax.inject.Provider@@ -32,10 +33,14 @@ class NiaApplication : Application(), ImageLoaderFactory {@Injectlateinit var imageLoader: Provider<ImageLoader>+ @Inject+ lateinit var profileVerifierLogger: ProfileVerifierLogger+override fun onCreate() {super.onCreate()// Initialize Sync; the system responsible for keeping data in the app up to date.Sync.initialize(context = this)+ profileVerifierLogger()}override fun newImageLoader(): ImageLoader = imageLoader.get()diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/util/ProfileVerifierLogger.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/util/ProfileVerifierLogger.ktnew file mode 100644index 000000000..595166f03--- /dev/null+++ b/app/src/main/java/com/google/samples/apps/nowinandroid/util/ProfileVerifierLogger.kt@@ -0,0 +1,70 @@+/*+ * Copyright 2023 The Android Open Source Project+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * https://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package com.google.samples.apps.nowinandroid.util++import android.util.Log+import androidx.profileinstaller.ProfileVerifier+import com.google.samples.apps.nowinandroid.core.network.di.ApplicationScope+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.guava.await+import kotlinx.coroutines.launch+import javax.inject.Inject++/**+ * Logs the app's Baseline Profile Compilation Status using [ProfileVerifier].+ *+ * When delivering through Google Play, the baseline profile is compiled during installation.+ * In this case you will see the correct state logged without any further action necessary.+ * To verify baseline profile installation locally, you need to manually trigger baseline+ * profile installation.+ *+ * For immediate compilation, call:+ * ```bash+ * adb shell cmd package compile -f -m speed-profile com.example.macrobenchmark.target+ * ```+ * You can also trigger background optimizations:+ * ```bash+ * adb shell pm bg-dexopt-job+ * ```+ * Both jobs run asynchronously and might take some time complete.+ *+ * To see quick turnaround of the ProfileVerifier, we recommend using `speed-profile`.+ * If you don't do either of these steps, you might only see the profile status reported as+ * "enqueued for compilation" when running the sample locally.+ *+ * @see androidx.profileinstaller.ProfileVerifier.CompilationStatus.ResultCode+ */+class ProfileVerifierLogger @Inject constructor(+ @ApplicationScope private val scope: CoroutineScope,+) {+ companion object {+ private const val TAG = "ProfileInstaller"+ }++ operator fun invoke() = scope.launch {+ val status = ProfileVerifier.getCompilationStatusAsync().await()+ Log.d(TAG, "Status code: ${status.profileInstallResultCode}")+ Log.d(+ TAG,+ when {+ status.isCompiledWithProfile -> "App compiled with profile"+ status.hasProfileEnqueuedForCompilation() -> "Profile enqueued for compilation"+ else -> "Profile not compiled nor enqueued"+ },+ )+ }+}