Skip to content

Jinwenxin/AppInsights-Android

 
 

Repository files navigation

Application Insights for Android

This project provides an Android SDK for Application Insights. Application Insights is a service that allows developers to keep their applications available, performing, and succeeding. This module allows you to send telemetry of various kinds (events, traces, exceptions, etc.) to the Application Insights service where your data can be visualized in the Azure Portal.

Setup

Add the repository and compile dependency

Top-level build file

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://dl.bintray.com/appinsights-android/maven'
        }
    }
}

Per-module

dependencies {
    compile 'com.microsoft.azure:applicationinsights-android:+'
}

Configure the instrumentation key and add permissions

Please see the "Getting an Application Insights Instrumentation Key" section of the wiki for more information on acquiring a key.

AndroidManifest.xml

<manifest>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    <application>
        <meta-data
            android:name="com.microsoft.applicationinsights.instrumentationKey"
            android:value="${AI_INSTRUMENTATION_KEY}" />
    </application>
</manifest>

Optional: load instrumentation key from gradle

~/.gradle/gradle.properties

ai_instrumentation_key=<KEY_PLACEHOLDER>

Top-level build file

android {
    buildTypes {
        all {
            manifestPlaceholders = [AI_INSTRUMENTATION_KEY: ai_instrumentation_key]
        }
    }
}

Usage

import com.microsoft.applicationinsights.TelemetryClient;
public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //... other initialization code ...//

        TelemetryClient client = TelemetryClient.getInstance(this);
        client.trackTrace("example trace");
        client.trackEvent("example event");
        client.trackException(new Exception("example error"));
        client.trackMetric("example metric", 1);
    }
}

Automatic collection of life-cycle events

Note: this only works in Android SDK version 15 and up (Ice Cream Sandwich+)

Extend Application and register for life cycle callbacks

MyApplication.java

import com.microsoft.applicationinsights.ApplicationLifeCycleEventTracking;
public class MyApplication extends Application {

    @Override
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void onCreate() {
        super.onCreate();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            registerActivityLifecycleCallbacks(LifeCycleTracking.getInstance());
        }
    }
}

AndroidManifest.xml

<manifest>
    <application android:name="MyApplication"></application>
</manifest>

Documentation

http://microsoft.github.io/AppInsights-Android/

Contributing

Development environment

About

Microsoft Application Insights SDK for Android

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.8%
  • Shell 0.2%