Getting Started

The Android SDK can be incorporated into native Android apps running on smartphones and other platforms, including Amazon Fire TV and Alexa.

Core SDK

mParticle’s Android integration is powered by a “core” library, which supports mParticle’s server-side integrations and audience platform.

You can get the core SDK via Maven Central. Please follow the releases page on Github to stay up to date with the latest version.

dependencies {
    implementation 'com.mparticle:android-core:5+'
}

Kits

Several integrations require additional client-side add-on libraries called “kits.” Some kits embed other SDKs, others just contain a bit of additional functionality. Kits are designed to feel just like server-side integrations; you enable, disable, filter, sample, and otherwise tweak kits completely from the mParticle platform UI. The Core SDK will detect kits at runtime, but you need to add them as dependencies to your build:

dependencies {
    implementation (
        'com.mparticle:android-example-kit:5+',
        'com.mparticle:android-another-kit:5+',
    )
}

Kits are deployed as individual artifacts in Maven Central, and each has a dedicated repository if you’d like to view the source code. Review the table below to see if you need to include any kits:

Kit Maven Artifact
Adjust android-adjust-kit
Adobe android-adobe-kit
Appboy android-appboy-kit
AppsFlyer android-appsflyer-kit
Apptentive android-apptentive-kit
Apptimize android-apptimize-kit
Apsalar android-apsalar-kit
Apteligent android-apteligent-kit
Branch Metrics android-branch-kit
Button android-button-kit
comScore android-comscore-kit
Flurry android-flurry-kit
ForeSee android-foresee-kit
Iterable android-iterable-kit
Kahuna android-kahuna-kit
Kochava android-kochava-kit
Leanplum android-leanplum-kit
Localytics android-localytics-kit
Radar android-radar-kit
Reveal Mobile android-revealmobile-kit
Tune android-tune-kit
Urban Airship android-urbanairship-kit
Wootric android-wootric-kit
Skyhook android-skyhook-kit

Optional Dependencies

Google Play Services Ads

The Google Play Services Ads framework is necessary to collect the Android Advertisting ID. AAID collection is required by all attribution and audience integrations, and many other integrations. Include the -ads artifact, a subset of Google Play Services:

    implementation 'com.google.android.gms:play-services-ads:15.0.1'

Firebase Cloud Messaging

mParticle supports several marketing automation and push messaging integrations. These require that mParticle register for an instance id using the Firebase Cloud Messaging framework:

    implementation 'com.google.firebase:firebase-messaging:15.0.1'

Google Play Install Referrer

Single Receiver

In order for attribution, deep linking, and many other integrations to work properly, add the mParticle ReferrerReceiver to your manifest file within the <application> tag. The mParticle SDK will collect any campaign referral information and automatically forward it to kits (such as AppsFlyer, Kochava, and Adjust) and server-side integrations.

<receiver android:name="com.mparticle.ReferrerReceiver" android:exported="true">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER"/>
    </intent-filter>
</receiver>

Multiple Receivers

Google Play will only deliver the INSTALL_REFERRER Intent to a single BroadcastReceiver - you cannot have multiple receivers in your manifest. If you already have your own receiver, or otherwise have multiple receivers that require referral data, you must expose your own BroadcastReceiver in your manifest and then forward the received Intent to mParticle:

public class ExampleReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //process the Intent/send to other receivers as desired, and
        //send the Context and Intent into mParticle's BroadcastReceiver
        new com.mparticle.ReferrerReceiver().onReceive(context, intent);
    }
}

Instant Apps

As of version 5 mParticle SDK supports Android Instant Apps. If you add Instant App functionality to your existing app the mParticle SDK will continue to work as normal.

Upgrade to Version 5 of the SDK

Version 5 of the Android SDK contains breaking changes from version 4.x, including new methods to support mParticle’s IDSync features and the deprecation of older methods for managing users.

MParticleOptions object

It is now required to configure the SDK by supplying an MParticleOptions argument when you .start() the SDK. Previously it was possible to .start() the SDK without providing an options object. See Initialize the SDK for more.

Identity and User Attributes

Previously, current user identities and attributes were set at the level of the device and included with any outgoing uploads. This approach has been deprecated in favor of more explicit user management via the Identity API. You may include an Identity request when you intialize the SDK. If you do not specify a request, identify will be called with the most recently stored user identities. You should also update the current User Identity at appropriate points in your app lifecycle, such as Login and Logout.

UserAliasHandler

