Skip to content

SDK Initialization

This document covers the content related to Android SDK initialization.

Application Configuration

The optimal location for SDK initialization is within the onCreate method of the 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 Description
datakitUrl String Yes The reporting URL address for local environment deployment (Datakit). 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 The 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, must 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 Sets the collection environment. Default is EnvType.PROD.
setEnv String No Sets the collection environment. Default is prod. Note: Configure only one of String or EnvType.
setOnlySupportMainProcess Boolean No Whether to support running only in the main process. Default is true. Set this field to false if execution is needed in other processes.
setEnableAccessAndroidID Boolean No Enables obtaining Android ID. Default is true. Set to false, then the device_uuid field data will not be collected. For market privacy audit related information, see here.
addGlobalContext Dictionary No Adds SDK global attributes. For addition rules, please refer to here.
setServiceName String No Sets the service name, affecting the service field data in Log and RUM. Default is df_rum_android.
setAutoSync Boolean No Whether to automatically synchronize data to the server after collection. Default is true. When false, use FTSdk.flushSyncData() to manage data synchronization manually.
setSyncPageSize Int No Sets the number of entries per synchronization request. SyncPageSize.MINI 5 entries, SyncPageSize.MEDIUM 10 entries, SyncPageSize.LARGE 50 entries. Default is SyncPageSize.MEDIUM.
setCustomSyncPageSize Enum No Sets the number of entries per synchronization request. Range [5,). Note that a larger number of entries means data synchronization occupies more computing resources. Default is 10. Note: Configure only one of setSyncPageSize and setCustomSyncPageSize.
setSyncSleepTime Int No Sets the synchronization 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. Enabled by default in ft-sdk version 1.6.9.
setNeedTransformOldCache Boolean No Whether compatibility is needed to synchronize old cache data from ft-sdk versions below 1.6.0. Default is false.
enableFileDataStore Void No Enables file caching, used for synchronization 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 Sets whether to use file caching. Pass true to use file caching, pass false to use default SQLite caching. Supported in ft-sdk version 1.7.2 and above.
setFileDataStoreShadow Boolean No Enables file cache shadow writing. When enabled, reading still uses SQLite, while writes are mirrored to file cache for pre-migration verification. Supported in ft-sdk version 1.7.2 and above.
setCompressIntakeRequests Boolean No Compresses upload synchronization data with deflate. 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 Enables total cache size limit. Default 100MB, unit Byte. When cacheSize is passed, the valid 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 Sets the discard policy 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 adds a unique ResourceID to Okhttp Requests, used for high-concurrency scenarios with identical requests. Supported in ft-sdk 1.6.10 and above, ft-plugin 1.3.5 and above.
setProxy java.net.Proxy No Sets Proxy for data network synchronization requests, only supports okhttp3. Supported in ft-sdk 1.6.10 and above.
setProxyAuthenticator okhttp3.Authenticator No Sets Proxy for data synchronization network requests, only supports okhttp3. Supported in ft-sdk 1.6.10 and above.
setDns okhttp3.Dns No Data synchronization network requests support custom Dns for custom domain name resolution processing, only supports okhttp3. Supported in ft-sdk 1.6.10 and above.
setDataModifier DataModifier No Modifies a single field. Supported in ft-sdk 1.6.11 and above. For usage examples, see here.
setLineDataModifier LineDataModifier No Modifies a single piece of data. Supported in ft-sdk 1.6.11 and above. For usage examples, see here.
setEnableDataFilter Boolean No Whether to enable DataKit-compatible blacklist filtering capability. Default is true. Supports filtering Logging and RUM data. Supported in ft-sdk version 1.7.2 and above.
setDataFilters HashMap<String, String[]> No Sets 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 the remote configuration function for data collection. Default is false. After enabling, SDK initialization or application hot start will trigger data updates. Supported in ft-sdk 1.6.12 and above. DataKit version requirement >= 1.60 or using public network Dataway.
setRemoteConfigMiniUpdateInterval Int No Sets the minimum interval for data updates, unit seconds, default 12 hours. Supported in ft-sdk 1.6.12 and above.
setRemoteConfigurationCallBack FTRemoteConfigManager.FetchResult No Remote configuration result callback, code example. Supported in ft-sdk 1.6.16 and above.

File Cache

ft-sdk version 1.7.2 and above supports writing synchronization 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 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's total cache size limit. After enabling, the individual log entry limit FTLoggerConfig.setLogCacheLimitCount and the 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 DataKit-compatible blacklist filtering, 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 is matched.
  • Blacklist filtering is executed after LineDataModifier and before local cache write. If both setLineDataModifier and blacklist filtering are configured, the filtering rules will be judged based on the modified data.

Rule expressions must 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)

To disable both local and remote data filtering, set explicitly:

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

The pull interval for remote blacklists is based on the pull_interval returned by the server; if 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, e.g., 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()

Actively Synchronizing Data

Use FTSdk to actively synchronize data.

Only required when FTSdk.setAutoSync(false) is set, to perform data synchronization manually.

FTSdk.flushSyncData();
FTSdk.flushSyncData()