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?

AdMob SDKの初期化処理を最適化してアプリ起動のパフォーマンスを向上させる

Posted at

Android の AdMob の SDK のドキュメントが更新されて、導入のドキュメントに初期化処理の最適化の記載が増えていたのでその紹介です。
もし過去に AdMob を導入していて以下に類する処理を実装をしていない場合は、アプリ起動時間の短縮や ANR の改善を見込める可能性があります。

追加された記載

導入の実装例に追加されたのは MobileAds#initialize の処理を別スレッドで行うことで起動時処理のブロッキングを防ぐというものです。

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val backgroundScope = CoroutineScope(Dispatchers.IO)
    backgroundScope.launch {
      MobileAds.initialize(this@MainActivity) {}
    }
  }
}

他の最適化オプション

それ以外にもベータ版の機能として広告の最適化オプションが提供されています。

初期化の最適化

AndroidManifest に以下の設定をすることで AdMob の初期化処理を別スレッドで行うようになります。
上記の MobileAds#initialize を別スレッドで処理している場合はこの設定は不要です。

<manifest>
  ...
  <application>
      ...
      <meta-data
          android:name="com.google.android.gms.ads.flag.OPTIMIZE_INITIALIZATION"
          android:value="true"/>
  </application>
</manifest>

広告読み込みの最適化

AndroidManifest に以下の設定をすることで広告読み込みの呼び出し処理を別スレッドで行うようになります。

<manifest>
  ...
  <application>
      ...
      <meta-data
          android:name="com.google.android.gms.ads.flag.OPTIMIZE_AD_LOADING"
          android:value="true"/>
  </application>
</manifest>

アプリ起動時の計測

あまりスペックが高くない端末である Xperia 10(Android 10) で Baseline Profiles 適用状態で Cold 状態からのアプリ起動の時間を Macrobenchmark で計測しました。

パターン 結果
何もなし スクリーンショット 2025-02-13 13.17.31.png
別スレッド実装 スクリーンショット 2025-02-13 13.24.59.png
OPTIMIZE_INITIALIZATION の設定 スクリーンショット 2025-02-13 13.27.27.png

内部の処理が不明なので理由が定かではないのですが、MobileAds#initialize を別スレッドにする方が一番効果あるという結果が出ました。

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?