前人未踏の領域へ Androidアプリ開発編

Androidアプリ開発に関する調査メモ置き場。古い記事にはアプリ以外も含まれます。

App Bundle対応

App Bundleが登場したので対応してみる。

App Bundle で何が変わるのか

アプリで対応する端末ごとに最適なAPKをPlay Store側で生成、 個別にインストールしてくれる。

何が嬉しいのか

  • これまではAPKに対応する端末ごとの全てのモジュールを1つにまとめて、あるいはMultiple APKで分割していたが、今後はその必要がなくなる。
  • ユーザーごとのインストール時のAPKのサイズが小さくなるため、ダウンロード、インストールが早くなる。

AAPT2を有効にする

既存アプリでは必要な設定らしい。

gradle.propertiesを編集

android.enableAapt2=true

Bundleファイルをビルドしてみる

Android Studioからビルド

メニューから BuildBuild Bundle(s) / APK(s)Build Bundle(s) を選択

ビルドが終わるとAndroid StudioのEvent logに以下のような表示が出る

18:49    Build Bundle(s)
            App bundle(s) generated successfully:
            Module 'app': locate or analyze the app bundle.

locate をクリックするとを生成されたファイルが確認できる。aabという拡張子になっていることがわかる。

analyze を選択するとaabファイルの中身が表示され、これまでのAPKとのサイズ比較ができる。ファイルサイズはさほど変わっていないが、baseディレクトリに移動しているのがわかる。

bundletool

bundletoolを使うことでaab, apkファイルを生成し、そこから端末にインストールできる。adb コマンドの代わり。

GitHubからjarファイルをダウンロードする。現在の最新は 0.6.0

github.com

コマンド

使えるコマンドは以下の通り

$ java -jar bundletool-all-0.6.0.jar
Synopsis: bundletool <command> ...

Use 'bundletool help <command>' to learn more about the given command.

build-bundle command: (Bundleを作る)
    Builds an Android App Bundle from a set of Bundle modules provided as zip
    files.

build-apks command: (APKを作る)
    Generates an APK Set archive containing either all possible split APKs and
    standalone APKs or APKs optimized for the connected device (see connected-
    device flag).

extract-apks command: (APKセットからAPKを取り出す)
    Extracts from an APK Set the APKs that should be installed on a given
    device.

get-device-spec command: (デバイススペックファイルを抽出する)
    Writes out a JSON file containing the device specifications (i.e. features
    and properties) of the connected Android device.

install-apks command: (apkをインストールする)
    Installs APKs extracted from an APK Set to a connected device. Replaces
    already installed package.

validate command: (apksを検証する)
    Verifies the given Android App Bundle is valid and prints out information
    about it.

version command:
    Prints the version of BundleTool.

build-apks コマンドでaabファイルを作るにはzip化されたモジュールが必要らしいが、その作り方が分からないので aabファイルはAndroid Studioのものを使うことにする。

APKを作る

$ java -jar bundletool-all-0.6.0.jar build-apks
 --bundle=app/build/outputs/bundle/staging/app.aab 
 --output=app/build/outputs/apk/bundle/bundled_app.apks
 --ks=~/.android/debug.keystore 
 --ks-pass=pass:android 
 --ks-key-alias=androiddebugkey 
 --key-pass=pass:android

こうすると1.4Gのファイルが出来上がった。

--connected-device オプションをつけることで接続中の端末を対象にできる

$ java -jar bundletool-all-0.6.0.jar build-apks 
--bundle=app/build/outputs/bundle/staging/app.aab 
--output=app/build/outputs/apk/bundle/bundled_app.apks 
--ks=~/.android/debug.keystore --ks-pass=pass:android 
--ks-key-alias=androiddebugkey 
--key-pass=pass:android 
--connected-device

こちらの場合は39MB。もともとが49MBだったので20%ほどサイズが小さくなっていた。

端末にインストール

install-apks コマンドを使うことで接続端末にインストールができる。 当然ながら署名済みでないとエラーになるので注意。

$ java -jar bundletool-all-0.6.0.jar install-apks 
--apks=app/build/outputs/apk/bundle/bundle_app.apks

Gradleでビルド

gradle wrapperでapk作ってた場合にどうるすのるのかなと思ったらちゃんとあった。

$ ./gradlew tasks

-- 中略 -- 
bundleDebug - Creates all Debug bundles.
bundleRelease - Creates all Release bundles.
-- 中略 -- 
./gradlew bundleRelease

これでOK

app/build/outputs/bundle/release/app.aab

みたいにファイルが作られている。

参考

bundletool  |  Android Developers

Android App Bundle でアプリや機能をオンデマンド配信する - Play Console ヘルプ