Adding Firebase to your C++ Projectplat_iosplat_androidplat_cpp

About the Firebase C++ SDK

The Firebase C++ SDK provides a C++ interface on top of Firebase for iOS and Android.

Access Firebase entirely from your C++ code, without having to write any Java code (on Android) or Objective-C/Swift code (on iOS). The SDK also translates many language-specific idioms used by Firebase into an interface more familiar to C++ developers. To show how easy it is to integrate Firebase into your C++ game we made a sample app, Zooshi, that you can download from GitHub.

Download the SDK

Zooshi (GitHub)


Find out more about how you can power up your games at our Firebase games page.

Features

The following features are supported by the Firebase C++ SDK on iOS and Android:

Feature Library
App (base library) libfirebase_app.a
AdMob libfirebase_admob.a
Analytics libfirebase_analytics.a
Authentication libfirebase_auth.a
Realtime Database libfirebase_database.a
Dynamic Links libfirebase_dynamic_links.a
Cloud Functions libfirebase_functions.a
Invites libinvites.a
Cloud Messaging libfirebase_messaging.a
Remote Config libfirebase_remote_config.a
Cloud Storage libfirebase_storage.a

The Firebase C++ SDK also includes desktop workflow support for a subset of features, enabling certain parts of Firebase on Windows, OS X, and Linux. (Beta)

Setup for iOS

Prerequisites

  • Xcode 8.0 or later.
  • For Cloud Messaging:
    • A physical iOS device
    • APNs certificate with Push Notifications enabled

Set up your app in Firebase console

It's time to add Firebase to your app. To do this you'll need a Firebase project and a Firebase configuration file for your app.

