Firebase Performance Monitoring for Android

Waiting for things to load is part of everyone’s mobile app experience.But it’s never a good experience for your user and how would you even know what the experience is? Your user is on a wide variety of devices, in a wide variety of networks, in a wide variety of locations all over the world.If you want to optimize the performance of your app, you metrics that tell you exactly what’s happening during the critical moments of your app’s use.You need that information to come directly from users.Now, You can get using Firebase Performance Monitoring.By integrating the SDK into your app, and without writing any code, your performance dashboard in the Firebase console will collect information about your app’s performance, as seen by your users.You’ll get data about your app’s startup time and details about its HTTP transactions.Using the provided API, you can instrument your app to measure those critical moments that you want to understand and improve.Then, in the dashboard, you can break down the data by Country, Version, Device, OS, Radio, Carrier, MIME type.

Install Firebase SDK in APP

This guide shows you how to use Firebase Performance Monitoring with your app.

1 Prerequisites

You need a few things set up in your environment:

  • A device running Android 4.0 +, and Google Play services 11.0.4 +
  • The Google Play services SDK from the Google Repository, available in the Android SDK Manager
  • The latest version of Android Studio 2.2+

2 Add Firebase to your Android project

Add Firebase to your app from Android Studio using the Firebase Assistant.To open the Firebase Assistant in Android Studio:

  1. Click Tools > Firebase to open the Assistant window.
  2. Click to expand one of the listed features (for example, Analytics), then click the provided tutorial link (for example, Log an Analytics event).
  3. Click the Connect to Firebase button to connect to Firebase and add the necessary code to your app.

Firebase Performance Monitor

3 Add Performance Monitoring to your app

  • Add the following dependencies project-level build.gradle :

Add the following dependencies app-level build.gradle :

  1. Below apply plugin: '', add the following line:
  2. Add the following to the dependencies section:
  3. If your app uses other Firebase SDKs, you should also change the version to 11.0.4 for those SDKs.


Recompile your app. Automatic traces and HTTP/S network requests are now monitored.

If you just install the SDK, you still get a few out-of-the box traces, the most important one being app start.This is the time between the app code start loading until the app is responsive to the user.Firebase also has a trace that monitors the foreground session so anytime the app is available to the user.and we do the same for the background as well.

Performance issues that can impact you as a developer falls under one of two buckets.The first one has to do with your app code performance.

So this is a thing like what kind of delays your user see as they interact with the app, or how much frame drops they see in the animation to monitor that Firebase built feature called Traces.

The other category of issues is about network activity between your app and the backends that it uses for that Firebase monitor network requests.


The easiest way to define a trace is its performance report between two points in your app.Imagine you have a record button and a stop button, and in between those two points you just pick up metrics, So these two points can be anything in your app, anything that you care about and you think is worth monitoring.So it could be something like the time between the user taps on one of the shoes until they see the full product details page.It could be as fine grained as a database fetch to get a piece of string and put it on the screen or it could be as long as the full checkout flow.

All you need to do is create a trace name or a trace object, give it a name, start it and stop it.That name you give it becomes your context.

Add the @AddTrace annotation to trace specific methods

You can add the @AddTrace annotation to methods in your app and provide a string to identify the resulting trace. This causes a trace to start at the beginning of this method, and to stop when the method completes. Traces created in this way do not have counters available.

Trace Metrics

In terms of metrics, by default you get the trace duration, so how much time took place between the start from the stop.So essentially, you can use it a time.But it’s actually more than that because you can attach custom metrics using counter API.which you see an example of here.So here think any example of countable events that are relevant to your performance how many times you called the local disk, how many times you called the GPS, how many times you dropped a frame, how many times you made a network call, and so on.


Any one of these events you can just give it a name, You can count it increment it and we’ll tally up those events and report them as custom metrics attached to your trace.

Network Monitor

An HTTP/S network request is a report that captures the time between when your app issues a request to a service endpoint and when the response from that endpoint is complete. For any endpoint that your app makes a request to, the SDK will capture several metrics:

  • Response time: Time between when the request is made and when the response is fully received
  • Payload size: Byte size of the network payload downloaded and uploaded by the app
  • Success rate: Percentage of successful responses compared to total responses (to measure network or server failures)


Disable the Firebase Performance Monitoring SDK

You can disable the Performance Monitoring SDK when building your app with the option to re-enable it at runtime, or build your app with Performance Monitoring enabled and then have the option to disable it at runtime using Firebase Remote Config. You can also completely deactivate Performance Monitoring, with no option to enable it at runtime.

Disable during your app build process

Disabling Performance Monitoring during your app build process could be useful is to avoid reporting performance data from a pre-release version of your app during app development and testing.

