0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Baseline Profilesで起動速度を改善する

Posted at

Androidアプリの起動速度が遅くなっている場合、Googleが提供する Baseline Profiles を活用することで改善できます。Baseline Profilesの導入手順をメモします。

Baseline Profilesは ART(Android Runtime)の最適化機能 です。 アプリのよく使うクラスやメソッドを事前にプロファイルとして生成しすることで、初回起動時でコンパイルに頼らず高速起動できます。
・完全にアプリを終了した状態からの起動
・バックグラウンドからの復帰
どちらの起動でもパフォーマンスが向上します。

導入手順

build.gradlesettings.gradle に以下を追加します。

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("androidx.baselineprofile")
}
plugins {
    id("androidx.baselineprofile") version "x.x.x" apply false
}

プロファイル生成のサンプルコード

・アプリの起動直後に必要な画面遷移を必ず含める
・RecyclerViewなど重い画面はスクロール操作も記録すると効果的

@ExperimentalBaselineProfilesApi
@RunWith(AndroidJUnit4::class)
class BaselineProfileGenerator {

    @get:Rule
    val baselineProfileRule = BaselineProfileRule()

    @Test
    fun generate() {
        baselineProfileRule.collect(
            packageName = "com.example.myapp"
        ) {
            pressHome()
            startActivityAndWait() // 起動

            // 重要な画面遷移は記録する
            device.findObject(By.text("ログイン")).click()
            device.findObject(By.text("ホーム")).click()
        }
    }
}

Baseline Profileの生成

以下のコマンドでプロファイルを生成し、app/src/main/baselineProfiles に配置します。

./gradlew :baselineprofile:connectedAndroidTest

配布と効果

Play Store配布時にAABへ含めることで初回起動から最適化済みコードを利用可能になり、起動速度やUIレスポンスが向上します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?