SDK Initialization¶
This document describes Android SDK initialization.
Application Configuration¶
The best place to initialize the SDK is in the onCreate method of Application. If your app does not have an Application class yet, you need to create one and declare it in AndroidManifest.xml. For an example, see here.
Basic Configuration¶
public class DemoApplication extends Application {
@Override
public void onCreate() {
// Deploy with local environment, Datakit
FTSDKConfig config = FTSDKConfig.builder(datakitUrl);
// Use public Dataway
FTSDKConfig config = FTSDKConfig.builder(datawayUrl, clientToken);
// ...
// config.setDebug(true); // debug mode
FTSdk.install(config);
}
}
class DemoApplication : Application() {
override fun onCreate() {
// Deploy with local environment, Datakit
val config = FTSDKConfig.builder(datakitUrl)
// Use public Dataway
val config = FTSDKConfig.builder(datawayUrl, clientToken)
// ...
// config.setDebug(true) // debug mode
FTSdk.install(config)
}
}
| Method Name | Type | Required | Description |
|---|---|---|---|
| datakitUrl | String | Yes | The URL for reporting data in a local environment (Datakit), e.g., http://10.0.0.1:9529, default port 9529. The device must be able to access this address. Note: Choose either datakitUrl or datawayUrl. |
| datawayUrl | String | Yes | The URL for reporting data via public Dataway, obtained from the [Real User Monitoring] application, e.g., https://open.dataway.url. The device must be able to access this address. Note: Choose either datakitUrl or datawayUrl. |
| clientToken | String | Yes | Authentication token, required together with datawayUrl. |
| setDebug | Boolean | No | Whether to enable debug mode, default is false. When enabled, SDK logs are printed. |
| 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 only support the main process, default is true. Set to false if you need to run in other processes. |
| setAllowWebViewHost | Array | No | Set the host scope for WebView RUM and Log Bridge. null allows all hosts, an empty array means not using Bridge automatically. Configured hosts also match their subdomains. Default is null. Supported since ft-sdk 1.7.5. See WebView Monitoring. |
| setEnableAccessAndroidID | Boolean | No | Whether to enable access to Android ID, default is true. Set to false to stop collecting the device_uuid field. For market privacy audits, see here. |
| addGlobalContext | Dictionary | No | Add SDK global attributes. See here for the rules. |
| setServiceName | String | No | Set the service name, affects the service field in Log and RUM data. Default is df_rum_android. |
| setAutoSync | Boolean | No | Whether to automatically sync data to the server after collection, default is true. When false, use FTSdk.flushSyncData() to manage data sync manually. |
| setSyncPageSize | Int | No | Set the number of items per sync request. SyncPageSize.MINI 5, SyncPageSize.MEDIUM 10, SyncPageSize.LARGE 50, default SyncPageSize.MEDIUM. |
| setCustomSyncPageSize | Enum | No | Set the number of items per sync request, valid range [5,). Note that a larger page size consumes more computing resources. Default is 10. Note: Only one of setSyncPageSize and setCustomSyncPageSize needs to be configured. |
| setSyncSleepTime | Int | No | Set the interval between syncs, in ms, valid range [0,5000], default 0. |
| enableDataIntegerCompatible | Void | No | Recommended to enable when coexisting with web data. Used to handle storage compatibility issues for web data types. Enabled by default since ft-sdk 1.6.9. |
| setNeedTransformOldCache | Boolean | No | Whether to maintain compatibility with old cached data from versions below ft-sdk 1.6.0. Default is false. |
| enableFileDataStore | Void | No | Enable file-based cache for sync cache and RUM aggregated data. Still uses SQLite cache by default. Supported since ft-sdk 1.7.2. |
| setUseFileDataStore | Boolean | No | Set whether to use file-based cache. Pass true to use file cache, false to use the default SQLite cache. Supported since ft-sdk 1.7.2. |
| setFileDataStoreShadow | Boolean | No | Enable shadow writes for file cache. When enabled, reads still use SQLite, while writes are mirrored to the file cache. Used for pre-migration validation. Supported since ft-sdk 1.7.2. |
| setCompressIntakeRequests | Boolean | No | Compress upload sync data using deflate. Enabled by default; set to false to disable. Supported since ft-sdk 1.6.3. |
| enableLimitWithCacheSize | Void, Long | No | Enable total cache size limit. Default is 100MB, in bytes. When cacheSize is passed, the valid range is [30MB,). After enabling, FTLoggerConfig.setLogCacheLimitCount and FTRUMConfig.setRumCacheLimitCount become ineffective. Supported since ft-sdk 1.7.2. |
| setCacheDiscard | CacheDiscard | No | Set the discard strategy when the cache reaches the size limit. Default is CacheDiscard.DISCARD. DISCARD discards newly appended data; DISCARD_OLDEST deletes the oldest cached data. Supported since ft-sdk 1.7.2. |
| enableLimitWithDbSize | Void | No | Deprecated, kept for backward compatibility with older versions. It is recommended to use enableLimitWithCacheSize instead. |
| setEnableOkhttpRequestTag | Boolean | No | Automatically add a unique ResourceID to OkHttp Requests for scenarios with high concurrency of identical requests. Supported since ft-sdk 1.6.10 and ft-plugin 1.3.5. |
| setProxy | java.net.Proxy | No | Set Proxy for data network sync requests. Only supports okhttp3. Supported since ft-sdk 1.6.10. |
| setProxyAuthenticator | okhttp3.Authenticator | No | Set Proxy authenticator for data sync network requests. Only supports okhttp3. Supported since ft-sdk 1.6.10. |
| setDns | okhttp3.Dns | No | Support custom DNS for domain name resolution in data sync network requests. Only supports okhttp3. Supported since ft-sdk 1.6.10. |
| setDataModifier | DataModifier | No | Modify individual fields. Supported since ft-sdk 1.6.11. See here for usage examples. |
| setLineDataModifier | LineDataModifier | No | Modify individual data lines. Supported since ft-sdk 1.6.11. See here for usage examples. |
| setEnableDataFilter | Boolean | No | Whether to enable blacklist filtering compatible with DataKit. Default is true. Supports filtering Logging and RUM data. Supported since ft-sdk 1.7.2. |
| setDataFilters | HashMap<String, String[]> |
No | Set local blacklist filtering rules. Supported categories are logging and rum; data matching the rules will be discarded. Supported since ft-sdk 1.7.2. |
| setRemoteConfiguration | Boolean | No | Whether to enable remote configuration for data collection. Default is false. When enabled, SDK initialization or app warm start triggers a data update. Supported since ft-sdk 1.6.12. Requires DataKit version >= 1.60 or use public Dataway. |
| setRemoteConfigMiniUpdateInterval | Int | No | Set the minimum update interval for data update, in seconds. Default is 12 hours. Supported since ft-sdk 1.6.12. |
| setRemoteConfigurationCallBack | FTRemoteConfigManager.FetchResult | No | Remote configuration result callback. See here for code examples. Supported since ft-sdk 1.6.16. |
File Cache¶
Since ft-sdk 1.7.2, you can write sync cache and RUM aggregated data to file-based cache. To ensure a smooth upgrade for older versions, the SDK still uses SQLite cache by default. To enable file cache, explicitly call enableFileDataStore() on FTSDKConfig.
If you want to validate file cache writes first, enable shadow writes. When enabled, the SDK still reads from SQLite while mirroring writes to the file cache. After validation, switch to enableFileDataStore().
Cache Size Limit¶
Since ft-sdk 1.7.2, it is recommended to use enableLimitWithCacheSize to configure the total cache size limit. After enabling, the separate log count limit (FTLoggerConfig.setLogCacheLimitCount) and RUM count limit (FTRUMConfig.setRumCacheLimitCount) become ineffective.
Blacklist Filtering¶
Since ft-sdk 1.7.2, blacklist filtering compatible with DataKit is supported to filter Logging and RUM data before writing to the local cache. This feature is enabled by default; you can disable it with setEnableDataFilter(false).
Blacklist rules are divided into local rules and remote rules:
- Local rules are configured via
setDataFiltersand support theloggingandrumcategories. - Remote rules are pulled by the SDK from DataKit or Dataway via
/v1/datakit/pull?filters=true. - Local and remote rules apply simultaneously. If any rule matches, the data item is discarded.
- Blacklist filtering runs after
LineDataModifierand before writing to the local cache. If bothsetLineDataModifierand blacklist filtering are configured, the filtering rules are evaluated against the modified data.
Rule expressions must be wrapped in {}. Supported operators: in, not in, match, not match. Multiple conditions can be combined with and / or. Field sources include data tags and fields, and also support data type identifier fields such as source, measurement, and 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, explicitly set:
The polling interval for remote blacklist follows 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, for example 10, 30s, 2m, 1h.
Runtime Capabilities¶
Shut Down SDK¶
If you need to dynamically change SDK configuration, you must shut down the SDK first to avoid generating incorrect data.
Clear SDK Cache Data¶
Use FTSdk to clear cached data that has not been reported.
Set Auto-Sync Data¶
Since ft-sdk 1.7.3, you can dynamically enable or disable automatic sync of cached data after SDK initialization. When disabled, the SDK will still write collected data to the local cache but will not automatically trigger sync after data collection; you can manage data synchronization manually with FTSdk.flushSyncData().
Manually Sync Data¶
Use FTSdk to manually sync data.
Only when
FTSdk.setAutoSync(false)is set, you need to manually sync data.