Question

init Mixpanel Java in Android

  • 29 March 2019
  • 1 reply
  • 54 views

Badge

Hey!


I'm looking better way to use Mixpanel in my Android app, I set the public

public static MixpanelAPI mixpanel;

and

mixpanel = MixpanelAPI.getInstance(ctx, MIXPANEL_TOKEN);

get warning

Warning:(36, 12) Do not place Android context classes in static fields (static reference to `MixpanelAPI` which has field `mContext` pointing to `Context`); this is a memory leak (and also breaks Instant Run)


What is the best practice init mixpanel?



1 reply

Userlevel 1
Badge

Hey @AntonV, the syntax to initialize your library in Android is

public static final String MIXPANEL_TOKEN = "YOUR_TOKEN";

// Initialize the library with your
// Mixpanel project token, MIXPANEL_TOKEN, and a reference
// to your application context.
MixpanelAPI mixpanel =
    MixpanelAPI.getInstance(context, MIXPANEL_TOKEN);

as stated in their Android docs. This might help get rid of the error. Let me know if it works! If not, can you share more of your code?


Reply