Add the following property to your app’s file to disable automatic traces and HTTP/S network request monitoring (but not custom traces) at build time:

Changing this property to true re-enables automatic traces and HTTP/S network request monitoring.

Allow your app to enable it at runtime, by adding a <meta-data> element to the <application> element of your app’s AndroidManifest.xml file, as follows:

To completely deactivate Performance Monitoring with no option to enable it at runtime, add a <meta-data>element to the <application> element of your app’s AndroidManifest.xml file, as follows:


Related Post

Phone Number Authentication with Firebase Auth


Memory Leak


Phone Number Authentication with Firebase Auth

Growing Globally with Phone Number identity

Identity is a really important part of growth.If people can’t or won’t login to your app, then you don’t get a user.You don’t have growth.

Why phone numbers?

Using phone number as an effective identity for growth.Sign-in is such an important part of your growth funnel.Over 90% of people who have issue login into an app is going to leave.

1.They’re global.

Every single person in the world who has a smartphone has a phone number that they have memorized.

2 Actually, in a lot of emerging markets, We’ve learned that people prefer to use their phone as their identity instead of an email address or another form of identification.

3 Phone numbers are higher quality.They give you higher quality users.Because they’re more expensive to mass produce, they’re naturally resistant to the kinds of abuse and fraud you might see more often in another type of identification.Most importantly very low-friction sign-in for apps.

Why is login so hard to use?

We know as developers that passwords don’t increase security as much as they frustration and friction.

As a developer, we know that login is harder than it needs to be dealing with login is hard.There are UX implications.There are security implications.

Firebase Phone Auth

Today’s apps need to be able to support high-quality,low-friction authentication and engage with them in new ways.That makes phone number auth really valuable.

A lot of the apps that actually don’t have phone number authentication today, bBuilding it in the first place can be a real struggle.The first thing you’ll need to do is integrate with an SMS provider, and get coverage with carriers to send SMS.Then,you’re going to want to expand your offering globally to support all the different areas that your user might come from, making sure you’re perfectly localized along the way.

After all of that, you need to build secure authentication.That means generating a one time code, verifying it from a user, minting cryptographically signed tokens, persisting session, writing your anti-abuse logic. Firebase launched its phone authentication APIs.

The first thing that Firebase allow is it enables basic sign-in and sign-up flows. On Android Firebase actually, allow a few and built in enhancements.In the previous post you understand about the SMS retriever API, which actually increases overall conversion from users, SO they don’t have to end up with the SMS app and parsing it.This is going to come built into firebase Android APIs.Finally, Firebase has a really new cool piece of functionality called instant verification on Android.

Firebase enables basic sign-in and sign-up flows.

The first thing that Firebase want to make sure enable your app to do is allow you to sign in and sign up your users.All a user would need to do is enter their phone number, and you can go ahead and pass it over to Firebase.

Firebase will validate the phone number, handle any normalization logic that needs to happen, if this is a new user, or a returning one, and go ahead and generate a one-time code associated with this request.

The new user will simply pass back the code into your application, and which you would just pass right back on to Firebase, and Firebase will verify it.Firebase will handle any of the complex logic, and deduplication, if you send multiple SMS by mistake or any of the other edge cases that might arise.The user will go ahead and enter their code.Firebase will validate it.Firebase will go ahead and mint cryptographically signed tokens, persist sessions the whole authentication.You won’t have to worry about how any of it.


Auto-retrieval works very similarly to we discussed in the previous post.It’s comes built in the user phone auth APIs.For auto retrieval, if the user goes ahead and click sign with the phone number.This time they can actually select their phone number if you integrate with hint selector.It’ll be auto-populated into the application.Firebase will validate it once more. Firebase will generate the code.But this time, when firebase actually sends the code in the SMS, Firebase will be able to pick it up from the background.Firebase auto-retrieve it directly, parse it out and populate it into the application.The best part of all of this is your user never actually had to leave your application, go to an SMS tab, wait for it and then copy and paste the code into your UI.They stayed in your application the entire time.It’s a really seamless experience for them overall.That’s all that you ever to do.All this powerful functionality is also really easy to use.


As a pre-requisite, ensure your application is configured for use with Firebase.

Then, add the FirebaseUI auth library dependency.

The second thing you need to do is just enable phone auth within your project in the Firebase console.


Enable Firebase Phone Auth

Now Firebase headless APIs don’t enforce any UI, so they give you programmatic access to our underlying infrastructure that you can use in any way that you choose.It’s really easy to use.On Android, in order to get all the awesome functionality the actual ability to generate and send codes, the auto-retrieval, even instant verification, comes down to just one single API call, and the implementation of the callbacks that you care about.

