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

3

18

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.

Unyielding answered 27/12, 2015 at 14:27 Comment(1)
this just happened by accident to me since it was the default autocomplete option ... and now my build fails as a result. The additional code would just magically come back if I deleted it from the Gradle files, and only touching the files on disk outside AndroidStudio and then telling it to sync Gradle files stopped the madness.Impassive
H
16

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

Hanlon answered 5/1, 2016 at 22:40 Comment(15)
interesting, but i still don't get: What is it used for? What does it do for the user? How does it work?Unyielding
Sorry, allow me to try to answer those questions succinctly: What does it do for the user? It allows the user to quickly and directly access content deep inside apps, that they have already visited. How does it work? By presenting deep link content the user has already visited inside apps in the auto complete results of GSA (the Google App).Hanlon
The example I present from the users point of view is the Live Nation example above, does that make sense?Hanlon
Please explain. I don't understand the image. What's the full scenario?Unyielding
1: User launches Live Nation app. 2: User navigates to Kenny Chesney page. 3: Live nation app makes app indexing api call .start() method. 4: User exists Live Nation app. 5: User searches for "Kenny" in Google Quicksearch Box. 6: Details are provided in auto suggest 7: User clicks details and is taken directly to Kenny Chesney page deep linkHanlon
so it all happens only when the user has searched for something withing the app, and then searched for the same thing via Google search?Unyielding
Yes. Although the user does not need to "search" inside the app itself, those API calls can be made while the user is navigating normally. Of course, an in-app search could also work. And then yes, the user would need to search for some part of the the TITLE string in order to be returned in the Google App.Hanlon
interesting. ok then. I will mark this answer as the correct one. If you find any useful information about this, please write it down.Unyielding
Are you sure it's not automatically inserted? I have never ever asked AS to insert this code, but it did anyway three times in different projects.Masthead
@Christine, yes there should be no way that this code is automatically generated without any trigger from the user. Is it possible you are using a keyboard shortcut?Hanlon
It must be an inadvertent activation via keyboard shorcut, then. Sometimes one of the cats jumps on my keyboard, maybe they pressed the right keys. Like they like to press ctrl-minus to make browser characters minuscule.Masthead
So is this Android's version of Windows COM+Titanium
@Hanlon Can I remove it without any worries? It seems like I triggered it without knowing.Breckenridge
@José, yes you should be fine removing it as it's optional code.Hanlon
If I type Alt + Enter I get the option "Insert app indexing API code". Once this has been selected, is there an option to uninstall the code automatically? This would save me deleting each line of code manually.Jollenta
A
3

In case this helps, you can disable that option by going to:

Settings > Intentions > Android > Insert App Indexing API code

and unchecking it.

Aventurine answered 11/11, 2016 at 22:9 Comment(1)
not able to find the last part - "Insert App Indexing API code" - is there any particular section to search underLaughton
H
0

you can disable that option by unchecking the option at:

Settings > Editor > Intentions > Android > Insert App Indexing API code

To find it, type "api code" into the search box on the File>Settings window. It's there in my installation of Android Studio 2.2.3

Ha answered 1/7, 2017 at 3:5 Comment(1)
I don't see it there.Unyielding

© 2022 - 2024 — McMap. All rights reserved.