Skip to content

SDK Initialization

This article covers the content related to Android SDK initialization.

Application Configuration

The optimal location for SDK initialization is in the onCreate method of Application. If your application has not yet created an Application, you need to create one and declare it in AndroidManifest.xml. For an example, please refer to here.

<application
       android:name="YourApplication">
</application>

Basic Configuration

public class DemoApplication extends Application {

    @Override
    public void onCreate() {
        // Local environment deployment, Datakit deployment
        FTSDKConfig config = FTSDKConfig.builder(datakitUrl);
        // Using public network DataWay
        FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken);
        // ...
        // config.setDebug(true);              // debug mode
        FTSdk.install(config);
    }
}
class DemoApplication : Application() {
    override fun onCreate() {
        // Local environment deployment, Datakit deployment
        val config = FTSDKConfig.builder(datakitUrl)
        // Using public network DataWay
        val config = FTSDKConfig.builder(datawayUrl, clientToken)
        // ...
        // config.setDebug(true)              // debug mode
        FTSdk.install(config)
    }
}
Method Name Type Required Meaning
datakitUrl String Yes Local environment deployment (Datakit) reporting URL address, example: http://10.0.0.1:9529, default port is 9529, the device installing the SDK must be able to access this address. Note: Choose one between datakitUrl and datawayUrl configuration
datawayUrl String Yes Public network Dataway reporting URL address, obtained from the [RUM] application, example: https://open.dataway.url, the device installing the SDK must be able to access this address. Note: Choose one between datakitUrl and datawayUrl configuration
clientToken String Yes Authentication token, needs to be configured together with datawayUrl
setDebug Boolean No Whether to enable debug mode, default is false, SDK runtime logs can be printed only after enabling
setEnv EnvType No Set the collection environment, default is EnvType.PROD
setEnv String No Set the collection environment, default is prod. Note: Only one of String or EnvType type needs to be configured
setOnlySupportMainProcess Boolean No Whether to support running only in the main process, default is true, if execution in other processes is needed, set this field to false
setEnableAccessAndroidID Boolean No Enable obtaining Android ID, default is true, if set to false, the device_uuid field data will not be collected, for market privacy audit related information see here
addGlobalContext Dictionary No Add SDK global attributes, for addition rules please refer to here
setServiceName String No Set the service name, affects the service field data in Log and RUM, default is df_rum_android
setAutoSync Boolean No Whether to automatically sync collected data to the server, default is true. When false, use FTSdk.flushSyncData() to manage data synchronization manually
setSyncPageSize Int No Set the number of entries per sync request, SyncPageSize.MINI 5 entries, SyncPageSize.MEDIUM 10 entries, SyncPageSize.LARGE 50 entries, default is SyncPageSize.MEDIUM
setCustomSyncPageSize Enum No Set the number of entries per sync request, range [5,), note that a larger number of entries means data synchronization consumes more computing resources, default is 10. Note: Only one of setSyncPageSize and setCustomSyncPageSize needs to be configured
setSyncSleepTime Int No Set the sync interval time, range [0,5000], unit ms, default is 0
enableDataIntegerCompatible Void No Recommended to enable when coexistence with web data is needed. This configuration handles web data type storage compatibility issues. Default enabled in ft-sdk version 1.6.9
setNeedTransformOldCache Boolean No Whether compatibility is needed for syncing old cache data from ft-sdk versions below 1.6.0, default is false
enableFileDataStore Void No Enable file cache, used for sync cache and RUM aggregated data. SQLite cache is still used by default, supported in ft-sdk version 1.7.2 and above
setUseFileDataStore Boolean No Set whether to use file cache. Pass true to use file cache, pass false to use default SQLite cache, supported in ft-sdk version 1.7.2 and above
setFileDataStoreShadow Boolean No Enable file cache shadow writing. After enabling, reading still uses SQLite, while writing is mirrored to file cache, used for pre-migration verification, supported in ft-sdk version 1.7.2 and above
setCompressIntakeRequests Boolean No Deflate compression for upload sync data, enabled by default, set to false to disable, this method is supported in ft-sdk version 1.6.3 and above
enableLimitWithCacheSize Void, Long No Enable total cache size limit, default 100MB, unit Byte. When cacheSize is passed, the range is [30MB,). After enabling, FTLoggerConfig.setLogCacheLimitCount and FTRUMConfig.setRumCacheLimitCount will become invalid. Supported in ft-sdk version 1.7.2 and above
setCacheDiscard CacheDiscard No Set the discard strategy when cache reaches the size limit, default is CacheDiscard.DISCARD. DISCARD discards appended data, DISCARD_OLDEST deletes the oldest cached data. Supported in ft-sdk version 1.7.2 and above
enableLimitWithDbSize Void No Deprecated, retained for compatibility with older versions. It is recommended to use enableLimitWithCacheSize instead
setEnableOkhttpRequestTag Boolean No Automatically add a unique ResourceID to OkHttp Request, used for high concurrency scenarios with the same request. Supported in ft-sdk version 1.6.10 and above, ft-plugin version 1.3.5 and above
setProxy java.net.Proxy No Set Proxy for data network sync requests, only supports okhttp3, supported in ft-sdk version 1.6.10 and above
setProxyAuthenticator okhttp3.Authenticator No Set Proxy for data sync network requests, only supports okhttp3, supported in ft-sdk version 1.6.10 and above
setDns okhttp3.Dns No Data sync network requests support custom DNS for custom domain name resolution, only supports okhttp3, supported in ft-sdk version 1.6.10 and above
setDataModifier DataModifier No Modify a single field. Supported in ft-sdk version 1.6.11 and above, usage example can be found here
setLineDataModifier LineDataModifier No Modify a single data line. Supported in ft-sdk version 1.6.11 and above, usage example can be found here
setEnableDataFilter Boolean No Whether to enable blacklist filtering capability compatible with DataKit, default is true. Supports filtering Logging and RUM data, supported in ft-sdk version 1.7.2 and above
setDataFilters HashMap<String, String[]> No Set local blacklist filtering rules. Supported categories are logging and rum; data matching the rules will be discarded. Supported in ft-sdk version 1.7.2 and above
setRemoteConfiguration Boolean No Whether to enable remote configuration for data collection, default is false. After enabling, SDK initialization or application hot start will trigger data updates. Supported in ft-sdk version 1.6.12 and above. DataKit version requirement >= 1.60 or using public network Dataway
setRemoteConfigMiniUpdateInterval Int No Set the minimum update interval for data, unit seconds, default 12 hours. Supported in ft-sdk version 1.6.12 and above
setRemoteConfigurationCallBack FTRemoteConfigManager.FetchResult No Remote configuration result callback, code example. Supported in ft-sdk version 1.6.16 and above

