Vai alla console

Add Firebase to your C++ projectplat_iosplat_androidplat_cpp

Power up your C++ games with our Firebase C++ SDKs which provide a C++ interface on top of Firebase for iOS and for Android.

Access Firebase entirely from your C++ code, without having to write any platform-native code. The Firebase SDK also translates many language-specific idioms used by Firebase into an interface more familiar to C++ developers.

Find out more information about powering up your games with Firebase at our Firebase games page.

Have you already added Firebase to your C++ project? Make sure that you're using the latest version of the Firebase C++ SDK.

Step 1: Set up your environment

  • Obtain the Android SDK.

  • Install your preferred editor or IDE, such as Android Studio, IntelliJ, or VS Code.

  • Open your C++ project in your preferred editor or IDE.

  • Set up a device or emulator for running your app.

    • Emulators must use an emulator image with Google Play.

    • For some C++ libraries, Google Play services is required on the client device; review the list on this page.

  • Sign into Firebase using your Google account.

Step 2: Create a Firebase project

Before you can add Firebase to your C++ project, you need to create a Firebase project to connect to your C++ project. Visit Understand Firebase Projects to learn more about Firebase projects.

Step 3: Register your app with Firebase

You can register one or more apps to connect with your Firebase project.

Visit Understand Firebase Projects to learn more about best practices and considerations for adding apps to a Firebase project, including how to handle multiple build variants.

  1. In the center of the Firebase console's project overview page, click the Android icon to launch the setup workflow.

    If you've already added an app to your Firebase project, click Add app to display the platform options.

  2. Enter your app's application ID in the Android package name field.

    • An application ID is sometimes referred to as a package name.

    • Find this application ID in your module (app-level) Gradle file, usually app/build.gradle (example ID: com.yourcompany.yourproject).

  3. (Optional) Enter other app information as prompted by the setup workflow.

    The nickname is an internal, convenience identifier and is only visible to you in the Firebase console.

  4. Click Register app.

Step 4: Add the Firebase configuration file

  1. Click Download google-services.json to obtain your Firebase Android config file.

    • You can download your Firebase Android config file again at any time.

    • Make sure the config file is not appended with additional characters, like (2).

  2. Open your C++ project in an IDE, then add your config file to your project:

  3. (Gradle builds only) To enable Firebase services in your C++ project, add the google-services plugin to your top-level build.gradle file.

    1. Add rules to include the Google Services plugin. Check that you have Google’s Maven repository, as well.

        buildscript {
      
          repositories {
            // Check that you have the following line (if not, add it):
            google()  // Google's Maven repository
          }
      
          dependencies {
            // ...
      
            // Add the following line:
            classpath 'com.google.gms:google-services:4.3.2'  // Google Services plugin
          }
        }
      
        allprojects {
          // ...
      
          repositories {
            // Check that you have the following line (if not, add it):
            google()  // Google's Maven repository
            // ...
          }
        }
      
    2. Add the following line to the bottom of the file.

        apply plugin: 'com.android.application'
      
        android {
          // ...
        }
      
        // Add the following line to the bottom of the file:
        apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin
      
  4. You're done with set up tasks in the Firebase console. Continue to Add Firebase C++ SDKs below.

Step 5: Add Firebase C++ SDKs

