Adding an Application Class to Your Android Application

Detailed steps to add an Application class to initialize Mars Android SDK

We recommend using a global Application class to initialize our SDK. If you don't have an Application class for your project follow the steps

  • Create a class that extends Application.

JAVA
JAVA
import android.app.Application;
public class MainApplication extends Application {
private static MainApplication instance;
private static MarsClient marsClient;
private static final String writeKey = "API-KEY";
@Override
public void onCreate() {
super.onCreate();
// initialize Mars SDK Here
marsClient = MarsClient.getInstance(
this,
"MARS_SDK_API_KEY",
new MarsConfig.Builder()
.withLogLevel(MarsLogger.MarsLogLevel.DEBUG)
.withTrackLifecycleEvents(true)
.withRecordScreenViews(true)
.build()
);
instance = this;
MarsClient.with(this).onIntegrationReady(writeKey, new MarsClient.Callback() {
@Override
public void onReady(Object instance) {
Log.i("Status", "Mars Integration is ready");
}
});
MarsClient.setSingletonInstance(marsClient);
}
public static MarsClient getMarsClient() {
return marsClient;
}
public static MainApplication getInstance() {
return instance;
}
}
  • Open AndroidManifest.xml file of your app and locate <application> tag.

  • Add an attribute android:name and set it to your new application class.

<application
android:name=".MainApplication"
<!-- ... -->
</application>