Upgrade your Android app from Firebase.com

This document guides you through upgrading your existing Firebase.com app to the new Firebase console and APIs.

There are four steps to take:

  1. Upgrade your project to the new Firebase console
  2. Install the new Firebase SDK
  3. Update your database code
  4. Update your authentication code

You can upgrade your project to the new firebase.google.com console at any time. Your applications will continue to work. You can then update your code when you are ready to use some of the new Firebase features in your application.

Import your project to the new Firebase console

  • Go to the Firebase console and find your project under "Projects currently at Firebase.com".
  • Click Import for the project you're looking to upgrade.
    • If your project is on a paid plan on firebase.com, you will need to set up the billing for the project in the new console. Your billing information is not automatically migrated
    • Select or create a billing account. After importing, this account is responsible for all charges for the project.
  • Your Realtime Database and Hosting content are automatically and instantly imported to the Firebase console.
  • Your user data is automatically migrated to the new authentication backend. This happens in the background and your users can continue to use the app while the data is being migrated. User signups and logins will not be affected. While the system is migrating user accounts, you will see a spinner in the Auth tab of the Firebase console.
  • If you have an active promo code for a Firebase.com app, reach out to us.

Install the new Firebase SDK

You don't have to update the code of your applications right away. Existing database and authentication code will continue to work against your migrated project. But when you are ready to start using some of the new Firebase features in your application, you can Install the new Firebase SDK (make sure to download the new google-services.json file and add the google-services plugin to your gradle file).

Note that when you start using the new SDK, Google Analytics for Firebase is automatically enabled. By default, your Analytics data will enhance other Firebase features and Google products. You can control how your Analytics data is shared in your settings at any time. Learn more

Update your database code

Update your Gradle dependencies

The easiest way to get started is to change your Gradle dependency:

BEFORE
dependencies {
  compile 'com.firebase:firebase-client-android:x.x.x'
}
AFTER
dependencies {
  compile "com.google.firebase:firebase-database:11.2.0"
}

Fix your class references

Your app will likely now have a number of compile errors because of renamed or moved classes. You should be able to fix many of them with some straightforward substitutions:

Before After
com.firebase.client com.google.firebase.database
FirebaseError DatabaseError
FirebaseException DatabaseException
Firebase.AuthStateListener FirebaseAuth.AuthStateListener
Firebase DatabaseReference

If you can't find a class or method you're looking for, take a look at the Database Reference docs.

Setting the Android Context and enabling Offline Persistence

In the new SDK, it's no longer necessary to call Firebase.setAndroidContext() so you can remove it from your code.

If your app uses disk persistence you now enable it via the FirebaseDatabase object:

BEFORE
Firebase.getDefaultConfig().setPersistenceEnabled(true);
AFTER
FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Like in the 2.x SDK, enabling disk persistence must be done before any other calls to the database are made.

Get a database reference

In the new SDK, Firebase references are replaced by DatabaseReference and you use the FirebaseDatabase class to get an initial reference to your database. So you can get the database reference in your code as follows:

BEFORE
Firebase rootRef = new Firebase("https://<your-app>.firebaseio.com/");
AFTER
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

Note that your Database URL is automatically determined from the google-services.json file you downloaded, so you don't need to specify it. If you want to specify it though, you still can (which might be convenient for migration purposes):

BEFORE
Firebase ref = new Firebase("https://<your-app>.firebaseio.com/path/to/data");
AFTER
DatabaseReference ref = FirebaseDatabase.getInstance()
    .getReferenceFromUrl("https://<your-app>.firebaseio.com/path/to/data");

Update your Java model objects

As with the 2.x SDK, Firebase Database will automatically convert Java objects that you pass to DatabaseReference.setValue() into JSON and can read JSON into Java objects using DataSnapshot.getValue().

In the new SDK, when reading JSON into a Java object with DataSnapshot.getValue(), unknown properties in the JSON are now ignored by default so you no longer need @JsonIgnoreExtraProperties(ignoreUnknown=true).

To exclude fields/getters when writing a Java object to JSON, the annotation is now called @Exclude instead of @JsonIgnore.

BEFORE
@JsonIgnoreExtraProperties(ignoreUnknown=true)
public class ChatMessage {
   public String name;
   public String message;
   @JsonIgnore
   public String ignoreThisField;
}

dataSnapshot.getValue(ChatMessage.class)
AFTER
public class ChatMessage {
   public String name;
   public String message;
   @Exclude
   public String ignoreThisField;
}

dataSnapshot.getValue(ChatMessage.class)

If there is an extra property in your JSON that is not in your Java class, you will see this warning in the log files:

W/ClassMapper: No setter/field for ignoreThisProperty found on class com.firebase.migrationguide.ChatMessage

