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