A lightweight and simple kotlin library for deep link handling on Android.
Configure root build.gradle
(jitpack.io):
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add Linkt
to your project build.gradle
:
dependencies {
implementation 'com.github.jeziellago:Linkt:TAG'
}
- Create your
DeepLinkModule
and register deep links:
class MyDeepLinkModule : DeepLinkModule {
override fun load() {
deepLinkOf(
"linkt://sample",
"linkt://sample/{userId}/{userName}"
) { context, bundle ->
Intent(context, MainActivity::class.java)
.apply { putExtras(bundle) }
}
}
}
In multi-module projects you should have one or more DeepLinkModule`s.
- Register your modules into
Application#onCreate
, withDeepLinkLoader#setup
:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
DeepLinkLoader.setup(MyDeepLinkModule())
}
}
- Create the
DeepLinkActivity
(or use yours if already exists), and callDeepLinkLoader#loadFrom
:
class DeepLinkActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// resolve deeplink
DeepLinkLoader.loadFrom(this)
}
}
Don't forget to configure AndroidManifest.xml
(required for Android deep links):
<activity
android:name="org.linkt.DeepLinkActivity"
android:theme="@android:style/Theme.NoDisplay">
<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="mycompany" />
</intent-filter>
</activity>
In your activity, you can get path parameters and query values as String
from intent.extras
:
- Template
linkt://sample/{userId}/{userName}
- Received:
linkt://sample/9999/Jose
// get path parameters
val userId = intent.extras.getString("userId")
val userId = intent.extras.getString("userName")
- Template:
linkt://sample
- Received
linkt://sample?subject=Linkt&name=Sample
// get query parameters
val subject = intent.extras.getString("subject")
val name = intent.extras.getString("name")
- Template
linkt://sample/{userId}/{userName}
- Received
linkt://sample/999/Jose?subject=Linkt&name=Sample
// get path parameters
val userId = intent.extras.getString("userId")
val userId = intent.extras.getString("userName")
// get query parameters
val subject = intent.extras.getString("subject")
val name = intent.extras.getString("name")
Copyright (c) 2021 Jeziel Lago
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.