When transitioning from one user to another, usually as a previously anonymous user creates an account and becomes a known user, some Identity Strategies will alias the old anonymous profile to the new known one. The UserAliasHandler gives you the option to copy over attributes currently held in local storage to the new user profile.

The MParticleUser Object

The setUserIdentity() and setUserAttribute() methods are deprecated as at version 5. Instead, the current user, as determined by the Identity API is exposed via the getCurrentUser() method. The MParticleUser provides methods to check the MPID of the current user, modify the user’s Identity Record and get/set attributes.

First Initialize after Upgrade

The first time the upgraded SDK is initialized for an existing user, the SDK will automatically migrate identities and attributes currently in local storage. If you do not provide an identity request in your MParticleOptions object, the initial identity request will be created automatically based on any migrated identities.

Deeplinking / Attribution

Version 5 includes a new Attribution API. If you use any of our deeplinking partners, including Appsflyer, Button, Branch and Tune, you may need to update your code. See our Kits documentation for more.

Shopping Cart

The shopping cart is now maintained against the current user. See the eCommerce docs for details.

Upgrade Checklist

  • Create MParticleOptions object and pass it to start() in your Application’s .onCreate().
  • If you are implementing an eCommerce strategy, implement a UserAliasHandler.
  • Replace any instance of .setUserIdentity() with the appropriate Identity method.
  • Any instance of .setUserAttribute() must now be called on your MParticleUser.
  • Any methods accessing the cart must be called on the current user object.

Common Errors / Fixes

If you update your build.gradle to version 5 without making further changes, your code will not compile. See below for a list of common errors and fixes:

Errors:

error: no suitable method found for start(ExampleApplication,String,String)
method MParticle.start(Context) is not applicable
(actual and formal argument lists differ in length)
method MParticle.start(MParticleOptions) is not applicable
(actual and formal argument lists differ in length)

Fix:

// Old code:
// MParticle.start(this, "foo-api-key", "foo-api-secret");

// Replace with MParticleOptions object and new start() method
// See Initialize the SDK for more
MParticleOptions options = MParticleOptions.builder(this)
            .credentials("foo-api-key", "foo-api-secret")
            .identify(identifyRequest)
            .identifyTask(
                new BaseIdentityTask()
                        .addFailureListener(this)
                        .addSuccessListener(this)
                    )
            .build();
MParticle.start(options);

Error:

error: cannot find symbol method setUserIdentity(String,IdentityType)

Fix

// Old code:
// MParticle.getInstance().setUserIdentity("foo@example.com", MParticle.IdentityType.Email);

// Replace with an appropriate Identity request.
// See Identity for more.

IdentityApiRequest apiRequest = IdentityApiRequest.withEmptyUser()
        .email("foo@example.com")
        .build();

MParticle.getInstance().Identity().login(apiRequest)
          .addFailureListener(new TaskFailureListener() {
              @Override
              public void onFailure(IdentityHttpResponse identityHttpResponse) {
                  //device may be offline and request should be retried - see below.
              }
          })
          .addSuccessListener(new TaskSuccessListener() {
              @Override
              public void onSuccess(IdentityApiResult identityApiResult) {
                 //Continue with login, and you can also access the new/updated user:
                 MParticleUser user = identityApiResult.getUser()
              }
          });

Error:

error: cannot find symbol method setUserAttribute(String,String)

Fix:

// Old code:
// MParticle.getInstance().setUserAttribute("foo", "bar");

// Replace by calling setUserAttribute on the current user.
// See Identity for more.
MParticleUser currentUser = MParticle.getInstance().Identity().getCurrentUser();
currentUser.setUserAttribute("foo","bar");

Error:

error: cannot find symbol method logout()

Fix:

// Old code:
// MParticle.getInstance().logout();

// Replace with Logout Identity Request
// See Identity for more
IdentityApiRequest apiRequest = IdentityApiRequest.withEmptyUser()
        .build();

MParticle.getInstance().Identity().logout(apiRequest)
          .addFailureListener(new TaskFailureListener() {
              @Override
              public void onFailure(IdentityHttpResponse identityHttpResponse) {
                  //device may be offline and request should be retried - see below.
              }
          })
          .addSuccessListener(new TaskSuccessListener() {
              @Override
              public void onSuccess(IdentityApiResult identityApiResult) {
                 //Continue with logout, and you can also access the new/updated user:
                 MParticleUser user = identityApiResult.getUser()
              }
          });