Initialization

The access point to all the features provided by Malwarelytics for Android is the AppProtection class. Before using AppProtection an instance has to be obtained with AppProtection.getInstance(). The next step is to initialize it with a desired configuration. There are two initialization methods provided - synchronous initialize(AppProtectionConfig) and asynchronous initializeAsync(AppProtectionConfig, InitializationObserver).

The AppProtection can be used after initialization is finished - either initialize(AppProtectionConfig) returns or initializeAsync(AppProtectionConfig, InitializationObserver) calls the observer. That doesn’t mean that it already has all up-to-date data. Especially the first initialization performs some background processing after the initialization is finished.

Synchronous Initialization

Synchronous initialization has to be called from a background thread, calling it on the main thread would cause ANR (Application Not Responding).

val appProtection = AppProtection.getInstance()
val appProtectionConfig = AppProtectionConfig.Builder(appContext)
    // add the desired configuration here
    .build()
// this has to be called on a background thread only
val initializationResult = appProtection.initialize(appProtectionConfig)
// the result value should be checked
// now it's possible to start using Malwarelytics for Android

Asynchronous Initialization

Asynchronous initialization can be called from any thread.

val appProtection = AppProtection.getInstance()
val appProtectionConfig = AppProtectionConfig.Builder(appContext)
    // add the desired configuration here
    .build()
appProtection.initializeAsync(appProtectionConfig, object : AppProtection.InitializationObserver {
    override fun onInitialized(initializationResult: AppProtection.InitializationResult) {
        // the result value should be checked
        // now it's possible to start using Malwarelytics for Android
    }
})

Initialization Result

Initialization returns a value from the InitializationResult enum, indicating whether the SDK can be fully used with all features or in a limited offline mode. Possible initialization return values are: SUCCESS, TEMPORARY_OFFLINE_MODE, and PERMANENT_OFFLINE_MODE.

Result value Description
SUCCESS The SDK was successfully initialized into full online mode.
TEMPORARY_OFFLINE_MODE There was a communication problem with the remote server. The communication might get reestablished later.
PERMANENT_OFFLINE_MODE The SDK is misconfigured, and communication with the remote server is not possible.

When Malwarelytics for Android is initialized into an offline mode, no communication with the remote server is happening. All features use only built-in detection data.

User Identifier

When Malwarelytics for Android initialize with the initialization result SUCCESS an internal identifier provided by the remote server becomes available. This internal ID called avUid can be obtained by calling:

val avUid = appProtection.getAvUid()

Other Identifiers

Malwarelytics for Android offers API for two other identifiers - clientAppUserId and clientAppDeviceId. Values for these identifiers are provided by the app integrating Malwarelytics for Android. The values are sent to the remote server where they allow to find the desired user or device.

Identifier Description
clientAppUserId App’s user ID. Typically app’s internal user ID or its derivative.
clientAppDeviceId App’s device ID. Typically an activation ID or its derivative.

These identifiers can be either set in configuration or later after initialization via API.

The setting of these values during the Malwarelytics initialization phase:

val config = AppProtectionConfig.Builder(appContext)
    .clientAppUserId(INTERNAL_CLIENT_USER_ID) // Use if an internal user ID is available at config time
    .clientAppDeviceId(INTERNAL_CLIENT_DEVICE_ID) // Use if an internal device ID is available at config time
    // other configuration items
    .build()

Setting or updating of these values after the Malwarelytics initialization phase:

val appProtection = AppProtection.getInstance()
// these methods cannot be called before the SDK is initialized
appProtection.updateClientAppUserId("NEW_VALUE_FOR_CLIENT_APP_USER_ID")
appProtection.updateClientAppDeviceId("NEW_VALUE_FOR_CLIENT_APP_DEVICE_ID")

Obtaining previously set values after the Malwarelytics initialization phase:

val appProtection = AppProtection.getInstance()
// these methods cannot be called before the SDK is initialized
val clientAppUserId = appProtection.getClientAppUserId()
val clientAppDeviceId = appProtection.getClientAppDeviceId()

Releasing

When the features provided by Malwarelytics for Android are not needed anymore, for example, when the user opts out from its usage, or to allow for a configuration change, the AppProtection instance can be released.

The AppProtection instance can be released by calling:

val appProtection = AppProtection.getInstance()
appProtection.release()

or by calling

val appProtection = AppProtection.getInstance()
appProtection.releaseAndResetAvUid()

Calling the release() method just releases the instance. Calling releaseAndResetAvUid() releases the instance and resets the avUid identifier, then a new value for the identifier is obtained from the remote server during the subsequent initialization.

Malwarelytics for Android cannot be used after release. It has to be re-initialized first.

Last updated on Jul 31, 2024 (21:35) View product
Search

develop

Malwarelytics for Android