File Cache

ft-sdk version 1.7.2 and above supports writing sync cache and RUM aggregated data to file cache. To ensure a smooth upgrade from older versions, the SDK still uses SQLite cache by default; if you wish to enable file cache, you can explicitly enable it in FTSDKConfig.

FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken)
        .enableFileDataStore();

FTSdk.install(config);
val config = FTSDKConfig.builder(datawayUrl, clientToken)
    .enableFileDataStore()

FTSdk.install(config)

If you need to verify file cache writing first, you can enable shadow writing. After enabling, the SDK still reads data from SQLite, while simultaneously mirroring writes to the file cache; switch to enableFileDataStore after verification passes.

FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken)
        .setFileDataStoreShadow(true);
val config = FTSDKConfig.builder(datawayUrl, clientToken)
    .setFileDataStoreShadow(true)

Cache Size Limit

ft-sdk version 1.7.2 and above recommends using the enableLimitWithCacheSize configuration to set the SDK total cache size limit. After enabling, the separate log entry limit FTLoggerConfig.setLogCacheLimitCount and RUM entry limit FTRUMConfig.setRumCacheLimitCount will become invalid.

FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken)
        // Enable total cache size limit, example is 100MB
        .enableLimitWithCacheSize(100 * 1024 * 1024L)
        // Delete the oldest cached data when cache reaches the limit
        .setCacheDiscard(CacheDiscard.DISCARD_OLDEST);

