1. Code
  2. Mobile Development
  3. Android Development

Optimize Your Mobile Application for Google

Scroll to top
Final product imageFinal product imageFinal product image
What You'll Be Creating

Have you ever wondered how those mysterious links pointing to apps appear in your search results when you search the web with Google? How can app developers make sure their apps are displayed here?

In this tutorial, I will demonstrate how to achieve this effect for your apps.

1. What's App Indexing?

Google App Indexing (now re-branded Firebase App Indexing), enables native apps to be listed in Google search results, as if they were normal webpages. The app entries, which would appear together with normal webpage entries, are displayed when a relevant search query is submitted from an appropriate device's browser. The feature is currently supported on Android and iOS platforms.

Why Do Developers Need It?

App Indexing maximizes a developer's chances of success, because it performs two important tasks simultaneously.

Firstly, it improves and enhances the mobile user experience. If an app relevant to a search query is found, the user is given the option to install the app or directly launch the app (if it is already installed on the device), without even having to leave the browser. Thus, the users will have a positive first impression about such an app, and they will subconsciously appreciate the fact that the app managed to appear within the right context, and that it provided them with several options to consider.

Secondly, it helps developers promote their apps directly via Google's search engine. Developers can harness this mechanism to drive traffic directly to their apps and increase the download count dramatically.

How Is It Technically Possible?

App Indexing works its magic with a fairly simple but clever way of establishing a relationship between an app and its website, aptly named "site association". The workflow consists of several steps:

  1. Adding an Intent Filter to support URLs and Google Search
  2. Associating a website with the app
  3. Adding App Indexing API code

2. Let's Build It

Now that we have an idea of the workflow, we'll proceed with each step, and finally, test our implementation on Android Studio. So, we'll create a new Android Project on Android Studio IDE. You'll need to have Android Studio 2.0 or a higher version installed on your machine. Create a new Android Project with an Empty Activity. I have used MyApp as the name of my new Android Project.

Create a new Project in Android StudioCreate a new Project in Android StudioCreate a new Project in Android Studio

Step 1

We'll start by adding an Intent Filter to support URLs and Google Search. Intents are the communicating agents between Android app components, and using Intent Filters is the standard way of introducing an implicit intent into your app. 

An implicit intent in your app enables it to request that the OS evaluate all the matching registered components (possibly within other installed apps) and select one of them, thus making it possible to perform a certain task using some other app installed on the system. An explicit intent, by contrast, directly specifies a target app component, so that only the specified component will be called upon to perform the action. You can read more on Intents and Intent Filters on the Android Developers website. 

Now, we'll get started creating an Intent Filter.

  • Open the AndroidManifest.xml file.
  • Select an <activity> element with the cursor.
  • Right-click while the selection is on, and then select Generate from the pop-up menu.
  • Further select URL from the secondary pop-up menu.
Select Generate from the pop-up menuSelect Generate from the pop-up menuSelect Generate from the pop-up menu

You'll see that Android Studio has added some additional code to your AndroidManifest.xml file. You can change the attributes of the <data> element of the generated code to fit your requirements. In my case, I have provided values related to my demo website.

Be sure to provide the necessary values for both http and https schemes.

1
<?xml version="1.0" encoding="utf-8"?>
2
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3
    package="com.mycompany.myapp">
4
5
    <application
6
        android:allowBackup="true"
7
        android:icon="@mipmap/ic_launcher"
8
        android:label="@string/app_name"
9
        android:supportsRtl="true"
10
        android:theme="@style/AppTheme">
11
        <activity android:name=".MainActivity">
12
            <intent-filter>
13
                <action android:name="android.intent.action.MAIN" />
14
15
                <category android:name="android.intent.category.LAUNCHER" />
16
            </intent-filter><!-- ATTENTION: This intent was auto-generated. Follow instructions at

17
  https://g.co/AppIndexing/AndroidStudio to publish your URLs. -->
18
            <intent-filter>
19
                <action android:name="android.intent.action.VIEW" />
20
21
                <category android:name="android.intent.category.DEFAULT" />
22
                <category android:name="android.intent.category.BROWSABLE" />
23
                <!-- ATTENTION: This data URL was auto-generated. We recommend that you use the HTTP scheme.

24
                  TODO: Change the host or pathPrefix as necessary. -->
25
                
26
                <data
27
                    android:host="sites.google.com"
28
                    android:pathPrefix="/site/appindexingex/home/main"
29
                    android:scheme="http" />
30
                <data
31
                    android:host="sites.google.com"
32
                    android:pathPrefix="/site/appindexingex/home/main"
33
                    android:scheme="https" />
34
            </intent-filter>
35
        </activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for

36
     App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
37
        <meta-data
38
            android:name="com.google.android.gms.version"
39
            android:value="@integer/google_play_services_version" />
40
    </application>
41
42
</manifest>

Step 2

Now we need to associate a website with our app.

You will need to have verified the web URL that you wish to associate with your app using the Google Search Console. Just log in to the Search Console with your Google account and you'll find easy-to-follow instructions for verifying the URL.