What’s about UI?

Building authentication UI can be pretty complex.You need to handle all sorts of edge cases across all the different platforms your application might support.You want to make sure that your login UI is actually high performance.That will give you good conversion rates.FirebaseUI wrote out all the code for different phone authentication flows that.


The first thing you’ll notice is Firebase UI integrates with hint selector directly.So I didn’t write any additional code for this.It’s able to pick up user’s phone number from the device, and I can just simply enter it directly into the app.

Now when you hit verify phone number, it’ll go ahead and send a code which will directly get right off the SMS.The code was sent to your device immediately written from the SIM.All I did was really tap my phone number and everything else was taken for me.The SMS was delivered.It was passed.It was taken.I never left the app.

Instant Verification

Instant verification works exactly like it sounds like.It allows you to verify the phone number without sending an SMS to the user, it appears instantly.Let’s How it works?

1.User Select Number

This time, When they enter their phone number, and you call APIs, Firebase is going to do the first check.That check will go ahead and see if firebase has verified this phone number on the device recently.If it has, it can actually instantly verify the phone number the next time around without needing to send any additional SMS.It thinks that this means there is no wait time, on SMS, or anything else, just pure simplicity verify.


Imagine that user coming back to the application, maybe after a week or two.This time user going to instant verification.When the user goes to the application, let’s sign in with a phone number again.It’ll pull up the phone number for user device just like last time.The user just going to do single tap log-in.Where I go ahead and select a phone number, It sees that I’d already been verified in the past and that It’s.No SMS was sent, nothing else.But instead, Firebase was able to just validate that we verified it, and logged them in directly into te application.


Related Post

SMS Retriever API

SMS Retriever API

Automatic SMS Verification with the Phone Selector and SMS Retriever API

Enable to support phone numbers for authentication, if you have an existing backend infrastructure that you are already using for verifying the phone numbers.

Using SMS Retriever API, you can perform SMS-based verification in your app automatically, without requiring the user to manually type verification codes, and without requiring any extra app permissions.

Most apps perform verification by sending an SMS to the user’s device with a one-time verification code that the application that verifies.So the app has to get the phone number of the device and then read the verification code from the SMS.

The Google Play Services SDK(10.2 and newer) offering you to enable read the phone number and the verification SMS automatically without requiring these extra permissions.

For reading the phone number, Android has the Phone Selector API.For verifying the phone number, Android has the SMS Retriever API.These APIs are part of the Google Play Service SDK.

1.Getting the phone number of the device

Don’t ask the user to enter these values.Manually inputting the phone number is painful.It adds a lot of friction for the user to provide a phone number to your app.It’s also error-prone.

Phone Selector API

Phone Selector API provides the Phone number to your app with a much better user experience and no extra permissions.Using this API, you can launch a dialogue, which shows the phone numbers on the device to the user.

Phone Number HintSelector

First, you create a hint request object and set the phone number identifier supported field to true.

Then, you get a pending intent from that hint request for the phone number selector dialogue.

Once the user selects the phone number, that phone number is returned to your app in the onActivityResult().

2.Reading the verification code automatically using SMS retriever API

SMS Retriever API provides you the message content to your app without requiring any extra permissions.The key part is that it provides you only the message targeted your app.You have to verification code in your SMS message and include app-specific hash.This app-specific hash is a static hash that you can just include in the SMS template without requiring many code changes.

SMS Retriever API

Start the SMS retriever

This makes it wait for one matching SMS, which includes the app-specific hash.

Once the SMS with the app-specific hash is received on the device, it is provided to your app via broadcast.

In your broadcast receiver, you can get the message content from the extras.Once you have the message content, you can extract the verification code, and verify the code just like you would normally do.

Register this BroadcastReceiver with the intent filter.

After starting the SMS retriever, you can just send the SMS with the verification code and the app-specific hash to the phone using any backend infrastructure of yours.

3.Construct a verification message

Construct the verification message that you will send to the user’s device. This message must:

  • Be no longer than 140 bytes
  • Begin with one of the following strings:
    • [#]
    • Two consecutive zero-width space characters (U+200B)
  • End with an 11-character hash string that identifies your app

Otherwise, the contents of the verification message can be whatever you choose. It is helpful to create a message from which you can easily extract the one-time code later on. For example, a valid verification message might look like the following:

Computing your app’s hash string

ou can get your app’s hash string with the AppSignatureHelper class from the SMS retriever sample app. However, if you use the helper class, be sure to remove it from your app after you get the hash string. Do not use hash strings dynamically computed on the client in your verification messages.


Related Post