Jetpack Composeでディープリンクを開くテストをしてみる
ディープリンクを開くには次の書式でコマンドを実行する。
adb shell am start -W -a android.intent.action.VIEW -d <ディープリンクのURI> <アプリのパッケージ名>
実際に入力するときは次の様な感じ。
adb shell am start -W -a android.intent.action.VIEW -d "testapp://home_route" com.example.testapplication
アプリのパッケージ名は、build.gradleのandroid.defaultConfig.applicationId
で確認することができる。
android { ... defaultConfig { applicationId "com.example.testapplication"
開きたいComposableのために、route
とdeepLinks
を定義する。testapp
というディープリンクのスキーマを表すSCHEME
は定数としてどこかで定義しておく。
object HomeDestination : TaNavigationDestination { override val route = "feedl_route" override val destination = "feed_destination" val deepLinks = listOf( navDeepLink { uriPattern = "$SCHEME://${route}" } ) }
navigation
内で呼び出されるcomposable
に対して先ほど定義したroute
とdeepLinks
を渡す。
fun NavGraphBuilder.homeGraph( onUpPress: () -> Unit = {}, ) { composable( route = HomeDestination.route, deepLinks = HomeDestination.deepLinks, ) { navBackStackEntry -> HomeRoute( onUpPress = onUpPress, ) } }
AndroidManifest.xmlにcomposable
に設定した内容に沿って<intent-filter>
を追加する。
<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="testapp" android:host="home_route" /> </intent-filter>
参考
アプリ コンテンツ用のディープリンクを作成する | Android デベロッパー | Android Developers