Firebase Java SDK

Built and maintained with 🧡 by
GitLive
Development teams merge faster with GitLive
The Firebase Java SDK is a pure java port of the
Firebase Android SDK
to run in clientside java environments such as the desktop.
Note this is different to the official Firebase Admin Java SDK which enables access
to Firebase services from privileged environments (such as servers or cloud).
It is used by the Firebase Kotlin SDK to support the JVM target for
multiplatform projects, enabling you to use Firebase directly from your common source targeting Desktop,
iOS, Android and Web, enabling the use of Firebase as a backend for
Compose Multiplatform, for example.
It's currently in an alpha state, to be used at your own discretion, and contributions are very welcome!
Using in your projects
You can add the library via Gradle:
dependencies {
implementation("dev.gitlive:firebase-java-sdk:0.6.2")
}
Or Maven:
<dependency>
<groupId>dev.gitlive</groupId>
<artifactId>firebase-java-sdk</artifactId>
<version>0.6.2</version>
</dependency>
You can skip the above if you are using the SDK via the Firebase Kotlin SDK
Initializing the SDK
Before you can use the SDK you need to call the FirebasePlatform.initializeFirebasePlatform function to provide an implementation for
logging, and persistent storage for simple key value pairs. This is used by Firebase Auth to persist the signed-in user.
Firebase Auth calls these methods from background threads, so your implementation must be thread-safe. The other Firebase
products store their data in files under getDatabasePath() and getFilesDir(), described below.
Here's a simple example implementation in Kotlin that only persists in-memory:
FirebasePlatform.initializeFirebasePlatform(object : FirebasePlatform() {
val storage = ConcurrentHashMap<String, String>()
override fun store(key: String, value: String) = storage.set(key, value)
override fun retrieve(key: String) = storage[key]
override fun clear(key: String) { storage.remove(key) }
override fun log(msg: String) = println(msg)
})
Customizing Firestore offline data persistence database location
The FirebasePlatform interface also includes a getDatabasePath method for you to override if the following default implementation is not suitable:
open fun getDatabasePath(name: String): File = File("${System.getProperty("java.io.tmpdir")}${File.separatorChar}$name")
This is used by Firestore to support offline data persistence.
Customizing file storage location
The FirebasePlatform interface also includes a getFilesDir method for you to override if the following default implementation is not suitable:
open fun getFilesDir(): File = File("${System.getProperty("java.io.tmpdir")}${File.separatorChar}firebase-files")
This is used by Remote Config and Installations to persist fetched configs and the installation ID, and by all Firebase
products to store their preferences, as JSON files in a shared_prefs subfolder.
The default is in the OS temp folder, which the OS may clean up one file at a time. These files only make sense together: if
Remote Config's fetched configs are removed while its preferences remain, it can serve default values until the template next
changes. Production apps should override getFilesDir() with a persistent folder of their own.
Initialize the Firebase application
It is also up to you to initialize the Firebase application object manually (unlike the Android SDK which is normally initialized via
the configuration file).
You first need to create a Firebase options object to hold the configuration data for the Firebase application. Full documentation for
the options can be found in the Android API reference documentation.
The use of FirebaseOptions.Builder is shown in the following example:
// Manually configure Firebase Options. The following fields are REQUIRED:
// - Project ID
// - App ID
// - API Key
FirebaseOptions options = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
You can then pass this to the initialize function, along with a mock Android context supplied by the firebase-java-sdk, like so:
import android.app.Application
val app = Firebase.initialize(Application(), options)
Project status
The following libraries are available for the various Firebase products.
[^1]: Google has not open-sourced the Firebase Auth implementation for Android so a basic implementation using the Rest API is provided.
[^2]: Realtime updates (addOnConfigUpdateListener) and defaults from XML resources are not supported.
Is the Firebase library or API you need missing? Create an issue to request additional API coverage or be awesome and submit a PR.
Limitations
Currently, the following limitations are observed:
Firebase Auth is a REST implementation and does not cover the whole Android API:
Supported: anonymous, email and password, custom token, email link and credential sign-in (Google, Facebook, GitHub,
Twitter, a generic OAuth provider, a phone verification code), linking a credential, profile, email and phone number
updates, password reset, email verification and the other action-code flows, sign-in method lookup, token refresh and
the auth state and ID token listeners.
Not supported, as they need a device or a browser: phone number verification (PhoneAuthProvider.verifyPhoneNumber
reports a failure to its callbacks), multi-factor enrolment and sign-in, provider sign-in through an Activity
(startActivityForSignInWithProvider and the FirebaseUser counterparts fail), reCAPTCHA and multi-tenancy (setTenantId throws). Not yet implemented:
reauthenticate, reauthenticateAndRetrieveData, unlink and updatePassword.
Realtime Database does not support Disk Persistence, and should be setup as follows:
FirebaseDatabase.getInstance().setPersistenceEnabled(false)
Building and contributing
This library is built with Gradle.
Run ./gradlew build to build the library locally.
Implementation details
Apart from Firebase Auth, the Firebase Android libraries and their dependencies are used as-is from the published maven artifacts available
at Google's Maven Repository. These are obviously designed to run on Android so to use them without modification we
include a minimal Android emulation layer in the SDK only implementing the functionality used by the Firebase libraries at runtime.
Robolectric is included as a compile-only dependency to overcome compilation errors due to missing Android APIs as they are many more
compile-time dependencies on the Android SDK than what is required for proper functioning at run-time. This makes development of the SDK
somewhat challenging as the compiler is unable to assist you on what Android functionality needs to be emulated before the library will
function correctly (instead the library will error at runtime usually with one of the ReflectiveOperationException subclasses.)
The best way to support more of the Firebase SDKs is via writing unit tests to test the required functionality at
runtime and assert the correct behavior.