Instant Apps: Getting Started

In this tutorial, you’ll learn how to integrate Google Play Instant with your Android project using Instant Development SDK. By Ivan Kušt.

3.7 (3) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 3 of 4 of this article. Click here to view the first page.

App Links

The next thing you’ll do is create an App Link to your new instant app.

What are App Links exactly?

They are a special type of deep links for Android. They allow your website URLs to open your app without the user needing to select the app.

Using App Links, you can create banners on your website that launch your instant app! Nice, right?

There are two steps to creating App Links:

  • Verify the ownership of the app and website URL.
  • Add a deep link to a place in your app.

Verifying App Links

To verify your deep link to make it a valid App link you need to perform the following steps:

  • Request an automatic App Link verification in your AndroidManifest.xml file.
  • Host a Digital Asset Links JSON file on your domain to declare a relationship between your website and the app.

You must host the Digital Asset Links JSON file on your domain in the following location:

https://domain.name/.well-known/assetlinks.json 
Note: For more information and file format specifications, check the official digital asset links documentation. There is also official documentation on verifying app links.

Implementing App Links

You’ll need to add a deep link in the AndroidManifest.xml you added in instant package. This is done by adding a second intent-filter element to the activity element for the MainActivity. You can see the code below:

 <activity
    android:name="com.raywenderlich.android.fromdusktilldawn.ui.main.MainActivity"
    android:theme="@style/AppTheme.Splash">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter android:autoVerify="true">
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.BROWSABLE" />
      <category android:name="android.intent.category.DEFAULT" />

      <data
        android:scheme="http"
        android:host="example.com"
        android:pathPrefix="/main" />

      <data
        android:scheme="https" />
  </activity>

The android:autoVerify="true" property in the intent-filter declares that the deep link was verified.

Note: You have to add both Http and Https schemes in order for App Links to work. For more details, visit the official deep linking documentation.

Testing App Links

To test your new App Link, you’ll use the gradle task that you defined earlier. Run the runinstantInstantDebug task with a url parameter set to http://example.com/main.

Using a gradle wrapper, run the task with the following command:

On Mac or Linux:

./gradlew runinstantInstantDebug --url=http://example.com/main

On Windows:

gradlew runinstantInstantDebug --url=http://example.com/main

After this command builds successfully, the app will open the instant app:

Using Multiple Entry Points

You can add even more App Links to your instant app.

Start by adding another App Link to LocationDetailsActivity. Open the AndroidManifest.xml file and add a new intent-filter to LocationDetailsActivity:

<activity android:name=".ui.locationdetail.LocationDetailActivity" >
  <intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />

    <data
      android:scheme="http"
      android:host="example.com"
      android:pathPrefix="/location" />

    <data
      android:scheme="https" />
  </intent-filter>
</activity>

This will add a new App Link that opens LocationDetailActivity with this url:

http://example.com/location?lat=45.8150&lng=15.9819

There is one more change you need to make: Open the LocationDetailViewModel.kt file and replace line 61 with the following:

val coordinates = if(intent.data != null) {
    Coordinates(
    intent.data?.getQueryParameter("lat")?.toDouble() ?: 0.0,
    intent.data?.getQueryParameter("lng")?.toDouble() ?: 0.0
    )
  } else {
    intent.extras?.get(COORDINATES_ARGUMENT) as? Coordinates
  }

This will parse lat and lng parameters passed with the App Link. You know that App Link opened the activity if intent.data is not null.

You can now test the new entry point by executing the following in the command line:

./gradlew runinstantInstantDebug --url="http://example.com/location?lat=45.8150&lng=15.9819"

Awesome work! You’ve now successfully created an App Link with multiple entry points!

Specifying the Default Entry Point

If you have multiple App Links, there are two ways to specify which one will be used as an entry point when users click Try Now in the Google Play Store:

  • Leave your default activity as an entry point for the Try Now experience.
  • Add URLs to activities and declare a default URL, which then becomes the entry point for the Try Now experience.

You can specify the default URL by adding the following in AndroidManifest.xml inside the application tags:

<meta-data android:name="default-url"
  android:value="https://example.com/location" />
Note: There is a new way to test this using ia tool, but Google Play Store uses the code above when users select the Try Now option.

Additional Options

There are some more options provided by the InstantApps API that are worth mentioning:

  • You can prompt the user to install the full version of the app.
  • You can determine whether the app is running in instant or installed mode.

Prompting the User to Install

The user tried your new instant app, but what now?

Well, you want them to install the full version of the app, of course!

There’s a simple way to prompt users to install the full version of the app from the instant app, and here’s how.

You’ll prompt users to install the full version of your app by using InstantApps APIs. They provide a convenient method called showInstallPrompt(), which shows an install dialog.

First, open the build.gradle file for the app module. Add the InstantApps dependency:

implementation 'com.google.android.gms:play-services-instantapps:16.0.1'

Sync the project with the gradle files.

Open the MainActivity.kt file and add the following code to the beginning MainActivity class:

  /**
   * Intended to launch after the app has been installed.
   */
  private val postInstallIntent = Intent(
      Intent.ACTION_VIEW, 
      Uri.parse("https://example.com/location")
  ).addCategory(Intent.CATEGORY_BROWSABLE)
   .putExtras(Bundle().apply {
      putString("The key to", "sending data via intent")
    })

You’ve defined postInstallIntent, which will be passed to the newly installed app the first time it’s run.

Add one more field to MainActivity:

private var askedToInstall = false

This serves as a boolean flag to let you know if you’ve already asked the user to install the full version. You don’t want to be too eager and bore the user.

Next, go to the searchForLocation() function and change its body to the following:

if (!askedToInstall) {
  askedToInstall = true

  InstantApps.showInstallPrompt(this,
    postInstallIntent,
    REQUEST_CODE,
    REFERRER)
} else {
  viewModel.searchFor(locationName).observe(this, Observer { coordinates ->
    if (coordinates != null) {
      startActivity(LocationDetailViewModel.createIntent(this, coordinates))
    } else {
      AlertDialog.Builder(this)
          .setMessage(R.string.cant_find_given_location)
          .setPositiveButton(R.string.ok, null)
          .show()
    }
  })
}

Next, add the following import:

import com.google.android.gms.instantapps.InstantApps

And define the following constants in the companion object:

companion object {
  private val REFERRER = "InstallApiActivity"
  private val REQUEST_CODE = 7
}

This defines the referrer and the request code that have to be passed to the showInstallPrompt() function.

Build and run the app using the runinstantInstantDebug gradle task.

This will show an install prompt one time when the user searches for a location:

Note: Hmm, the prompt looks kind of strange. That’s right! You’ll have to upload the app to Google Play Store to see a valid prompt.