You can get rid of this warning by putting an @IgnoreExtraProperties annotation on your class. If you want Firebase Database to behave as it did in the 2.x SDK and throw an exception if there are unknown properties, you can put a @ThrowOnExtraProperties annotation on your class.

Update your authentication code

Firebase Authentication functionality now lives behind its own FirebaseAuth class, so authentication operations are now done on a FirebaseAuth instance instead of via a Firebase reference.

Update your Gradle dependencies

Since Authentication now lives in its own module, you first need to add a dependency to your build.gradle:

dependencies {
  compile 'com.google.firebase:firebase-auth:11.2.0'
}

Update your imports

BEFORE
import com.firebase.client.AuthData;
AFTER
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

Sign a user in anonymously

The authentication methods work similarly to how they worked before, but now exist as methods on the FirebaseAuth object with new names, and AuthData is now replaced by FirebaseUser. For example, here is how signing in anonymously works both in the 2.x API and in the new API:

BEFORE
ref.authAnonymously(new Firebase.AuthResultHandler() {
   public void onAuthenticated(AuthData authData) { }
   public void onAuthenticationError(FirebaseError firebaseError) {
       throw firebaseError.toException();
   }
});
AFTER
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.signInAnonymously().addOnFailureListener(new OnFailureListener() {
   public void onFailure(Throwable throwable) {
       throw new RuntimeException(throwable);
   }
});

Sign a user in with a custom token

Authenticating with custom tokens on the client side also works similarly to how they worked before. Here is how signing in with a custom token works both in the 2.x API and in the new API:

BEFORE
ref.authWithCustomToken(AUTH_TOKEN, new Firebase.AuthResultHandler() {
   public void onAuthenticated(AuthData authData) { }
   public void onAuthenticationError(FirebaseError firebaseError) {
       throw firebaseError.toException();
   }
});
AFTER
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.signInWithCustomToken(AUTH_TOKEN)
        .addOnCompleteListener(this, new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {
            }
        });

The custom tokens you generate on your server have a new format. You can use the Firebase Admin SDKs for Node.js and Java to create custom tokens that are compatible with the new API, or you can create custom tokens using a third-party JWT library.

Note that the Firebase Admin SDKs generate custom tokens that expire after one hour, unlike the helper libraries for the 2.x API, which by default generate tokens that expire after 24 hours.

Sign a user in with a social provider

For social providers, such as Google, Facebook, or Twitter, the changes are a bit bigger.

The flow for the social providers is the same as before: you first get the user's credentials from the provider and then use those to authenticate with Firebase:

BEFORE
ref.authWithOAuthToken(provider, token, authResultHandler);
AFTER
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)

For more information see the documentation on using Facebook authentication with Firebase and using Google authentication with Firebase.

Twitter authentication is slightly different again:

BEFORE
Map options = new HashMap<>();
options.put("oauth_token", oauth_token);
options.put("oauth_token_secret", oauth_token_secret);
options.put("user_id", user_id);
ref.authWithOAuthToken(token.provider, options, authResultHandler);
AFTER
AuthCredential credential = TwitterAuthProvider.getCredential(token, secret);
mAuth.signInWithCredential(credential)

For more information see Authenticate Using Twitter

Sign a user out

BEFORE
ref.unauth()
AFTER
FirebaseAuth.getInstance().signOut();

Monitor authentication state

BEFORE
ref.addAuthStateListener(new Firebase.AuthStateListener() {
   @Override
   public void onAuthStateChanged(final AuthData authData) {
       if (authData != null) {
           Log.i("AuthStateChanged", "User is signed in with uid: " + authData.getUid());
       } else {
           Log.i("AuthStateChanged", "No user is signed in.");
       }
   }
});
AFTER
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
   @Override
   public void onAuthStateChanged(@NonNull final FirebaseAuth firebaseAuth) {
       final FirebaseUser user = firebaseAuth.getCurrentUser();
       if (user != null) {
           Log.i("AuthStateChanged", "User is signed in with uid: " + user.getUid());
       } else {
           Log.i("AuthStateChanged", "No user is signed in.");
       }
   }
});

Migrate existing logins

If users have logged into your app with a legacy SDK, then you'll need a bit of code to keep them signed in with the new SDK. Otherwise, they would have to sign in again. Open source sample code for doing this is provided in the Firebase Auth Migration Helpers GitHub repo.

Update the new password reset template

If your app allows users to sign-in with Email & Password authentication , you probably also give these users the option to reset their password.

Once you've upgraded to the new SDK, those password reset emails will use the new templates specified in the Firebase Console. Be sure to update them for the needs of your app.

Update your Firebase libraries

If you're using any of the following libraries, you'll need to upgrade to the latest version.

Library Supported Version Resource
GeoFire 1.2.x Github

Send feedback about...

Need help? Visit our support page.