ウィジェットを使ってみる

前提条件とセットアップ

開始する前に、環境が次の要件を満たしていることを確認してください。

ランタイム要件

Wear ウィジェットを使用するには、対象デバイスの com.google.android.wearable.protolayout.renderer APK のバージョンが 1.6.1 以降である必要があります。

次のいずれかの方法で、互換性のあるレンダラ バージョンを取得します。

  • Wear OS 7 エミュレータ: Wear OS 7 エミュレータ イメージを使用します。7 より前のバージョンは適していません。セットアップ手順については、Wear OS 7 エミュレータをセットアップするをご覧ください。
  • 物理デバイス: Google Play ストアから自動アップデートを受信する物理 Wear OS デバイス、または Google Play ストアにログインしているデベロッパー デバイスを使用します。

デバイスにインストールされているバージョンを確認するには、次のコマンドを使用します。

adb shell dumpsys package com.google.android.wearable.protolayout.renderer | \
  grep -m 1 versionName | \
  awk -F= '{print $2}'

Gradle 構成

Wear Widget ライブラリは Google Maven で入手できます。

1. SDK バージョンを構成する

compileSdktargetSdk37 以上に設定されていることを確認します。

android {
    compileSdk = 37
    // ...
    defaultConfig {
        targetSdk = 37
        // ...
    }
}

2. 依存関係を追加する

アプリの build.gradle.kts ファイルに次の依存関係を含めます。

Groovy

dependencies {
    // Core Wear Widget and Remote Compose libraries
    implementation "androidx.compose.remote:remote-creation-compose:1.0.0-alpha010"
    implementation "androidx.compose.remote:remote-core:1.0.0-alpha010"
    implementation "androidx.glance.wear:wear:1.0.0-alpha09"
    implementation "androidx.glance.wear:wear-core:1.0.0-alpha09"
    implementation "androidx.wear.compose.remote:remote-material3:1.0.0-alpha03"

    // Tooling for previews (optional, but recommended)
    implementation "androidx.compose.remote:remote-tooling-preview:1.0.0-alpha010"
    implementation "androidx.wear.compose:compose-ui-tooling:1.6.1"
    implementation "androidx.wear.tiles:tiles-tooling-preview:1.6.0"
    debugImplementation "androidx.wear.tiles:tiles-renderer:1.6.0"
}

Kotlin

dependencies {
    // Core Wear Widget and Remote Compose libraries
    implementation("androidx.compose.remote:remote-creation-compose:1.0.0-alpha010")
    implementation("androidx.compose.remote:remote-core:1.0.0-alpha010")
    implementation("androidx.glance.wear:wear:1.0.0-alpha09")
    implementation("androidx.glance.wear:wear-core:1.0.0-alpha09")
    implementation("androidx.wear.compose.remote:remote-material3:1.0.0-alpha03")

    // Tooling for previews (optional, but recommended)
    implementation("androidx.compose.remote:remote-tooling-preview:1.0.0-alpha010")
    implementation("androidx.wear.compose:compose-ui-tooling:1.6.1")
    implementation("androidx.wear.tiles:tiles-tooling-preview:1.6.0")
    debugImplementation("androidx.wear.tiles:tiles-renderer:1.6.0")
}

Hello World ウィジェットを作成する

Wear ウィジェットは、GlanceWearWidgetService を拡張するサービスと、GlanceWearWidget を拡張するウィジェット クラスで構成されます。UI は @RemoteComposable 関数を使用して定義されます。@RemoteComposable 関数を使用します。

サービスを定義する

サービスは、システムがバインドするエントリ ポイントです。

ウィジェットを定義するには、GlanceWearWidgetService を拡張するサービスを作成します。このライブラリは現在開発中であるため、最終的な名前と構造が調整されている間は、一部の API が制限されます。@SuppressLint("RestrictedApi") アノテーションを使用すると、これらの新しい進化中の機能を意図的に使用していることをコンパイラに伝えることができます。この要件は一時的なものであり、今後の安定版リリースで API が確定したら削除される予定です。

