Simplifying SDK Integration
We are pleased to announce the introduction of the Builder Pattern to the Localytics SDKs, available starting from version 7.0.0. This enhancement simplifies SDK initialization, improves code readability, and minimizes integration errors. It gives developers greater control and flexibility during setup, while maintaining a consistent, fluent configuration experience across both iOS and Android platforms.
The Localytics SDK includes a wide range of features such as analytics, privacy management, and data upload controls. Previously, integrating these features required multiple setup calls, often in a specific sequence. To make this process more efficient, we have introduced a builder-based initialization approach that allows developers to configure their SDK setup through a single, chainable structure. This approach supports optional parameters, enforces correct execution order, and results in cleaner, more maintainable code.
What Is the Builder Pattern?
The Builder Pattern is a creational design pattern used to construct complex objects step by step. It separates the process of constructing an object from its representation, resulting in code that is flexible, easy to read, and less prone to initialization issues.
Instead of using large initializers with many parameters, developers can configure only what they need by chaining setup methods in a fluent and readable way. Once all configurations are defined, the object can be built into an immutable and ready-to-use instance. This design pattern improves code readability, reduces the chance of integration errors, and simplifies maintenance as the SDK evolves.
Benefits for Localytics Customers
Integrating the Builder Pattern into our SDKs offers several advantages for Localytics customers. Developers can configure their SDK setup within a single, concise block, without worrying about method ordering or missing parameters. This leads to faster onboarding, cleaner integration, and easier updates in the future.Additionally, this change creates a consistent development experience across iOS and Android, ensuring that teams working across both platforms can apply the same configuration principles.
How the Builder Pattern Works
The Builder Pattern implementation in our SDKs consists of three main components. The client is your application code, which specifies how the SDK should be configured. The builder is the interface that collects configuration parameters through chained method calls. The product is the fully configured SDK instance that is created once the .build() or .autoIntegrate() method is called.
This pattern ensures that all configurations are applied in a controlled and predictable manner, reducing setup complexity and preventing misconfiguration.
iOS SDK Example (Version 7.0.0 and Above)
Starting with version 7.0.0, the iOS SDK replaces the old autoIntegrate approach with a builder-based initialization method. Developers can now configure all SDK options through a single, fluent interface.
Swift
import Localytics
Localytics.builder()
.settingAppKey("YOUR-LOCALYTICS-APP-KEY")
.settingLaunchOptions(launchOptions)
.settingLoggingEnabled(false)
.settingPauseDataUploading(false)
.settingOptedOut(false)
.settingPrivacyOptedOut(false)
.settingTestModeEnabled(false)
.settingLocationMonitoringEnabled(false)
.settingLocationMonitoringPersist(false)
.build()
.autoIntegrate()
Objective-C
@import Localytics;
[[[[[[[[[[[[Localytics builder]
settingAppKey:@"YOUR-LOCALYTICS-APP-KEY"]
settingLaunchOptions:launchOptions]
settingLoggingEnabled:NO]
settingPauseDataUploading:NO]
settingOptedOut:NO]
settingPrivacyOptedOut:NO]
settingTestModeEnabled:NO]
settingLocationMonitoringEnabled:NO]
settingLocationMonitoringPersist:NO]
build]
autoIntegrate];
Each method corresponds to a specific configuration option. For example, settingAppKey defines your Localytics app key, settingLaunchOptions configures app launch parameters, and settingLoggingEnabled, settingPauseDataUploading, and settingOptedOut manage key SDK behaviors. Additional methods control privacy preferences and location tracking options. Together, these methods provide a structured, readable way to initialize the SDK without multiple setup calls.
Android SDK Example (Version 7.0.0 and Above)
The Android SDK also adopts the builder pattern, offering a similar configuration structure and syntax within your Application class.
Java
import com.localytics.androidx.
*;
@Override
public void onCreate() {
super.onCreate();
new Localytics.Builder()
.setPauseDataUpload(false)
.setLoggingEnabled(false)
.setOptedOut(false)
.setPrivacyOptedOut(false).setLocationMonitoringEnabled(false)
.setLocationMonitoringPersist(false)
.redirectLogsToDisk(false, this)
.setTestModeEnabled(false)
.autoIntegrate(this);
}
Kotlin
import com.localytics.androidx.*
override fun onCreate() {
super.onCreate()
Localytics.Builder()
.setPauseDataUpload(false)
.setLoggingEnabled(false)
.setOptedOut(false)
.setPrivacyOptedOut(false)
.setLocationMonitoringEnabled(false)
.setLocationMonitoringPersist(false)
.redirectLogsToDisk(false, this)
.setTestModeEnabled(false)
.autoIntegrate(this)
}
Each of these builder methods corresponds directly to existing configuration methods inprevious SDK versions.
For example,
- setPauseDataUpload(false) replaces
Localytics.pauseDataUploading(false) - setLoggingEnabled(false) replaces
Localytics.setLoggingEnabled(false) - redirectLogsToDisk(false, this) replaces
Localytics.redirectLogsToDisk(false, this). - setOptedOut(false) replaces Localytics.setOptedOut(false)
The builder provides a cleaner syntax and improves readability while maintaining familiar functionality.
Final Thoughts and Next Steps
The Builder Pattern represents a significant improvement in the way developers integrate the Localytics SDK. It makes initialization code cleaner, more maintainable, and easier to extend as new features are introduced. It also ensures consistent behavior across iOS and Android, improving the development experience for cross-platform teams.
We encourage all developers to begin using the new builder-based initialization approach in their applications. To learn more and explore detailed implementation examples, review the documentation for both iOS and Android SDKs included below.