The steps in this section are an example of how to add supported Firebase products to your Firebase C++ project.

  1. Download the Firebase C++ SDK, then unzip the SDK somewhere convenient.

    The Firebase C++ SDK is not platform-specific, but it does contain platform-specific libraries.

  2. Specify the location of the unzipped SDK in your project's gradle.properties file:

    systemProp.firebase_cpp_sdk.dir=full-path-to-SDK
    
  3. Add the following to your project’s settings.gradle file:

    def firebase_cpp_sdk_dir = System.getProperty('firebase_cpp_sdk.dir')
    
    gradle.ext.firebase_cpp_sdk_dir = "$firebase_cpp_sdk_dir"
    includeBuild "$firebase_cpp_sdk_dir"
    
  4. Add the following to your project-level build.gradle file:

    android.defaultConfig.externalNativeBuild.cmake {
      arguments "-DFIREBASE_CPP_SDK_DIR=$gradle.firebase_cpp_sdk_dir"
    }
    
    apply from: "$gradle.firebase_cpp_sdk_dir/Android/firebase_dependencies.gradle"
    firebaseCpp.dependencies {
      analytics
    }
    
  5. To your project's CMakeLists.txt file, add the following content, including the libraries for the Firebase products that you want to use in your app.

    Analytics enabled

    # Add Firebase libraries to the target using the function from the SDK.
    add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)
    
    # The Firebase C++ library `firebase_app` is required,
    # and it must always be listed last.
    
    # Add the Firebase SDKs for the products you want to use in your app
    # For example, to use Analytics, Firebase Authentication, and Firebase Realtime Database
    set(firebase_libs firebase_analytics firebase_auth firebase_database firebase_app)
    target_link_libraries(${target_name} "${firebase_libs}")
    

    Analytics not enabled

    # Add Firebase libraries to the target using the function from the SDK.
    add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)
    
    # The Firebase C++ library `firebase_app` is required,
    # and it must always be listed last.
    
    # Add the Firebase SDKs for the products you want to use in your app
    # For example, to use Firebase Authentication and Firebase Realtime Database
    set(firebase_libs firebase_auth firebase_database firebase_app)
    target_link_libraries(${target_name} "${firebase_libs}")
    
  6. Sync your app to ensure that all dependencies have the necessary versions.

  7. If you added Analytics, run your app to send verification to Firebase that you've successfully integrated Firebase. Otherwise, you can skip the verification step.

    Your device logs will display the Firebase verification that initialization is complete. If you ran your app on an emulator that has network access, the Firebase console notifies you that your app connection is complete.

You’re all set! Your C++ app is registered and configured to use Firebase services.

Available libraries

Learn more about the C++ Firebase libraries in the reference documentation and in our open-source SDK release on GitHub.

Available libraries for Android (using CMake)

Note that C++ libraries for iOS are listed on the iOS version of this setup page.

Firebase product Library references (using CMake)
AdMob firebase_admob
(required) firebase_analytics
(required) firebase_app
Analytics firebase_analytics
(required) firebase_app
Authentication firebase_auth
(required) firebase_app
Cloud Functions firebase_functions
(required) firebase_app
Cloud Messaging firebase_messaging
(recommended) firebase_analytics
(required) firebase_app
Cloud Storage firebase_storage
(required) firebase_app
Dynamic Links firebase_dynamic_links
(recommended) firebase_analytics
(required) firebase_app
Realtime Database firebase_database
(required) firebase_app
Remote Config firebase_remote_config
(recommended) firebase_analytics
(required) firebase_app

Additional information for mobile setup

Get NDK crash reports

Firebase Crashlytics supports crash reporting for apps using Android native libraries. To learn more, see Get Android NDK crash reports.

Custom build systems

Firebase provides the script generate_xml_from_google_services_json.py to convert google-services.json to .xml resources that you can include in your project. This script applies the same transformation that the Google Play services Gradle plugin performs when building Android applications.