@SuppressLint("RestrictedApi")
class HelloWidgetService : GlanceWearWidgetService() {
    override val widget: GlanceWearWidget = HelloWidget()
}

ウィジェットを定義する

ウィジェット クラスは、ウィジェットのデータとレイアウトを提供します。

@SuppressLint("RestrictedApi")
class HelloWidget : GlanceWearWidget() {
    override suspend fun provideWidgetData(
        context: Context,
        params: WearWidgetParams,
    ): WearWidgetData {
        return WearWidgetDocument(background = WearWidgetBrush.color(Color.Blue.rc)) {
            HelloWidgetContent()
        }
    }
}

コンテンツを定義する

コンテンツは Remote Compose コンポーネントを使用して構築されます。

@SuppressLint("RestrictedApi")
@RemoteComposable @Composable
fun HelloWidgetContent() {
    RemoteBox(
        modifier = RemoteModifier.fillMaxSize(),
        contentAlignment = RemoteAlignment.Center,
    ) {
        RemoteText(
            text = "Hello World",
            color = Color.White.rc
        )
    }
}

ウィジェット構成 XML を作成する

新しいファイル res/xml/hello_widget_info.xml を作成して、ウィジェットのプロパティとサポートされているサイズを定義します。<wearwidget-provider> タグでサポートされている XML 属性の完全なリファレンスについては、WearWidgetProviderInfo のドキュメントをご覧ください。

<wearwidget-provider
    description="@string/hello_widget_description"
    icon="@mipmap/ic_launcher"
    label="@string/hello_widget_label"
    preferredType="SMALL">

    <container
        type="SMALL"
        previewImage="@drawable/widget_preview_small" />
    <container
        type="LARGE"
        previewImage="@drawable/widget_preview_large" />
</wearwidget-provider>

AndroidManifest.xml に登録する

必要なインテント フィルタとメタデータを使用して、AndroidManifest.xml にサービスを登録します。

<service
    android:name=".snippets.widget.HelloWidgetService"
    android:exported="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/hello_widget_label"
    android:permission="com.google.android.wearable.permission.BIND_TILE_PROVIDER">

    <intent-filter>
        <action android:name="androidx.glance.wear.action.BIND_WIDGET_PROVIDER" />
        <!-- If you already have a Tile, omit the following line. -->
        <action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />
    </intent-filter>

    <meta-data
        android:name="androidx.glance.wear.widget.provider"
        android:resource="@xml/hello_widget_info" />

    <meta-data
        android:name="androidx.wear.tiles.PREVIEW"
        android:resource="@drawable/tile_preview" />
</service>

ビルドとデプロイ

サービスとウィジェットを定義したら、プロジェクトをビルドしてデバイスまたはエミュレータにデプロイできます。

ビルドとインストール

プロジェクトをビルドし、接続されたデバイスまたはエミュレータにデバッグ APK をインストールします。

./gradlew :app:installDebug

ウィジェットを追加してプレビューする

アプリをインストールしたら、adb を使用して、ウィジェットをプログラムでカルーセルに追加し、画面に表示します。

注: Wear ウィジェットは、デバッグ目的で基盤となるタイル インフラストラクチャを使用します。そのため、adb コマンドには add-tile オペレーションと show-tile オペレーションが必要です。

1. ウィジェットをカルーセルに追加します。

adb shell am broadcast \
  -a com.google.android.wearable.app.DEBUG_SURFACE \
  --es operation add-tile \
  --ecn component <your_package_name>/.HelloWidgetService

2. ウィジェットを表示する:

adb shell am broadcast \
  -a com.google.android.wearable.app.DEBUG_SYSUI \
  --es operation show-tile \
  --ei index 0

Android Studio のプレビューも、さまざまな画面サイズでレイアウトをテストするのに役立ちます。