FirebaseUI for Android

repository·master·Indexed 26 days ago

https://github.com/firebase/firebaseui-android

An open-source library providing UI bindings for common Firebase services, including Auth, Firestore, Realtime Database, and Cloud Storage. It allows developers to quickly connect UI elements to Firebase APIs, featuring a comprehensive authentication flow with support for Email, Google, Phone, Facebook, Apple, and custom OAuth providers via the FirebaseAuthUI class and AuthUIConfiguration DSL.

Tokens
35.4K
Snippets
85
Records
128
Agent score
89%

What's inside FirebaseUI for Android

  1. Choose between FirebaseRecyclerAdapter and FirebaseRecyclerPagingAdapter

    master

    FirebaseUI for Realtime Database provides two types of adapters for RecyclerView depending on your data needs:

    • FirebaseRecyclerAdapter: Binds a Query to a RecyclerView and responds to all real-time events (additions, removals, moves, or changes). Use this for small result sets where real-time updates are required.
    • FirebaseRecyclerPagingAdapter: Binds a Query to a RecyclerView by loading data in pages. Use this for large, static data sets to improve memory efficiency. Note: This adapter does not respect real-time events; it will not detect new, removed, or changed items once loaded.
  2. Choose a Firestore RecyclerView adapter

    master

    FirebaseUI provides two types of adapters for binding Cloud Firestore queries to a RecyclerView:

    • FirestoreRecyclerAdapter: Binds a Query and responds to all real-time events (additions, removals, moves, or changes). Best for small result sets where all data should be loaded at once.
    • FirestorePagingAdapter: Binds a Query by loading data in pages. Best for large, static data sets. Note that this adapter does not support real-time events; it will not detect new/removed items or changes to items already loaded.
  3. Register FirebaseImageLoader in AppGlideModule

    master

    To enable Glide to handle StorageReference objects, you must register the FirebaseImageLoader.Factory within a custom AppGlideModule. This class is processed by the Glide annotation processor at compile time to generate the GlideApp class.

    @GlideModule
    public class MyAppGlideModule extends AppGlideModule {
    
        @Override
        public void registerComponents(Context context, Glide glide, Registry registry) {
            // Register FirebaseImageLoader to handle StorageReference
            registry.append(StorageReference.class, InputStream.class,
                    new FirebaseImageLoader.Factory());
        }
    }
  4. Manage FirestoreRecyclerAdapter lifecycle

    master

    By default, you must manually manage the lifecycle of a FirestoreRecyclerAdapter to start and stop the underlying Firestore snapshot listener:

    • Call startListening() in onStart() to begin receiving updates.
    • Call stopListening() in onStop() to remove the listener and clear data.

    Automatic Lifecycle Management: To avoid manual calls, pass a LifecycleOwner to the FirestoreRecyclerOptions.Builder#setLifecycleOwner(...) method. FirebaseUI will then automatically handle startListening() and stopListening() based on the lifecycle state.

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
  5. Populate a ListView using FirebaseListAdapter

    master

    For simpler list implementations using ListView, use FirebaseListAdapter. This approach is analogous to FirebaseRecyclerAdapter but does not use a ViewHolder. You must implement populateView to bind your data model to the view.

    FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
            .setQuery(query, Chat.class)
            .build();
    
    FirebaseListAdapter<Chat> adapter = new FirebaseListAdapter<Chat>(options) {
        @Override
        protected void populateView(View v, Chat model, int position) {
            // Bind the Chat to the view
            // ...
        }
    };
  6. Configure Java 8 and AGP 7 for FirebaseUI

    master

    To use FirebaseUI v8.0 or higher, your application must be configured to use Java 8 language features and Android Gradle Plugin (AGP) 7.0 or higher.

    Java 8 Configuration

    Add the following to your app's build.gradle to enable Java 8 compatibility for both Java and Kotlin modules:

    AGP 7 Configuration

    In your root build.gradle file, ensure the com.android.tools.build:gradle dependency is set to version 7.0.0 or higher. Note that this requires Gradle 7 and JDK 11 to be installed on your development machine.

    android {
        ... 
        // Configure only for each module that uses Java 8
        // language features (either in its source code or
        // through dependencies).
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        // For Kotlin projects
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
    buildscript {
      // ...
      dependencies {
        // ...
        classpath 'com.android.tools.build:gradle:7.0.0'
      }
    }
  7. Inherit from your app's Material Theme

    master

    Use AuthUITheme.fromMaterialTheme() to automatically inherit your app's existing Material Design 3 colors, typography, and shapes. You can also pass arguments to fromMaterialTheme() to override specific properties while inheriting the rest.

    // Basic inheritance
    val configuration = authUIConfiguration {
        providers {
            provider(AuthProvider.Email())
        }
        theme = AuthUITheme.fromMaterialTheme()
    }
    
    // Inheritance with overrides
    val configuration = authUIConfiguration {
        providers {
            provider(AuthProvider.Google())
            provider(AuthProvider.Facebook())
        }
        theme = AuthUITheme.fromMaterialTheme(
            providerButtonShape = RoundedCornerShape(16.dp)  // Override button shape
        )
    }
  8. Define a data model for Cloud Firestore

    master

    To use FirebaseUI with Cloud Firestore, your data model classes must follow these requirements for automatic serialization and deserialization:

    1. JavaBean Naming Pattern: Use standard getters and setters (e.g., getName() maps to the name field) so Firestore can map data to field names.
    2. Empty Constructor: You must provide a public empty constructor, which is required for Firestore's automatic data mapping.

    Example model class:

    public class Chat {
        private String mName;
        private String mMessage;
        private String mUid;
        private Date mTimestamp;
    
        public Chat() { }
    
        public Chat(String name, String message, String uid) {
            mName = name;
            mMessage = message;
            mUid = uid;
        }
    
        public String getName() { return mName; }
        public void setName(String name) { mName = name; }
        public String getMessage() { return mMessage; }
        public void setMessage(String message) { mMessage = message; }
        public String getUid() { return mUid; }
        public void setUid(String uid) { mUid = uid; }
    
        @ServerTimestamp
        public Date getTimestamp() { return mTimestamp; }
        public void setTimestamp(Date timestamp) { mTimestamp = timestamp; }
    }
  9. Dependency requirements for FirebaseUI 5.0

    master

    FirebaseUI version 5.0.0 requires specific minimum versions of Firebase dependencies due to major updates in the underlying Firebase SDK. To avoid conflicts, ensure your application does not declare any of the following dependencies at a version lower than specified:

    • com.google.firebase:firebase-core must be at least 16.0.9
    • com.google.firebase:firebase-auth must be at least 17.0.0
    • com.google.firebase:firebase-firestore must be at least 19.0.0
    • com.google.firebase:firebase-database must be at least 17.0.0
    • com.google.firebase:firebase-storage must be at least 17.0.0
    // Ensure these are NOT lower than these versions when using FirebaseUI 5.0.0
    com.google.firebase:firebase-core:16.0.9
    com.google.firebase:firebase-auth:17.0.0
    com.google.firebase:firebase-firestore:19.0.0
    com.google.firebase:firebase-database:17.0.0
    com.google.firebase:firebase-storage:17.0.0
  10. Migrate Facebook and Twitter authentication providers

    master
    Starting with FirebaseUI 2.0, the library no longer includes direct dependencies on the Facebook or Twitter SDKs. If your application uses these authentication providers, you must manually include the appropriate SDKs in your project dependencies to prevent runtime crashes.
  11. Create a release branch

    master

    To release a new version, create a dedicated release branch from the latest development branch.

    1. Checkout and pull the latest version-x.y.z-dev branch.
    2. Create a new branch named version-x.y.z from the HEAD of the dev branch.
    3. Update Config.kt and gradle.properties to remove SNAPSHOT from the version name and set the release version.
    4. Update README.md and auth/README.md with the latest version number and transitive dependency descriptions.
    5. Commit changes and push the branch to GitHub to create a pull request against master.
    $ VERSION=1.2.3
    $ git checkout version-$VERSION-dev && git pull origin version-$VERSION-dev
    $ git checkout -b version-$VERSION
    
    # After making changes...
    $ git commit -am "Version x.y.z"
    $ git push -u origin HEAD:version-$VERSION
  12. Migrate Cloud Firestore usage for FirebaseUI 4.0

    master
    When upgrading to FirebaseUI 4.0, you must adopt the breaking changes introduced in the firebase-firestore library version 16.0.0. Specifically, QueryListenOptions has been removed and replaced by the MetadataChanges enum. Refer to the official Firebase Android release notes for detailed migration steps regarding Firestore.