Have you ever visited a website that had an app, but you couldn’t be bothered to download it? Well, Google has a solution for you!
With Instant Apps you get a beautiful interface with native performance and capabilities in a single tap. The user is able to visit any part of your website, like search, social media, messaging or pretty much any other URL, and view it in the Instant App without needing to install it first.
If you’re familiar with Android, you are probably sceptical about whether or not this is going to be worth it – it’s probably just for the latest version (8.0 at the time of writing) anyway, right? Well, I’ve got some great news for you: It’s available all the way back to 5.0!
An Instant App is not a new, separate app. You can use the same APIs, the same project and most importantly, the same source code. To start, we’ll need an existing project that you want to turn into an Instant App. Another requirement is the Instant App SDK, to download it go to Tools > SDK Manager. On the SDK Tools tab, check the “Instant Apps Development SDK” option and click OK to install it.
Next comes a big part. We’ll need to turn the app into multiple modules. If you already did that, you’re golden! Basically, what we need to do is turn the part of the app we want to be an Instant App into a feature module. This module will act as an AAR when part of the regular application and will become the instant application when built as Instant App. It’s best to think of this feature module as a library. You can use one or more feature modules in an Instant App. One thing to note is that there can only be one base feature (one entry point).
If you look under File > New… > Module, you should see something like this:
This will create a feature module that is ready to be consumed by either the application or the Instant App modules. If you already have a separate module, you can simply change the existing build.gradle file as follows:
- Replace “apply plugin: ‘com.android.application’” with “apply plugin: ‘com.android.feature’”
- If you have an “applicationId” property in “defaultConfig” section remove it
- Choose a base feature, and modify the android section to contain “baseFeature = true”
You can then use the feature module just like any other module. If you happened to turn your entire app into an
Instant App you’ll also need to add the Phone & Tablet module under File > New… > Module
You don’t need to add any activity, all you need to do is add dependency on the feature module. For example, if your feature module is called “instant-app-base” all you need to do is add the following:
https://gist.github.com/zskamljic/2b284c21b7b96f09be402a3a5ea5381f
If you sync gradle now, and run the app once it’s done, it should run and perform just like before.
Now that we’ve got a feature module, and the app can also be installed and used on its own, we can continue with the instant part! Before we continue, we’ll need to remove the application from the device or simulator, since the Instant App doesn’t need to pop up if the app is already installed. Now let’s create an Instant App module, and make it depend on the instant-app-base module, just like we did with the Phone & Tablet module. To make it run, we need to add an app link. We can do that through Tools > App Links Assistant. A tab should open on the right where we select the “Open URL Mapping Editor” button. We enter the host URL of our webpage and select a pathPrefix of /blog:
It is advisable to add both http and https hosts with same path and same activity.
All we need to do now is to make it runnable. Let’s open the Run/Debug configurations and set the URL to one of those we specified earlier:
We can now run the application, to verify that it’s running as an Instant App we can expand the quick settings and see something like this:
All you need to do now is publish it on the developer console and you’re done. The procedure to do that is pretty much the same as regular publishing, simply select “Release management > Android Instant Apps”
This was just a glimpse at Instant Apps, the full documentation is available here.