If you don't build using Gradle (for example, you use ndk-build, makefiles, Visual Studio, etc.), you can use this script to automate the generation of Android 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'll need to add the files in libs/android/*.pro corresponding to the Firebase C++ libraries that you're using in your ProGuard configuration.

For example, with Gradle, if you're using Google Analytics, your build.gradle file would look like:

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

Google Play services requirement

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

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

The following table lists whether Google Play services is required on a client device for each supported Firebase product.

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

AdMob and Google Play services

Most versions of the Google Mobile Ads SDK for Android can work properly without Google Play services on the client device. However, if you're 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 is required.

AdMob initialization will only return kInitResultFailedMissingDependency when both the following are true:

  • Google Play services is unavailable on the client device.
  • You're using com.google.android.gms:play-services-ads-lite.

Set up a desktop workflow (beta)

When you're creating a game, it's often much easier to test your game on desktop platforms first, then deploy and test on mobile devices later in development. To support this workflow, we provide a subset of the Firebase C++ SDKs which can run on Windows, OS X, Linux, and from within the C++ editor.

  1. For desktop workflows, you need to complete the following:

  2. Create a desktop version of the Firebase configuration file:

    • If you added the Android google-services.json file — When you run your app, Firebase locates this mobile file, then automatically generates a desktop Firebase config file (google-services-desktop.json).

    • If you added the iOS GoogleService-Info.plist file — Before you run your app, you need to convert this mobile file to a desktop Firebase config file. To convert the file, run the following command from the same directory as your GoogleService-Info.plist file:

      generate_xml_from_google_services_json.py --plist -i GoogleService-Info.plist

    This desktop config file contains the C++ project ID that you entered in the Firebase console setup workflow. Visit Understand Firebase Projects to learn more about config files.

  3. Add Firebase SDKs to your C++ project.

    The steps below serve as an example of how to add any supported Firebase product to your C++ project. In this example, we walk through adding Firebase Authentication and Firebase Realtime Database.

    1. Set your FIREBASE_CPP_SDK_DIR environment variable to the location of the unzipped Firebase C++ SDK.

    2. To your project's CMakeLists.txt file, add the following content, including the libraries for the Firebase products that you want to use. For example, to use Firebase Authentication and Firebase Realtime Database:

      # Add Firebase libraries to the target using the function from the SDK.
      add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)
      
      # The Firebase C++ library `firebase_app` is required,
      # and it must always be listed last.
      
      # Add the Firebase SDKs for the products you want to use in your app
      # For example, to use Firebase Authentication and Firebase Realtime Database
      set(firebase_libs firebase_auth firebase_database firebase_app)
      target_link_libraries(${target_name} "${firebase_libs}")
      
  4. Run your C++ app.

Available libraries (desktop)

The Firebase C++ SDK includes desktop workflow support for a subset of features, enabling certain parts of Firebase to be used in standalone desktop builds on Windows, OS X, and Linux.

Firebase product Library references (using CMake)
Authentication firebase_auth
(required) firebase_app
Cloud Functions firebase_functions
(required) firebase_app
Cloud Storage firebase_storage
(required) firebase_app
Realtime Database firebase_database
(required) firebase_app
Remote Config firebase_remote_config
(required) firebase_app

Firebase provides the remaining desktop libraries as stub (non-functional) implementations for convenience when building for Windows, OS X, and Linux. Therefore, you don't need to conditionally compile code to target the desktop.

Realtime Database desktop

The Realtime Database Desktop SDK uses REST to access your database, so you must declare the indexes you use with Query::OrderByChild() on desktop or your listeners will fail.

Additional information for desktop setup

Windows libraries

For Windows, library versions are provided based on the following:

  • Build platform: 32-bit (x86) vs 64-bit (x64) mode
  • Windows runtime environment: Multithreaded / MT vs Multithreaded DLL /MD
  • Target: Release vs Debug

Note that the following libraries were tested using Visual Studio 2015 and 2017.

When building C++ desktop apps on Windows, link the following Windows SDK libraries to your project. Consult your compiler documentation for more information.

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

OS X libraries

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

Note that the OS X libraries have been tested using Xcode 10.1.0.

When building C++ desktop apps on OS X, link the following to your project:

  • pthread system library
  • CoreFoundation OS X system framework
  • Foundation OS X system framework
  • Security OS X system framework
  • GSS OS X system framework
  • Kerberos OS X system framework

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.

Note that the Linux libraries were tested using GCC 4.8.0, GCC 7.2.0, and Clang 5.0 on Ubuntu.

When building C++ desktop apps on Linux, link the pthread system library to your project. Consult your compiler documentation for more information. If you're building with GCC 5 or later, define -D_GLIBCXX_USE_CXX11_ABI=0.

Next steps