About the Firebase C++ SDK
The Firebase C++ SDK provides a C++ interface on top of Firebase for iOS and Android.
It allows you to access Firebase entirely from your C++ code, without requiring you to write any Java code (on Android) or Objective-C/Swift code (on iOS), and translates many language-specific idioms used by Firebase into an interface more familiar to C++ developers.
Setup for iOS
Prerequisites
- Xcode 7.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.
- Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
- 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.
- 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.
- At the end, you'll download a
GoogleService-Info.plist
file. You can download this file again at any time. - If you haven't done so already, copy this into your Xcode project root.
Add the Firebase C++ SDK to your app
-
Download the Firebase C++ SDK. Extract the files somewhere convenient.
-
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!
-
Build your app. The Firebase code should compile and link correctly now. You're done!
Dependencies
Various 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' |
Analytics |
firebase.framework firebase_analytics.framework pod 'Firebase/Core' |
AdMob |
firebase.framework firebase_admob.framework pod 'Firebase/Core' pod 'Firebase/AdMob' |
Cloud Messaging |
firebase.framework firebase_messaging.framework pod 'Firebase/Core' pod 'Firebase/Messaging' |
Authentication |
firebase.framework firebase_auth.framework pod 'Firebase/Core' pod 'Firebase/Auth' |
Invites & Dynamic Links |
firebase.framework firebase_invites.framework pod 'Firebase/Core' pod 'Firebase/Invites' |
Remote Config |
firebase.framework firebase_remote_config.framework pod 'Firebase/Core' pod 'Firebase/RemoteConfig' |
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 and Cloud Messaging libraries need to attach
handlers to the application delegate using method swizzling. If you are using
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
- Android NDK version 10d or later.
- The gcc compiler.
Set up your app in Firebase console
To add Firebase to your app you'll need a Firebase project and a Firebase configuration file for your app.
- Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
- 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.
- 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.
- At the end, you'll download a
google-services.json
file. You can download this file again at any time. - If you haven't done so already, copy this into your project's module folder,
typically
app/
.
Add the Firebase C++ SDK to your app
-
Download the Firebase C++ SDK. Extract the files somewhere convenient.
-
Add the location of the Firebase C++ SDK to your
gradle.properties
file:systemProp.firebase_cpp_sdk.dir=~/your_local_firebase_sdk_dir
-
Add the location of the Android NDK to your
local.properties
file:ndk.dir=~/your_local_ndk_dir
-
Add any dependencies to your
app/build.gradle
file. (See the dependencies section for details.) -
You should now be able to build your project with Firebase support by running
build.gradle
from Android Studio. You're done!
Dependencies
Various Firebase features have different dependencies. Include all of the
needed dependencies to the dependencies section of your app/build.gradle
file:
Analytics |
libanalytics.a libapp.a compile 'com.google.firebase:firebase-analytics:9.6.1' compile 'com.google.android.gms:play-services-base:9.6.1' |
AdMob |
libadmob.a libapp.a compile 'com.google.firebase:firebase-ads:9.6.1' |
Cloud Messaging |
libmessaging.a libapp.a compile 'com.google.firebase:firebase-messaging:9.6.1' compile 'com.google.android.gms:play-services-base:9.6.1' |
Authentication |
libauth.a libapp.a compile 'com.google.firebase:firebase-auth:9.6.1' compile 'com.google.android.gms:play-services-base:9.6.1' |
Invites/Dynamic Links |
libinvites.a libapp.a compile 'com.google.firebase:firebase-invites:9.6.1' compile 'com.google.android.gms:play-services-base:9.6.1' |
Remote Config |
libremote_config.a libapp.a compile 'com.google.firebase:firebase-config:9.6.1' compile 'com.google.android.gms:play-services-base:9.6.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 |
Invites/Dynamic Links | Required |
Remote Config | 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
.