SDK Initialization¶
This article 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.
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 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, 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 | 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, set this field to false if execution in other processes is needed |
| setEnableAccessAndroidID | Boolean | No | Enable obtaining Android ID, default is true, set to false will stop collection of device_uuid field data, related to market privacy audits see here |
| addGlobalContext | Dictionary | No | Add SDK global attributes, addition rules please refer to here |
| setServiceName | String | No | Set 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 SyncPageSize.MEDIUM |
| setCustomSyncPageSize | Enum | No | Set the number of entries per sync request, range [5,), note that larger entry count 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 to need compatibility 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. Default still uses SQLite cache, supported in ft-sdk 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 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 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 1.6.3 and above |
| enableLimitWithCacheSize | Void, Long | No | Enable total cache size limit, default 100MB, unit Byte. When cacheSize is passed, valid range is [30MB,). After enabling, FTLoggerConfig.setLogCacheLimitCount and FTRUMConfig.setRumCacheLimitCount will become invalid. Supported in ft-sdk 1.7.2 and above |
| setCacheDiscard | CacheDiscard | No | Set the discard policy when cache reaches size limit, default is CacheDiscard.DISCARD. DISCARD discards appended data, DISCARD_OLDEST deletes the oldest cached data. Supported in ft-sdk 1.7.2 and above |
| enableLimitWithDbSize | Void | No | Deprecated, retained for backward compatibility with older versions. It is recommended to use enableLimitWithCacheSize instead |
| setEnableOkhttpRequestTag | Boolean | No | Automatically add unique ResourceID to Okhttp Request, 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 | Set Proxy for data network sync requests, only supports okhttp3, supported in ft-sdk 1.6.10 and above |
| setProxyAuthenticator | okhttp3.Authenticator | No | Set Proxy for data sync network requests, only supports okhttp3, supported in ft-sdk 1.6.10 and above |
| setDns | okhttp3.Dns | No | Data sync network requests support custom Dns for custom domain resolution, only supports okhttp3, supported in ft-sdk 1.6.10 and above |
| setDataModifier | DataModifier | No | Modify individual fields. Supported in ft-sdk 1.6.11 and above, usage example see here |
| setLineDataModifier | LineDataModifier | No | Modify single data entries. Supported in ft-sdk 1.6.11 and above, usage example 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 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 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 1.6.12 and above. DataKit version requirement >= 1.60 or using public network Dataway |
| setRemoteConfigMiniUpdateInterval | Int | No | Set 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 sync cache and RUM aggregated data to file cache. To ensure smooth upgrades 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.
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.
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 RUM entry limit FTRUMConfig.setRumCacheLimitCount will become invalid.
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, supportingloggingandrumcategories. - 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 executes after
LineDataModifierand before local cache write. If bothsetLineDataModifierand blacklist filtering are configured, 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, also supporting 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 explicitly set:
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.
Clearing SDK Cache Data¶
Use FTSdk to clear unreported cache data.
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; can be used with FTSdk.flushSyncData() to manage data synchronization manually.
Manual Data Sync¶
Use FTSdk to manually sync data.
Only needed when
FTSdk.setAutoSync(false)to manage data synchronization yourself.