FTSdk.install(config);
val config = FTSDKConfig.builder(datawayUrl, clientToken)
    // Enable total cache size limit, example is 100MB
    .enableLimitWithCacheSize(100 * 1024 * 1024L)
    // Delete the oldest cached data when cache reaches the limit
    .setCacheDiscard(CacheDiscard.DISCARD_OLDEST)

FTSdk.install(config)

Blacklist Filtering

ft-sdk version 1.7.2 and above supports blacklist filtering compatible with DataKit, used to filter Logging and RUM data before writing to local cache. This capability is enabled by default and can be turned off via setEnableDataFilter(false).

Blacklist rules are divided into local rules and remote rules:

  • Local rules are configured via setDataFilters, supporting logging and rum categories.
  • Remote rules are pulled by the SDK from DataKit or Dataway via /v1/datakit/pull?filters=true.
  • Local rules and remote rules take effect simultaneously, data will be discarded if any rule matches.
  • Blacklist filtering is executed after LineDataModifier and before local cache writing. If both setLineDataModifier and blacklist filtering are configured, the filtering rules will be judged based on the modified data.

Rule expressions need to be written within {}, supporting in, not in, match, not match operators, multiple conditions can be combined using and / or. Field sources include data tags and fields, and also support data type identifier fields such as source, measurement, class.

HashMap<String, String[]> filters = new HashMap<>();
filters.put("logging", new String[]{
        "{ source in ['custom_log'] and message match ['password'] }"
});
filters.put("rum", new String[]{
        "{ source in ['resource'] and status in [404, 503] }"
});

FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken)
        .setEnableDataFilter(true)
        .setDataFilters(filters);

FTSdk.install(config);
val filters = hashMapOf(
    "logging" to arrayOf(
        "{ source in ['custom_log'] and message match ['password'] }"
    ),
    "rum" to arrayOf(
        "{ source in ['resource'] and status in [404, 503] }"
    )
)

val config = FTSDKConfig.builder(datawayUrl, clientToken)
    .setEnableDataFilter(true)
    .setDataFilters(filters)

FTSdk.install(config)

If you need to disable both local and remote data filtering, you can set it explicitly:

FTSDKConfig.builder(datawayUrl, clientToken)
        .setEnableDataFilter(false);

The pull interval for remote blacklists is based on the pull_interval returned by the server; when the server does not return a valid value, the SDK uses 10 seconds as the fallback interval. pull_interval supports seconds or strings with units, such as 10, 30s, 2m, 1h.

Runtime Capabilities

Shutting Down the SDK

If dynamically changing SDK configuration, it needs to be shut down first to avoid generating erroneous data.

FTSdk.shutDown();
FTSdk.shutDown()

Clearing SDK Cache Data

Use FTSdk to clear unreported cache data.

FTSdk.clearAllData();
FTSdk.clearAllData()

Setting Automatic Data Sync

ft-sdk version 1.7.3 and above supports dynamically enabling or disabling automatic cache data synchronization after SDK initialization. After disabling, the SDK will still write collected data to local cache, but will not automatically trigger synchronization after data collection; it can be combined with FTSdk.flushSyncData() to manage data synchronization manually.

// Disable automatic sync
FTSdk.setAutoSync(false);

// Enable automatic sync
FTSdk.setAutoSync(true);
// Disable automatic sync
FTSdk.setAutoSync(false)

// Enable automatic sync
FTSdk.setAutoSync(true)

Active Data Sync

Use FTSdk to actively sync data.

Only when FTSdk.setAutoSync(false) is set, manual data synchronization is required.

FTSdk.flushSyncData();
FTSdk.flushSyncData()