Initialize the SDK

The SDK should should be started within the onCreate() method of your app’s Application class to ensure session management. If you don’t already have an Application class, create it and then specify its fully-qualified name in the <application> tag of your app’s AndroidManifest.xml. Read below and adjust the code samples below to fit your app’s requirements.

Your Application.onCreate() should only initialize the SDK. You should not attempt to log any events, as Android may instantiate your Application class for a number of reasons, in the background, even while the user isn’t using their device.

Create an MParticleOptions object and Initialize the SDK

You must initialize the SDK with an MParticleOptions object. At minimum you must supply your mParticle workspace key and secret, through the credentials() method. You may also wish to pass a custom identity request - see Identity for more information.

Supply your MParticleOptions object to the mParticle start API to initialize the SDK:

MParticleOptions options = MParticleOptions.builder(this)
        .credentials("REPLACE ME WITH KEY","REPLACE ME WITH SECRET")
        .logLevel(MParticle.LogLevel.VERBOSE)
        .build();
MParticle.start(options);
MParticleOptions.builder(this).run {
    credentials("REPLACE ME WITH KEY", "REPLACE ME WITH SECRET")
    logLevel(MParticle.LogLevel.VERBOSE)
    build()
}.let {
    MParticle.start(it)
}

Development and Production Environments

The SDK will detect whether your app is running in a Development environment or a Production environment, or can bet set to an environment manually. In development mode the SDK will print informative log messages and will upload data at a higher rate to mParticle servers to allow for easier debugging.

You can set the environment in your mParticleOptions object. Note that trying to set the environment to Development after the SDK has detected it to be Production will result in no action - this is to avoid the situation of setting the SDK environment to development after the app has been deployed to the App Store or Google Play Store.

In order to see the stream of events sent from your app to mParticle servers, log into the mParticle platform, navigate to Live Stream, select your app, and filter on the appropriate devices.

MParticleOptions options = MParticleOptions.builder(this)
            .credentials("foo-key", "foo-secret")
            .installType(MParticle.InstallType.KnownUpgrade)
            .environment(MParticle.Environment.Production)
            .build();
MParticle.start(options);
MParticleOptions.builder(this).run {
    credentials("foo-key", "foo-secret")
    installType(MParticle.InstallType.KnownUpgrade)
    environment(MParticle.Environment.Production)
    build()
}.let { 
    MParticle.start(it)
}
Environment Description
AutoDetect Default. The SDK will detect environment automatically.
Development The running environment of the SDK is development
Production The running environment of the SDK is production

Production Logcat Logging

Logging is disabled when the SDK is in production mode. You can manually enable it using the adb setprop command with the “mParticle” tag.

adb shell setprop log.tag.mParticle VERBOSE