You can also find detailed instructions on how to define the structure of the associated website to match that of your app in the Firebase App Indexing Guide. However, since we are only testing the app using Android Studio, we won't need to explore those details in this tutorial.

For our testing purposes, all we need is a live web URL verified by Google. The attributes you provide for the <data> element in Step 1 must correspond to this verified web URL, which must be fully functional.

Step 3

Finally we'll add App Indexing API code. Android Studio provides an automated way of doing this:

  • Open the MainActivity.java file of your Android Project.
  • Select the word onCreate with the cursor.
  • Right-click while the selection is on, and select Generate from the pop-up menu.
  • Further select the App Indexing API Code from the secondary pop-up menu.
Select the App Indexing API Code from the secondary pop-up menuSelect the App Indexing API Code from the secondary pop-up menuSelect the App Indexing API Code from the secondary pop-up menu

You'll see the auto-generated code added by Android Studio to your MainActivity.java file. Be sure to edit the links of the Uri.parse() function, so that they will correctly point to the URLs associated with your app.

1
package com.mycompany.myapp;
2
3
import android.net.Uri;
4
import android.support.v7.app.AppCompatActivity;
5
import android.os.Bundle;
6
7
import com.google.android.gms.appindexing.Action;
8
import com.google.android.gms.appindexing.AppIndex;
9
import com.google.android.gms.common.api.GoogleApiClient;
10
11
public class MainActivity extends AppCompatActivity {
12
13
    /**

14
     * ATTENTION: This was auto-generated to implement the App Indexing API.

15
     * See https://g.co/AppIndexing/AndroidStudio for more information.

16
     */
17
    private GoogleApiClient client;
18
19
    @Override
20
    protected void onCreate(Bundle savedInstanceState) {
21
        super.onCreate(savedInstanceState);
22
        setContentView(R.layout.activity_main);
23
        // ATTENTION: This was auto-generated to implement the App Indexing API.

24
        // See https://g.co/AppIndexing/AndroidStudio for more information.

25
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
26
    }
27
28
    @Override
29
    public void onStart() {
30
        super.onStart();
31
32
        // ATTENTION: This was auto-generated to implement the App Indexing API.

33
        // See https://g.co/AppIndexing/AndroidStudio for more information.

34
        client.connect();
35
        Action viewAction = Action.newAction(
36
                Action.TYPE_VIEW, // TODO: choose an action type.

37
                "Main Page", // TODO: Define a title for the content shown.

38
                // TODO: If you have web page content that matches this app activity's content,

39
                // make sure this auto-generated web page URL is correct.

40
                // Otherwise, set the URL to null.

41
42
                
43
44
                Uri.parse("http://sites.google.com/site/appindexingex/home/main"),
45
                // TODO: Make sure this auto-generated app URL is correct.

46
                Uri.parse("android-app://com.mycompany.myapp/http/sites.google.com/site/appindexingex/home/main")
47
        );
48
        AppIndex.AppIndexApi.start(client, viewAction);
49
    }
50
51
    @Override
52
    public void onStop() {
53
        super.onStop();
54
55
        // ATTENTION: This was auto-generated to implement the App Indexing API.

56
        // See https://g.co/AppIndexing/AndroidStudio for more information.

57
        Action viewAction = Action.newAction(
58
                Action.TYPE_VIEW, // TODO: choose an action type.

59
                "Main Page", // TODO: Define a title for the content shown.

60
                // TODO: If you have web page content that matches this app activity's content,

61
                // make sure this auto-generated web page URL is correct.

62
                // Otherwise, set the URL to null.

63
64
65
                Uri.parse("http://sites.google.com/site/appindexingex/home/main"),
66
                // TODO: Make sure this auto-generated app URL is correct.

67
                Uri.parse("android-app://com.mycompany.myapp/http/sites.google.com/site/appindexingex/home/main")
68
        );
69
        AppIndex.AppIndexApi.end(client, viewAction);
70
        client.disconnect();
71
    }
72
}

3. Testing It

Now, we'll test our work on Android Studio, to make sure that our App Indexing implementation works correctly. First, make sure that you are connected to the Internet. Then, on Android Studio, select Tools > Android > Google App Indexing Test.

On Android Studio select Tools  Android  Google App Indexing TestOn Android Studio select Tools  Android  Google App Indexing TestOn Android Studio select Tools  Android  Google App Indexing Test

After some time, a screen similar to the one below will appear. The passing test results assure you that App Indexing will work correctly in your app.

Test result screen showing test passedTest result screen showing test passedTest result screen showing test passed

Conclusion

App Indexing is a useful feature for your app to attract more downloads, and it will definitely help you target a wider user base as a developer. In this tutorial, we checked only whether our App Indexing implementation works correctly, but not how exactly the user perceives the functionality resulting from the implementation. You can proceed to that next level by finding more information in the Firebase, Google Webmasters, and Android Studio App Indexing Help websites.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.