Вы находитесь на странице: 1из 13

Android Developers

Add URL and App Indexing


Support
Android Studio helps you add support for
URLs, app indexing, and search
functionality to your apps. These features
can help to drive more trafc to your app,
discover which app content is used most,
make it easier for users to nd content in
an installed app, and attract new users.

In this document
Typical Workow
Adding an Intent Filter for
URL Support and Google
Search
Adding App Indexing API
Skeleton Code to an Activity
Testing a URL

Typical Workow
To use Android Studio to add support for URL,
app indexing, and search features to your app:
1. Add intent lters and code to handle

Viewing App Indexing API


Messages in the logcat
Monitor
Conguring Lint
Performing a Google App
Indexing Test

incoming intents.
2. Associate a website with your app.
3. Add App Indexing API code.

See also
App Indexing

Intent lters and the App Indexing API are

Making Your App Content


Searchable by Google

ways to implement URL support and app

Handling App Links

indexing, but there are other possible

Improving Your Code with lint

implementations as well. See Alternate

logcat Monitor

Android Indexing Methods

Google Search Console

(https://developers.google.com/app-indexing/reference

Google Play Services

/deeplinks)

for more information.

Intent lters for URLs


Android Studio can create a basic intent lter
in your manifest that you can customize to
dene URLs for your app. You can then write

Video
DevBytes: App Indexing

Dependencies and

Java code in an activity to handle the intent.

prerequisites

This implementation lets users directly open


the specied app activity by clicking a URL.
Users can see the URLs in google.com in a
browser, in the Google Search app, and in
Google Now on Tap.

Android 2.3 (API level 9) or


higher
Android Studio 1.5 or higher
Google Play Services 8.1.0 or
higher

Website association with


