Testing deeplinks using ki
ki is a Kotlin Interactive Shell developed by the folks at JetBrains.
One of my colleagues recently shared a neat little Ruby script to get an ad-hoc localhost webpage up and running with all deeplink URLs.
We’ll be doing a similar exercise with ki.
Even though ktor would be the popular choice for this article only for simplicity sake (number of modules to be loaded to get things up and running), I’ll be using Javalin
as the web framework of choice.
Let’s get started
A very helpful feature of ki is that you can import libraries on the fly. Let’s import javalin by using the :dependsOn
function. Once downloaded, import io.javalin.Javalin
into your shell.
Using the :paste
mode, we’ll be assigning our HTML string to a variable val html
With Javalin
imported and HTML string ready for use lets create our server and provide an endpoint /deeplink
which will provide our ad-hoc webpage.
Then, javalin.start()
with your desired port (7000 in this case), to fire up the localhost server.
Testing on emulator
With javalin started on localhost:7000
we now verify our ad-hoc webpage on an emulator.
Use your system’s ip-address (using ifconfig on Mac) to access the webpage from your device’s browser.
In the example,
- running
ifconfig
gives172.20.10.3
as the system’s ip-address. - since we started Javalin on the port 7000, we fire
172.20.10.3:7000/deeplink
- Google External has
https://www.google.com
as the URL and hence opens the webpage. - The Twitter link has
twitter://user?id=monsterhunter
the URL which is the deeplink for the Twitter apps. Hence it opens the Twitter app instead.
Running a .kts script instead
ki also allows you to load an already created .kts
script file into the shell. So So can save some time and effort by consolidating all the above-written code into a .kts
file instead.
This script exposes a Javalin
object which can be used to start a localhost server.
Use :l
command to load the script and then use :ls
to check for the variables and methods exposed by the script.
Well we already have a way to test deeplinks using adb
. Apart from notifications, web pages are another popular source of launching deeplinks. So, I just wanted you to introduce some simple utilities of the ki Kotlin-shell and hope this article could do some justice to that 😄
Happy coding 💻 and stay safe during these trying times 😷