SPEAKER: Adding support for a new platform
feature in your app can be a very repetitive process.
Many times you will switch back and forth
between your code and a step by step tutorial or documentation,
especially where you're not familiar with the API.
That was the case when I was implementing support
for App Links, in a sample app back when Android
Marshmallow was released.
App Links is a neat feature that lets
you verify a domain that is listed in your activities
content filters.
From that moment forward, whenever a user
clicks on a URL containing this domain,
your app will open automatically without showing
the disambiguation dialog.
I remember that in order to make this API work,
I had to make many small changes in different parts of my app,
which is why I was very curious when I heard about the new App
Links assistant that is part of the Android Studio 2.3 release.
Its promise-- to add App Links support in your project
with just a few clicks without leaving the IDE.
Let's see how that works out.
I found the perfect sample app for my experiment
under Google/searchsamples on Github.
It's a very bare bones recipe app.
The main page doesn't really do anything,
but there is also a content provider with several example
recipes set up and ready to be displayed in the detail page,
implemented in recipe activity.
All I need to do is open the activity
and call Show Recipe, passing it content
URI which matches a recipe in the content provider.
Now, imagine that I'm also running a website for my app,
and I'd like my users to be able to share and open
links to recipes in the same way, regardless of which device
they are using.
Let's see how long it takes to implement this using the App
Links assistant.
You'll find the assistant under the Tools menu,
and it shows up as a panel on the right side here.
First step, I want to define URL mappings in my app.
I will add just one to connect recipe URLs to the detail
activity.
I just have to fill in my website's host name, and what
path to match.
I'll use the path prefix recipe to also capture any recipe ID
after the last path separator.
And I want to launch the recipe activity.
The tool generates the correct intent filter,
and adds it to my manifest.
I can even see a preview of the changes
and check that the URL I need will get matched
to the correct activity.
Step 2, add logic to the activity to handle the URL.
I'll select the activity that I specified in the previous step,
and some code gets added to my uncreate method.
This is just to help you get the necessary data from the intent.
But remember that it's up to you to actually handle it
in your app.
In my case, I want to load and show the correct recipe.
For that, I need the recipe ID, which is the last path
part in the URL.
Now I'll just convert that to a URI, pointing to my content
provider, and pass it on to Show Recipe.
In some cases, such as this one, when the activity's launch
mode is set to single top, you will also
have to handle a new intent delivered
to an already running instance of the activity.
I will refactor the code I just wrote into a new method
and code that from our new intent as well.
Before I move on to configuring App Links on my server,
I just want to check that everything I configured so far
works on the device.
I'll just use the App Links tester in step 4
to launch a URL pointing to the grilled potato salad recipe.
It correctly launches my app, and shows the recipe.
Great.
You might have noticed that there's
a migration dialog that popped up when I launch the URL.
Getting rid of it and launching directly into the app
is the last part.
Let's proceed with step 3, generating the digital asset
links file.
You'll need a few details about your app,
such as the domain you're using in your links, application
ID, and the signing config.
Most of these will normally be pre-filled for you
so you can just click the Generate button.
Now, you will need to place this file under this path
on the domain that you control.
Please note that this is where it's
the easiest to make a mistake.
The path must match exactly.
The well-known folder must be under the root of the domain,
and the server must be using a valid HTTPS certificate
to serve the file even if your App Links are only using HTTP.
For testing my app, I'm just using Github pages,
which gives me an easy way of hosting my asset links file
with the correct SSL certificate.
Let's test the sample link again in step 4.
And the app launches, taking me directly into the recipe.
No need to click through a dialog this time.
That's App Links, fully added to a sample app in just four
steps, without looking at the documentation.
If you like the App Links assistant,
or if you have any feedback, please let us
know on our social channels.
Thanks for watching, and for more Android studio videos,
click here.