Android Studio helps you add support for URLs, app indexing, and search functionality to your apps. These features can help to drive more traffic to your app, discover which app content is used most, make it easier for users to find content in an installed app, and attract new users.
To use Android Studio to add support for URL, app indexing, and search features to your app:
Intent filters and the App Indexing API are ways to implement URL support and app indexing, but there are other possible implementations as well. See Alternate Android Indexing Methods for more information.
Android Studio can create a basic intent filter in your manifest that you can customize to define URLs for your app. You can then write Java code in an activity to handle the intent. This implementation lets users directly open the specified 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.
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 defined in your intent filters 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 verification for URLs 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.
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 can’t get content from your app.
Android Studio helps you test your code with the following features:
The details for implementing URL support and app indexing are described next.
To use Android Studio features to add an intent filter defining a URL:
AndroidManifest.xml
file to open it
in the Code Editor.<activity>
element, click in the left column so the light bulb
<activity>
element and select Generate
> URL.The Code Editor adds skeleton code using the intention action and generate code mechanisms.
The Code Editor adds an intent filter similar to the following:
<!-- ATTENTION: This intent was auto-generated. Follow instructions at https://g.co/AppIndexing/AndroidStudio to publish your URLs. --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category 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. --> <data android:host="www.example.com" android:pathPrefix="/gizmos" android:scheme="http" /> </intent-filter>
<data>
element
and optionally the <category>
element, as needed.We recommend that you define a <data>
element that supports URLs that you’ll
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 it’s included in search results.
The URLs you specify in your intent filters can be the same as the URLs of the comparable pages on your website.
To support Google Search for your URLs:
Alternatively, for Android 6.0 (API level 23) and higher, add link default handling and verification.
To test and debug your links, you can use the following Android Studio features:
In addition, you can preview your APK in the Google Search Console to test your URLs, whether the app is associated with a website or not.
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:
The Code Editor adds skeleton code using the intention action and generate code mechanisms.
If you don’t 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:
onStart()
method, or the onStart()
method doesn’t contain an AppIndexApi.start()
or AppIndexApi.view()
call.onStop()
method, or the onStop()
method doesn’t 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 API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient client; // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); } @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. client.connect(); Action viewAction = Action.newAction( Action.TYPE_VIEW, // TODO: choose an action type. "Main 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://www.example.com/gizmos"), // TODO: Make sure this auto-generated app URL is correct. Uri.parse("android-app://com.example/http/www.example.com/gizmos") ); AppIndex.AppIndexApi.start(client, 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. "Main 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://www.example.com/gizmos"), // TODO: Make sure this auto-generated app URL is correct. Uri.parse("android-app://com.example/http/www.example.com/gizmos") ); AppIndex.AppIndexApi.end(client, viewAction); client.disconnect(); } }
For more information about the App Indexing API methods, see Android API for App Indexing. For information about the action types, see the Action Class Constant Summary.
If your app isn’t already configured for the Google Play Services App Indexing API, the Code
Editor also modifies your build.gradle
and AndroidManifest.xml
files
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
and Setting Up Google Play Services.
Pay attention to the comments, which help you find areas that need work, such as setting the title and URLs. For more information, see Add the App Indexing API.
To test and debug your App Indexing API code, you can use the following Android Studio features:
In addition, you can preview your APK in the Google Search Console.
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:
Or type the URL you want to test, for example, http://example.com/gizmos
.
If the link is successful, the app launches in the device or emulator, and displays the app at the specified activity. Otherwise, an error message appears in the Run window.
For more information about creating run configurations at the project, default, and module levels, see Build and Run Your App.
You can view App Indexing API log messages while the app is running, as described next.
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:
App indexing log messages should appear. If they don’t, check the following items:
build.gradle
file? 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 page and Setting Up Google Play Services.
You can use the Android Studio built-in Lint tool to check whether you have valid URLs defined 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:
To set default Lint checks for URLs and the App Indexing API:
For example, the following Lint warning appears for the first setting:
To produce a list of Lint checks displayed in the Inspection Results window:
The scope specifies the files you want to analyze, and the profile specifies the Lint checks you’d like to perform.
In the Inspections dialog, you can search for the string "app indexing" to find the URL and App Indexing API Lint checks. Note that changing Lint settings for a profile in the Inspections dialog doesn’t change the default settings, as described in the previous procedure. It does change the settings for profiles displayed in the Inspections dialog, however.
The results appear in the Inspection Results window.
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:
To perform a Google App Indexing Test:
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 find the URL.
The following table lists common errors and warnings you might encounter.
Warning or Error | Description |
---|---|
Error: Google cannot index this page. | Your app can't be crawled by Googlebot or using the App Indexing API, so Google isn't able to index this app. |
Warning: The App URL you sent by using the App Indexing API doesn't match the URL opened. | When calling the App Indexing API, the URL specified in the app must match the opened URL. |
Warning: Google cannot index this page using the App Indexing API because the title is empty. | When calling the App Indexing API, the title shouldn't be empty. |
Warning: Google can index this page using Googlebot crawling but identified blocked resources. | The app references other resources, and some of them are blocked or
temporarily unavailable. If these resources aren't critical, it might not
matter. Check the preview on the right to see whether the content
displays correctly. To fix this issue, make sure the resources aren't
blocked by robots.txt . |
Warning: Google cannot index this page using the App Indexing API. | Your app isn’t using the App Indexing API. We recommended adding App Indexing API support to your app. |