URLs
After setting up URL support for your app, you can associate your website with your
app by using the Google Search Console and Google Play Developer Console.
Afterward, Google indexes your app for URLs dened in your intent lters and
begins to include them in search results. In addition, you can optionally exclude app
content from Google Search. After you associate a website with your app, features
such as Now on Tap and enhanced search result display (like including your app
icon) become available.
As an alternative to associating your app with a website, for Android 6.0 (API level
23) and higher, you can add default handlers and verication for URLs
(https://developer.android.com/training/app-links/index.html)

instead.

Chrome displaying google.com serves search results with URLs that are accessible
to both signed-in users and those who aren't. Google Search app users must be
signed in to see URLs in their search results.

App Indexing API code in activities


Next, add the App Indexing API code to your app. Android Studio can create
skeleton code in an activity that you can then customize to support app indexing.
The App Indexing API allows app indexing even if Googlebot
(https://developers.google.com/app-indexing/reference/deeplinks#allow-access-by-googlebot-optional)

cant

get content from your app.

URL support and App Indexing API testing


Android Studio helps you test your code with the following features:
URL support testing helps you verify that a specied URL can launch an app.
The logcat Monitor helps you test App Indexing API calls in an activity.
The Android Lint tool has warnings for certain issues involving URL support and

the App Indexing API. These warnings and errors appear in the Code Editor and
in Lint inspection results.
A Google App Indexing test checks whether Google can index a URL by either
crawling your app page or using the App Indexing API.
The details for implementing URL support and app indexing are described next.

Adding an Intent Filter for URL Support and


Google Search
To use Android Studio features to add an intent lter dening a URL:
1. In the Android view (https://developer.android.com/studio/projects/index.html#ProjectFiles) of the
Project window, double-click the AndroidManifest.xml le to open it in the
Code Editor.
2. Insert an intent lter in one of the following ways:
In an <activity> element, click in the left column so the light bulb
appears. Click

and select Add URL.

Right-click in an <activity> element and select Generate > URL.


Place your cursor in an activity, and then select Code > Generate > URL.
The Code Editor adds skeleton code using the intention action
(https://www.jetbrains.com/idea/help/intention-actions.html)

and generate code

(https://www.jetbrains.com/idea/help/generating-code.html)

mechanisms.

The Code Editor adds an intent lter similar to the following:

<!-- ATTENTION: This intent was auto-generated. Follow instructions at


https://g.co/AppIndexing/AndroidStudio to publish your URLs. -->
<<iinntteenntt--ffiilltteerr>>
<<aaccttiioonn android:name="android.intent.action.VIEW" //>>

<<ccaatteeggoorryy android:name="android.intent.category.DEFAULT" //>>


<<ccaatteeggoorryy android:name="android.intent.category.BROWSABLE" //>>
<!-- ATTENTION: This data URL was auto-generated.
We recommend that you use the HTTP scheme.
TODO: Change the host or pathPrefix as necessary. -->
<<ddaattaa
android:host="www.example.com"
android:pathPrefix="/gizmos"

android:scheme="http" //>>
<<//iinntteenntt--ffiilltteerr>>

3. Modify the <data> element (https://developer.android.com/training/app-indexing/deeplinking.html#adding-lters)

and optionally the <category> element, as needed.

We recommend that you dene a <data> element that supports URLs that youll
add in the future. In the previous sample code, for example, Google will index
any URLs starting with http://www.example.com/gizmos. Also, remember to
include a URL for your app home screen so its included in search results.
The URLs you specify in your intent lters can be the same as the URLs of the
comparable pages on your website.
4. In the corresponding activity, add Java code (https://developer.android.com/training
/app-indexing/deep-linking.html#handling-intents)

to read data from the intent lter and direct

the app to respond accordingly.


5. Test your URL (#testintent).
To support Google Search for your URLs:
1. Dene an association (https://developers.google.com/app-indexing/android/app#declarea-website-association)

between your app and your website.

Alternatively, for Android 6.0 (API level 23) and higher, add link default handling
and verication (https://developer.android.com/training/app-links/index.html).
2. Optionally exclude certain URLs (https://developers.google.com/app-indexing/android
/app#create-the-noindexxml-le)

from the Google index.

3. Optionally add App Indexing API code (#indexing) to support additional search
features.
To test and debug your links, you can use the following Android Studio features:
Launch your URL (#testintent) in Android Studio to test that it works.
Enable the following Android Lint categories (#lint):
Missing support for Google App Indexing
URL not supported by app for Google App Indexing
Perform a Google App Indexing Test (#appindexingtest).
In addition, you can preview your APK in the Google Search Console

(https://developers.google.com/app-indexing/android/test#preview-your-apk-on-search-console)

to test

your URLs, whether the app is associated with a website or not.

Adding App Indexing API Skeleton Code to


an Activity
After adding URL support to your app, you can add App Indexing API code to an
activity to support additional search features.
To add App Indexing API code to an activity:
1. In Android view (https://developer.android.com/studio/projects/index.html#ProjectFiles) in the
Project window, double-click the activity Java le to open it in the Code Editor.
2. Insert skeleton code in one of the following ways:
In an activity denition, click in the Java code so the light bulb
Click

appears.

and select Insert App Indexing API Code.

Right-click in an activity denition and select Generate > App Indexing API
Code.
Place the cursor in an activity, and then select Code > Generate > App
Indexing API Code.
The Code Editor adds skeleton code using the intention action
(https://www.jetbrains.com/idea/help/intention-actions.html)

and generate code

(https://www.jetbrains.com/idea/help/generating-code.html)

mechanisms.

If you dont see the App Indexing API Code menu item, make sure your cursor is
within an activity, and check your code for App Indexing API methods. The Code
Editor can insert skeleton Java code into an activity in the following
circumstances:
The activity doesnt have an onStart() method, or the onStart() method
doesnt contain an AppIndexApi.start() or AppIndexApi.view() call.
The activity doesnt have an onStop() method, or the onStop() method
doesnt contain an AppIndexApi.end() or AppIndexApi.viewEnd() call.
The Code Editor adds Java code similar to the following:
/**

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


* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
pprriivvaattee GGoooogglleeAAppiiCClliieenntt client;

// ATTENTION: This was auto-generated to implement the App Inde


// See https://g.co/AppIndexing/AndroidStudio for more informat
client = nneeww GGoooogglleeAAppiiCClliieenntt.BBuuiillddeerr(tthhiiss).addApi(AAppppIInnddeexx
}

@Override
ppuubblliicc vvooiidd onStart() {
ssuuppeerr.onStart();

// ATTENTION: This was auto-generated to implement the App Inde


// See https://g.co/AppIndexing/AndroidStudio for more informat
client.connect();
AAccttiioonn viewAction = AAccttiioonn.newAction(
AAccttiioonn.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content sh
// TODO: If you have web page content that matches
// this app activity's content,
// make sure this auto-generated web page URL is correc
// Otherwise, set the URL to null.
UUrrii.parse("http://www.example.com/gizmos"),
// TODO: Make sure this auto-generated app URL is corre
UUrrii.parse("android-app://com.example/http/www.example.c
);
AAppppIInnddeexx.AAppppIInnddeexxAAppii.start(client, viewAction);

@Override
ppuubblliicc vvooiidd onStop() {
ssuuppeerr.onStop();

// ATTENTION: This was auto-generated to implement the App Inde


// See https://g.co/AppIndexing/AndroidStudio for more informat
AAccttiioonn viewAction = AAccttiioonn.newAction(
AAccttiioonn.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content sh
// TODO: If you have web page content that matches
// this app activity's content,
// make sure this auto-generated web page URL is correc
// Otherwise, set the URL to null.
UUrrii.parse("http://www.example.com/gizmos"),
// TODO: Make sure this auto-generated app URL is corre
UUrrii.parse("android-app://com.example/http/www.example.c
);
AAppppIInnddeexx.AAppppIInnddeexxAAppii.eenndd(client, viewAction);
client.disconnect();

For more information about the App Indexing API methods, see Android API
for App Indexing (https://developers.google.com/app-indexing/reference/package-summary)

. For

information about the action types, see the Action Class Constant Summary
(https://developers.google.com/android/reference/com/google/android/gms/appindexing
/Action#constant-summary)

If your app isnt already congured for the Google Play Services App Indexing
API, the Code Editor also modies your build.gradle and
AndroidManifest.xml les to include it. If your app already depends on it but

the version is lower than 8.1.0, your app is upgraded to version 8.1.0. For more
information and to correct any issues, see Add Google Play Services
(https://developers.google.com/app-indexing/android/publish#add-google-play-services)

and

Setting Up Google Play Services (https://developers.google.com/android/guides/setup)

3. Customize the skeleton code, as needed.


Pay attention to the comments, which help you nd areas that need work, such
as setting the title and URLs. For more information, see Add the App Indexing
API (https://developers.google.com/app-indexing/android/publish)

4. Verify that your app indexing code is working (#testindexing) by using the logcat
Monitor.
To test and debug your App Indexing API code, you can use the following Android
Studio features:
Examine logcat Monitor Messages (#testindexing).
Enable the following Android Lint category (#lint): Missing support for Google App
Indexing API
Perform a Google App Indexing Test (#appindexingtest).
In addition, you can preview your APK in the Google Search Console
(https://developers.google.com/app-indexing/android/test#preview-your-apk-on-search-console)

Testing a URL
When you run your app from Android Studio, you can specify a URL to launch so you
can test it.
To launch a URL from Android Studio:

1. In Android Studio, open your project in Android view (https://developer.android.com/studio


/projects/index.html#ProjectFiles).

2. After opening a project, select Run > Edit Congurations.


3. In the Run/Debug Congurations dialog, beneath Android Application, select the
module you want to test.
4. Select the General tab.
5. In the Launch eld, select URL.
6. In the URL eld, click to select from a list of dened URLs.
Or type the URL you want to test, for example, http://example.com/gizmos.
7. Click OK.
8. Select Run > Run app or Debug app.
9. If the Select Deployment Target dialog appears, select a connected device or an
emulator, and click OK.
If the link is successful, the app launches in the device or emulator, and displays
the app at the specied activity. Otherwise, an error message appears in the Run
window.
For more information about creating run congurations at the project, default, and
module levels, see Build and Run Your App (https://developer.android.com/tools/building/buildingstudio.html).

You can view App Indexing API log messages while the app is running, as described
next.

Viewing App Indexing API Messages in the


logcat Monitor
The logcat Monitor can display app indexing log messages to determine if your App
Indexing API code is pushing the correct data to the cloud. For example, you can
check the app title and the URL. The logcat Monitor is part of Android Monitor in
Android Studio.
To view App Indexing API messages in the logcat Monitor:
1. Run your app in Android Studio so it launches a URL (#testintent).

2. Display Android Monitor (https://developer.android.com/tools/help/am-logcat.html#running) and


click the logcat tab.
3. Set the log level (https://developer.android.com/tools/help/am-logcat.html#level) to Verbose.
4. In the lter menu, select No Filters.
5. Search the log (https://developer.android.com/tools/help/am-logcat.html#searching) for the string
"appindex".
App indexing log messages should appear. If they dont, check the following
items:
Is Google Play Services installed on the device or emulator? You can check
the settings on the device.
Is the Google Play Services version on the device or emulator lower than the
version specied in the build.gradle le? If so, it might be out-of-date and
should be upgraded to a higher version.
For more information, see the Google Play Services Download
(https://play.google.com/store/apps/details?id=com.google.android.gms&hl=en)

page and

Setting Up Google Play Services (https://developers.google.com/android/guides/setup)

6. Visit app pages that trigger App Indexing API calls.

Conguring Lint
You can use the Android Studio built-in Lint tool to check whether you have valid
URLs dened in the manifest and have implemented the App Indexing API correctly
in activities.
You can view URL and app indexing warnings and errors in two ways:
As pop-up text in the Code Editor. When Lint nds a problem, it highlights the
problematic code in yellow, or underlines the code in red for more serious
issues.
In the Lint Inspection Results window after you select Analyze > Inspect Code.
To set default Lint checks for URLs and the App Indexing API:
1. In Android Studio, open your project in Android view (https://developer.android.com/studio
/projects/index.html#ProjectFiles).

2. Select File > Other Settings > Default Settings.


3. In the Default Preferences dialog, select Editor > Inspections.
4. In the Prole eld, select Default or Project Default to set the scope for Android
Studio or just for this project, respectively.
5. Expand the Android Lint category and change the Lint settings as needed:
Missing support for Google App Indexing - Reports a warning if the app
hasnt implemented URLs, which are used by Google Search. This warning
setting is enabled by default.
Missing support for Google App Indexing API - Reports if an app hasnt
implemented the App Indexing API at all. This warning setting is disabled by
default.
URL not supported by app for Google App Indexing - Reports URL errors in
manifest code. This error setting is enabled by default.
For example, the following Lint warning appears for the rst setting:

6. Click OK.
To produce a list of Lint checks displayed in the Inspection Results window:
1. In Android Studio, open your project in Android view (https://developer.android.com/studio
/projects/index.html#ProjectFiles)

and select a portion of your project that you want to

test.
2. Select Analyze > Inspect Code.
3. In the Specify Inspection Scope dialog, select the inspection scope and prole.
The scope species the les you want to analyze, and the prole species the
Lint checks youd like to perform.
4. If you want to change the Lint settings, click . In the Inspections dialog,
optionally click Manage to dene a new prole, specify the Lint settings you

want, and then click OK.


In the Inspections dialog, you can search for the string "app indexing" to nd the
URL and App Indexing API Lint checks. Note that changing Lint settings for a
prole in the Inspections dialog doesnt change the default settings, as described
in the previous procedure. It does change the settings for proles displayed in
the Inspections dialog, however.
5. Click OK.
The results appear in the Inspection Results window.

Performing a Google App Indexing Test


You can use a Google App Indexing Test to check whether Google can index a URL
by either crawling your app page or using the App Indexing API. Google can index
the URL if your app supports at least one of the following:
Googlebot can open and crawl the page identied by the URL.
Your app sends the correct data by using the App Indexing API.
To perform a Google App Indexing Test:
1. Add URL (#intent) and App Indexing API (#indexing) support to your app.
2. While your project is open in Android Studio, select Tools > Android > Google
App Indexing Test.
3. In the Google App Indexing Test dialog, select a Module, URL, and Language.
4. Log in if you see a message asking you to log into a Google Cloud Platform
account.
5. In the Google App Indexing Test dialog, click OK.
Android Studio builds the APK and starts the test. The test can take a few
minutes to complete. The results appear in a new tab in the Code Editor.

If the app preview on the right shows the screen that corresponds to the URL
you're testing, then Googlebot can nd the URL.
6. Correct any issues the test identies, and repeat the test as often as needed.
The following table lists common errors and warnings you might encounter.
Warning or Error

Description

Error: Google

Your app can't be crawled by Googlebot or using the

cannot index this

App Indexing API, so Google isn't able to index this app.

page.
Warning: The App

When calling the App Indexing API, the URL specied in

URL you sent by

the app must match the opened URL.

using the App


Indexing API
doesn't match the
URL opened.
Warning: Google

When calling the App Indexing API, the title shouldn't be

cannot index this

empty.

page using the App


Indexing API
because the title is
empty.
Warning: Google

The app references other resources, and some of them

can index this page

are blocked or temporarily unavailable. If these

using Googlebot

resources aren't critical, it might not matter. Check the

crawling but

preview on the right to see whether the content displays

identied blocked

correctly. To x this issue, make sure the resources

resources.

aren't blocked by robots.txt (https://support.google.com


/webmasters/answer/6062608)

Warning: Google

Your app isnt using the App Indexing API. We

cannot index this

recommended adding App Indexing API support to your

page using the App

app.

Indexing API.

Вам также может понравиться