Migrate to the Latest API

If you’re currently using a version of the Firebase App Indexing API older than 10.0.0 and want to move to the latest version, follow these steps.

Before you start, make sure you've added Firebase to your app.

Update the App Indexing library

Go to your app’s build.gradle script and add the following dependency:

dependencies {
  ...
  compile 'com.google.firebase:firebase-appindexing:10.0.1'
  ...
}
      

Update the imports in your activity

      // Before
      import com.google.android.gms.appindexing.Action;
      import com.google.android.gms.appindexing.AppIndex;
      import com.google.android.gms.common.api.GoogleApiClient;

      // After
      import com.google.firebase.appindexing.Action;
      import com.google.firebase.appindexing.FirebaseUserActions;
      import com.google.firebase.appindexing.Indexable;
      import com.google.firebase.appindexing.builders.Actions;
      

Remove calls to the Google API Client

public class ViewMessageActivity extends Activity {
  ...
  // Delete this
  private GoogleApiClient mClient;
  ...

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ...
    // Delete this
   mClient = new GoogleApiClient.Builder(this)
        .addApi(AppIndex.API)
        .build();
    …
   }
 }
 

Update the user action builders

      //Before
      public Action getAction() {
       Thing object = new Thing.Builder()
        .setName(mText)
        .setUrl(mUrl)
        .build();


      return new Action.Builder(Action.TYPE_VIEW)
        .setObject(object)
        .build();
     }
      

      // After
     public Action getAction() {
       return new Action.Builder(Action.Builder.TYPE_VIEW)
         .setObject(mText, mUrl)
         .build();
     }
      

Update the user action calls

Before you log actions on any content in the app using the new version of the API, make sure you've added it to the on-device index. Any call to either start() or stop() logging should now be preceded by a call to add the content to the index first, if you haven't already added it.

Before
//Before

@Override
protected void onStart() {
   super.onStart();
   mClient.connect();
   AppIndex.AppIndexApi.start(mClient, getAction());
}


@Override
protected void onStop() {
   AppIndex.AppIndexApi.end(mClient, getAction());
   mClient.disconnect();
   super.onStop();
}


// After

@Override
protected void onStart() {
   super.onStart();
/* If you’re logging an action on an item that has already been added
to the index, you don’t have to add the following line.  */
   FirebaseAppIndex.getInstance().update(getIndexable());
   FirebaseUserActions.getInstance().start(getAction());
}


@Override
protected void onStop() {
   FirebaseUserActions.getInstance().end(getAction());
   super.onStop();
      

Send feedback about...

Need help? Visit our support page.