androidgoogle-api-clientandroid-app-indexing

What is the new generated code "This was auto-generated to implement the App Indexing API."?


Background

I just created a new POC today (about activity transitions, but this isn't the topic), and I've noticed a new line being written in the "onCreate" method of the main activty:

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

And more:

@Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        mClient.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "SinglePhotoViewer Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.user.transitionstest/http/host/path")
        );
        AppIndex.AppIndexApi.start(mClient, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "SinglePhotoViewer Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.user.transitionstest/http/host/path")
        );
        AppIndex.AppIndexApi.end(mClient, viewAction);
        mClient.disconnect();
    }

and this was added to the manifest:

<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
        App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

The problem

Looking at the website that was written, I still don't get what it is.

I guess this is how to use it, but I don't get how it all works.

Also, weird thing is that any other new project that I create doesn't show this new line of code

The questions

  1. What is this? What does it do?
  2. What should I do with it?
  3. Are there any customizations for it? Any recommendations?
  4. On which cases does this line of code get generated? I didn't notice how and when it gets created...

My guess, according to what I've read and seen on the website, is that this is only used for apps that can perform some sort of searching, so that Google could show the user previous queries and faster results.


Solution

  • You are correct: that code is automatically created for you by Android Studio, to aid in the implementation of the App Indexing API.

    However, it is not created by simply adding a new activity to your app. You would need to explicitly ask Android Studio to create this code. You would then need to update it with details of your activity: Type of Action, Title, Deep Link, Corresponding Web Page (if one exists).

    To have this code generated for you, you can use the pop-up intention list by Alt + Enter, select “Insert App Indexing API Code”:

    enter image description here

    Or you can use pop-up code generate list by Alt + Insert, select “App Indexing API Code”:

    enter image description here

    Here is the relevant Google Developers documentation:

    https://developers.google.com/app-indexing/android/test#link-creation

    There are really just four pieces to this you need to tweak:

    // What type of action is this? (TYPE_VIEW, TYPE_LISTEN, TYPE_WATCH, etc...)    
    Action.TYPE_VIEW
    
    // Title of your page, just like the web
    "SinglePhotoViewer Page"
    
    // The web url of corresponding content, if exists, otherwise leave blank, ""
    Uri.parse("http://host/path") 
    
    // Your deep link starting with "android-app://"
    Uri.parse("android-app://com.example.user.transitionstest/http/host/path")
    

    As a best practice, you should pick a title that most accurately describes the content at that deep link location in your app. Just like you would in the <TITLE></TITLE> tags in an HTML webpage header.

    Once implemented, any activity that is viewed by your end user will report this deep link to the Android OS. Then it will be available in the Suggest Autocomplete results when a user types a query into the Google Quicksearch Box. Your app icon and the title you have provided will be shown in the Suggest results if the users query matches your title by keyword.

    Here's an example of what it would look like, from the end users point of view, in the Live Nation app, assuming he had previously visited the two pages shown in the Suggest results on the left:

    enter image description here

    Furthermore, by implementing the App Indexing API you will get a ranking boost in search results as noted in the link you provided in your original question:

    This enables query autocompletions for your app users, as well as richer search results, improved Search quality, and enhanced ranking signals.

    Finally, you may be interested in this code lab as an additional resource:

    https://codelabs.developers.google.com/codelabs/app-indexing/#0