Search code examples
androidperformancefirebase

how to integrate firebase performance monitoring in Android


Hi I need to monitor my methods and network calls performance in Android using Android Studio. Anyone help me out?


Solution

  • Go to project level build.gradle and add

    buildscript {
        repositories {
            jcenter()
            mavenLocal()
            google()
        }
    
        dependencies {
            //Your gradle version example classpath 'com.android.tools.build:gradle:3.1.3'
            //play services plugin example classpath 'com.google.gms:google-services:3.2.0'
            
            classpath 'com.google.firebase:firebase-plugins:1.1.5'
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    

    App level build.gradle add apply plugin: 'com.google.firebase.firebase-perf' below com.android.application

    In dependencies add firebase-perf

    android.applicationVariants.all {
        // Set this to false to disable Firebase Performance Monitoring at compile time
        FirebasePerformance {
            instrumentationEnabled true
        }
    }
    
    dependencies{
        implementation 'com.google.firebase:firebase-perf:16.0.0'
    }
    

    At the bottom add below line

    apply plugin: 'com.google.gms.google-services'
    

    You can monitor Network request, traces and method response time by adding below code in activity or class

    @AddTrace(name = "devcie_info", enabled = true)
    getdeviceInfo(){}
    

    For Traces

    Trace myTrace = FirebasePerformance.getInstance().newTrace("test_trace");
    myTrace.start();
    getdeviceInfo(){}
    myTrace.stop();
    

    Network Requests

    HttpMetric metric = FirebasePerformance.getInstance().newHttpMetric(url, FirebasePerformance.HttpMethod.GET);
    

    On response set response code and payload size

    metric.setRequestPayloadSize(response.length());
    metric.setHttpResponseCode(httpURLConnection.getResponseCode());
    metric.stop();
    

    For detailed information go to Link