To create a Firebase project:

  1. Create a Firebase project in the Firebase console, if you don't already have one. Click Add project. If you already have an existing Google project associated with your mobile app, select it from the Project name drop down menu. Otherwise, enter a project name to create a new project.
  2. Optional: Edit your Project ID. Your project is given a unique ID automatically, and it's used in publicly visible Firebase features such as database URLs and your Firebase Hosting subdomain. You can change it now if you want to use a specific subdomain.
  3. Follow the remaining setup steps and click Create project (or Add Firebase if you're using an existing project) to begin provisioning resources for your project. This typically takes a few minutes. When the process completes, you'll be taken to the project overview.

Now that you have a project, you can add your iOS app to it:

  1. Click Add Firebase to your iOS app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.
  2. When prompted, enter your app's bundle ID. It's important to enter the bundle ID your app is using; this can only be set when you add an app to your Firebase project.
  3. During the process, you'll download a GoogleService-Info.plist file. You can download this file again at any time.
  4. After you add the initialization code, run your app to send verification to the Firebase console that you've successfully installed Firebase.

Add the Firebase C++ SDK to your app

  1. Download the Firebase C++ SDK. Extract the files somewhere convenient.

  2. Add the frameworks downloaded in the SDK to your Xcode project. Note that you only need to include the base Firebase framework, along with the frameworks that correspond to the services you want to use (refer to the dependencies section below). The easiest way to do this is usually just to drag them from a Finder window directly onto the project navigator pane in Xcode (left-most pane, by default - click the file icon at the top left). Make sure you add them to your project, and not to the Pods project!

  3. Build your app. The Firebase code should compile and link correctly now. You're done!

Dependencies

Firebase features have different dependencies. Be sure to include all of the needed dependencies to your project and Podfile.

Firebase
firebase.framework
pod 'Firebase/Core', '5.1.0'
Analytics
firebase.framework
firebase_analytics.framework
pod 'Firebase/Core', '5.1.0'
AdMob
firebase.framework
firebase_admob.framework
pod 'Firebase/Core', '5.1.0'
pod 'Firebase/AdMob', '5.1.0'
Cloud Messaging
firebase.framework
firebase_messaging.framework
pod 'Firebase/Core', '5.1.0'
pod 'Firebase/Messaging', '5.1.0'
Authentication
firebase.framework
firebase_auth.framework
pod 'Firebase/Core', '5.1.0'
pod 'Firebase/Auth', '5.1.0'
Realtime Database
firebase.framework
firebase_database.framework
pod 'Firebase/Core', '5.1.0'
pod 'Firebase/Database', '5.1.0'
Cloud Functions
firebase.framework
firebase_functions.framework
pod 'Firebase/Core', '5.1.0'
pod 'Firebase/Functions', '5.1.0'
Dynamic Links
firebase.framework
firebase_dynamic_links.framework
pod 'Firebase/Core', '5.1.0'
pod 'Firebase/DynamicLinks', '5.1.0'
Invites
firebase.framework
firebase_invites.framework
pod 'Firebase/Core', '5.1.0'
pod 'Firebase/Invites', '5.1.0'
Remote Config
firebase.framework
firebase_remote_config.framework
pod 'Firebase/Core', '5.1.0'
pod 'Firebase/RemoteConfig', '5.1.0'
Storage
firebase.framework
firebase_storage.framework
pod 'Firebase/Core', '5.1.0'
pod 'Firebase/Storage', '5.1.0'

Method swizzling

On iOS, some application events (such as opening URLs and receiving notifications) require your application delegate to implement specific methods. For example, receiving a notification may require your application delegate to implement application:didReceiveRemoteNotification:. Because each iOS application has its own app delegate, Firebase uses method swizzling, which allows the replacement of one method with another, to attach its own handlers in addition to any you may have implemented.

The Firebase Invites, Dynamic Links, and Cloud Messaging libraries need to attach handlers to the application delegate using method swizzling. If you are using any of these libraries, at load time, Firebase will identify your AppDelegate class and swizzle the required methods onto it, chaining a call back to your existing method implementation.

Setup for Android

Prerequisites

Set up your app in Firebase console

It's time to add Firebase to your app. To do this you'll need a Firebase project and a Firebase configuration file for your app.

To create a Firebase project:

  1. Create a Firebase project in the Firebase console, if you don't already have one. Click Add project. If you already have an existing Google project associated with your mobile app, select it from the Project name drop down menu. Otherwise, enter a project name to create a new project.
  2. Optional: Edit your Project ID. Your project is given a unique ID automatically, and it's used in publicly visible Firebase features such as database URLs and your Firebase Hosting subdomain. You can change it now if you want to use a specific subdomain.
  3. Follow the remaining setup steps and click Create project (or Add Firebase if you're using an existing project) to begin provisioning resources for your project. This typically takes a few minutes. When the process completes, you'll be taken to the project overview.

Now that you have a project, you can add your Android app to it:

  1. Click Add Firebase to your Android app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.
  2. When prompted, enter your app's package name. It's important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.
  3. During the process, you'll download a google-services.json file. You can download this file again at any time.
  4. After you add the initialization code, run your app to send verification to the Firebase console that you've successfully installed Firebase.

Add the Firebase C++ SDK to your app

  1. Download the Firebase C++ SDK. Extract the files somewhere convenient.

  2. Add the location of the Firebase C++ SDK to your gradle.properties file:

    systemProp.firebase_cpp_sdk.dir=~/your_local_firebase_sdk_dir
    
  3. Add the location of the Android NDK to your local.properties file:

    ndk.dir=~/your_local_ndk_dir
    
  4. Add any dependencies to your app/build.gradle file. (See the dependencies section for details.)

  5. You should now be able to build your project with Firebase support by running build.gradle from Android Studio. You're done!

Dependencies

Firebase features have different dependencies. Include all of the needed dependencies to the dependencies section of your app/build.gradle file.

Analytics
libfirebase_analytics.a
libfirebase_app.a
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-analytics:16.0.1'
implementation 'com.google.android.gms:play-services-base:15.0.1'
AdMob
libfirebase_admob.a
libfirebase_app.a
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-ads:15.0.1'
Cloud Messaging
libfirebase_messaging.a
libfirebase_app.a
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-messaging:17.0.0'
implementation 'com.google.android.gms:play-services-base:15.0.1'
Authentication
libfirebase_auth.a
libfirebase_app.a
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.2'
implementation 'com.google.android.gms:play-services-base:15.0.1'
Realtime Database
libfirebase_database.a
libfirebase_app.a
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.android.gms:play-services-base:15.0.1'
Cloud Functions
libfirebase_functions.a
libfirebase_app.a
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-functions:16.0.1'
implementation 'com.google.android.gms:play-services-base:15.0.1'
Dynamic Links
libfirebase_dynamic_links.a
libfirebase_app.a
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-invites:16.0.0'
implementation 'com.google.android.gms:play-services-base:15.0.1'
Invites
libfirebase_invites.a
libfirebase_app.a
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-invites:16.0.0'
implementation 'com.google.android.gms:play-services-base:15.0.1'
Remote Config
libfirebase_remote_config.a
libfirebase_app.a
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-config:16.0.0'
implementation 'com.google.android.gms:play-services-base:15.0.1'
Storage
libfirebase_storage.a
libfirebase_app.a
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.google.android.gms:play-services-base:15.0.1'

Additional Information

Custom Build Systems

We currently provide generate_xml_from_google_services_json.py to convert google-services.json to .xml resources to be included in an application. This script applies the same transformation that the Google Play Services Gradle plug-in performs when building Android applications. Users who don't use Gradle (e.g ndk-build, makefiles, Visual Studio etc.) can use this script to automate the generation of string resources.

ProGuard

Many Android build systems use ProGuard for builds in Release mode to shrink application sizes and protect Java source code. If you use ProGuard, you will need to add the files in libs/android/*.pro corresponding to the Firebase C++ libraries you are using to your ProGuard configuration.

For example, with Gradle, build.gradle would contain:

android {
  [...other stuff...]
  buildTypes {
    release {
      minifyEnabled true
      proguardFile getDefaultProguardFile('your-project-proguard-config.txt')
      proguardFile file(project.ext.firebase_cpp_sdk_dir + "/libs/android/app.pro")
      proguardFile file(project.ext.firebase_cpp_sdk_dir + "/libs/android/analytics.pro")
      [...and so on, for each Firebase C++ library you are using.]
    }
  }
}

Requiring Google Play services

Many Firebase C++ libraries require Google Play services on the user's Android device. If a Firebase C++ library returns kInitResultFailedMissingDependency on initialization, it means Google Play services is not available on the device (it needs to be updated, reactivated, permissions fixed, etc.) and that Firebase library cannot be used until the situation is corrected.

You can find out why Google Play services is unavailable (and try to fix it) by using the functions in google_play_services/availability.h.

Firebase C++ Library Google Play services required?
Analytics Not required
AdMob Not required (usually)
Cloud Messaging Required
Authentication Required
Dynamic Links Required
Invites Required
Remote Config Required
Realtime Database Required
Cloud Functions Required
Storage Required

A note on AdMob and Google Play services

Most versions of the Google Mobile Ads SDK for Android can work properly without Google Play services. However, if you are using the com.google.android.gms:play-services-ads-lite dependency instead of the standard com.google.firebase:firebase-ads dependency listed above, Google Play services will be required in your specific case.

AdMob initialization will only return kInitResultFailedMissingDependency when Google Play services is unavailable and you are using com.google.android.gms:play-services-ads-lite.

Desktop workflow implementations (Beta feature)

When you are creating an app, it's often much easier to test your app on desktop platforms first, then deploy and test on device later on in development. To aid with this workflow, we provide a subset of the Firebase C++ SDK to support desktop platforms:

Desktop Feature Library
core Firebase library libfirebase_app.a
Authentication libfirebase_auth.a
Realtime Database libfirebase_database.a
Cloud Functions libfirebase_functions.a
Remote Config libfirebase_remote_config.a
Cloud Storage libfirebase_storage.a

The remaining desktop libraries are stub (non-functional) implementations, provided for convenience when building for Windows, OS X, and Linux, so that you don't need to conditionally compile code when also targeting the desktop.

Desktop project setup

To use desktop workflow support, you must have an Android or iOS project set up in Firebase console.

If you have an Android project, you can simply use the google-services.json file on desktop.

If you have an iOS project and don't wish to create an Android project, you can use the included Python script generate_xml_from_google_services_json.py --plist to convert your GoogleService-Info.plist file into a google-services-desktop.json file.

By default, when your app initializes, Firebase will look for a file named google-services.json or google-services-desktop.json in the current directory. Ensure that one of these files is present, or call AppOptions::LoadFromJsonConfig() before initializing Firebase to specify your JSON configuration data directly.

Windows libraries

For Windows, library versions are provided depending on whether your project is building in 32-bit (x86) or 64-bit (x64) mode, which Windows runtime environment you are using (Multithreaded /MT or Multithreaded DLL /MD), and whether you are targeting Release or Debug.

The provided libraries have been tested using Visual Studio 2015 and 2017. When building C++ desktop apps on Windows, you will need to link the following Windows SDK libraries (consult your compiler documentation for more information):

Firebase C++ Library Windows SDK library dependencies
Authentication advapi32, ws2_32, crypt32
Realtime Database advapi32, ws2_32, crypt32, iphlpapi, psapi, userenv
Cloud Functions advapi32, ws2_32, crypt32
Remote Config advapi32, ws2_32, crypt32
Storage advapi32, ws2_32, crypt32

OS X libraries

For OS X (Darwin), library versions are only provided for the 64-bit (x86_64) platform. Frameworks are also provided for your convenience.

The provided libraries have been tested using Xcode 8.3.3 and 9.2. When building C++ desktop apps on OS X, you will need to link the pthread system library, as well as the CoreFoundation and Security OS X system frameworks (consult your compiler documentation for more information).

Linux libraries

For Linux, library versions are provided for 32-bit (i386) and 64-bit (x86_64) platforms.

The provided libraries have been tested using GCC 4.8.0, GCC 7.2.0, and Clang 5.0 on Ubuntu. When building C++ desktop apps on Linux, you will need to link the pthread system library (consult your compiler documentation for more information).

Note on Firebase C++ desktop support

Firebase C++ SDK desktop support is a Beta feature, and is intended for workflow use only during the development of your app, not for publicly shipping code.

Send feedback about...

Need help? Visit our support page.