안드로이드에서 딥 링크 활성화하는 법
() translation by (you can also view the original English article)
딥링크란?
안드로이드의 딥 링크는 앱 내에서 특정 페이지를 열어 (선택적으로) 해당 페이지에 데이터를 전달합니다. 특히 개발자들은 딥 링크를 알림을 클릭하거나 이메일을 통해 앱 링크를 전송하는 것과 같은 동작에 유용하게 사용할 수 있습니다.
이메일 클라이언트를 예로 들어 봅시다. 사용자가 이메일 수신 알림을 클릭하면 앱에서 해당 이메일로 이동하는 딥 링크가 열립니다. 한 가지 더 중요한 점을 말하자면 딥 링크가 구글로 하여금 앱을 인덱싱해서 검색 결과에 포함된 앱의 특정 영역으로 연결해 줄 수도 있다는 것입니다. 딥 링크는 구글 검색 결과로 나타나며 사용자를 앱의 특정 영역으로 데려다 줄 수 있습니다.
딥 링크 구현
앱에 딥 링크를 추가하려면 안드로이드 매니페스트 파일에 딥 링크를 인텐트 필터로 추가해야 합니다. 다음 예제를 봅시다.
1 |
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" |
2 |
android:supportsRtl="true" android:theme="@style/AppTheme"> |
3 |
<activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> |
4 |
<intent-filter>
|
5 |
<!-- Notice that the MAIN activity already has an intent-filter. This is not
|
6 |
A deep link because its action is not a VIEW-->
|
7 |
<action android:name="android.intent.action.MAIN" /> |
8 |
<category android:name="android.intent.category.LAUNCHER" /> |
9 |
</intent-filter>
|
10 |
</activity>
|
11 |
<activity android:name="com.example.matthew.deeplinks.LinkActivity" android:label="@string/title_activity_link" |
12 |
android:theme="@style/AppTheme.NoActionBar"> |
13 |
<intent-filter>
|
14 |
<!-- Sets the intent action to view the activity -->
|
15 |
<action android:name="android.intent.action.VIEW" /> |
16 |
<!-- Allows the link to be opened from a web browser -->
|
17 |
<category android:name="android.intent.category.BROWSABLE" /> |
18 |
<!-- Allows the deep link to be used without specifying the app name -->
|
19 |
<category android:name="android.intent.category.DEFAULT" /> |
20 |
<!-- URI tutsplus://deeplink -->
|
21 |
<data android:scheme="tutsplus" android:host="deeplink"/> |
22 |
<!-- URI https://www.mydeeplink.com -->
|
23 |
<data android:scheme="http" android:host="www.mydeeplink.com"/> |
24 |
</intent-filter>
|
25 |
</activity>
|
26 |
</application>
|
<action>
과 <data>
태그가 필요합니다. <action>
태그에서는 링크가 클릭됐을 때 어떤 일이 일어날지를 선택합니다. <data>
태그에서는 페이지에 대한 딥 링크로 어떤 URI를 사용할 수 있는지 지정합니다.
위 예제에서 http://www.mydeeplink.com이나 tutsplus://deeplink를 클릭하면 사용자를 LinkActivity
액티비티로 데려갑니다. <category>
태그에는 딥 링크의 속성을 지정합니다. 참고로 각 URI 스킴과 액티비티에 대해서는 별도의 인텐트 필터를 만들어야 합니다.
한 액티비티에 링크를 여러 개 만들 수도 있습니다. 각 링크를 구분하려면 코드상에서 인텐트의 데이터를 파싱해서 링크를 구분해야 합니다. 이 작업은 보통 onCreate()
메서드에서 데이터를 읽어 그에 따라 동작을 수행하는 식으로 이뤄집니다.
1 |
protected void onCreate(Bundle savedInstanceState) { |
2 |
super.onCreate(savedInstanceState); |
3 |
setContentView(R.layout.activity_link); |
4 |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
5 |
setSupportActionBar(toolbar); |
6 |
|
7 |
Intent in = getIntent(); |
8 |
Uri data = in.getData(); |
9 |
// Do something with data. For example, open certain email in view.
|
10 |
}
|
딥 링크 테스트
안드로이드 스튜디오에서는 손쉽게 딥 링크를 테스트할 수 있습니다. Run > Edit Configurations를 차례로 클릭해 프로젝트 설정을 편집합니다.






상단의 General 탭을 열고 Launch Options 영역에 있는 Deep Link 필드에 URI를 입력합니다. 안드로이드 스튜디오를 이용해 앱을 실행하면 지정된 URI를 열려고 시도합니다.
결론
이제 딥 링크를 만들고 사용하는 법을 배웠으므로 사용자가 앱과 상호작용하는 새로운 진입점을 만들 수 있습니다. 사용자는 휴대전화에서 구글 검색을 통해 앱 내의 페이지를 찾을 수 있으며, 여러분은 클릭됐을 때 앱 내에서 특정 페이지를 여는 알림을 만들 수 있습니다.