# Configuration

The `Logger` needs to be started before it can be used. Note that the `start` method only needs to be called once and will cause the Logger to retain this configuration until the end of the process. Subsequent calls to the `start` method won't have any effect.

The minimum required configuration of the SDK is as follows:

=== "Android (Kotlin)"

    ```kotlin
    import io.bitdrift.capture.Capture.Logger
    import io.bitdrift.capture.providers.session.SessionStrategy

    Logger.start(
      apiKey = "<your-api-key>",
      sessionStrategy = SessionStrategy.Fixed,
    )
    ```

    ### Initialization
    The Android SDK handles its initial configuration using [Jetpack Startup](https://developer.android.com/topic/libraries/app-startup). It uses an `Initializer` called `ContextHolder` to get a reference to the application's `Context`.

    #### Initialization Dependency

    If you wish to start the `Logger` from inside your own `Initializer` make sure to add it as a dependency:
    ```kotlin
    import io.bitdrift.capture.ContextHolder

    class AppExampleInitializer : Initializer<AppExampleDependency> {
      override fun create(context: Context): AppExampleDependency {
        // You can call Capture.Logger.start() here safely
        return AppExampleDependency()
      }

      override fun dependencies(): List<Class<out Initializer<*>>> {
        // Defines a dependency on ContextHolder so it can be
        // initialized after bitdrift Capture is initialized.
        return listOf(ContextHolder::class.java)
      }
    }
    ```

    #### Manual Initialization
    If you wish to avoid automatic initialization and want to manually call the bitdrift Capture initializer you can follow the steps below.

    Add this to your AndroidManifest.xml configuration to disable automatic initialization:
    ```xml
    <application>
      <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        android:exported="false"
        tools:node="merge"
      >
        <!-- Disable automatic initialization of Bitdrift ContextHolder -->
        <meta-data
          android:name="io.bitdrift.capture.ContextHolder"
          tools:node="remove"
        />
      </provider>
    </application>
    ```

    Manually call the bitdrift initializer:
    ```kotlin
    import io.bitdrift.capture.ContextHolder
    // This needs to run before calling Logger.start()
    AppInitializer.getInstance(applicationContext)
      .initializeComponent(ContextHolder::class.java)
    ```

=== "Android (Java)"

    ```java
    import io.bitdrift.capture.Capture.Logger;
    import io.bitdrift.capture.providers.session.SessionStrategy;

    Logger.start(
      "<your-api-key>",
      new SessionStrategy.Fixed()
    );
    ```

    ### Initialization
    The Android SDK handles its initial configuration using Jetpack Startup[^1]. It uses an `Initializer` called `ContextHolder` to get a reference to the application's `Context`.

    #### Initialization Dependency

    If you wish to start the `Logger` from inside your own `Initializer` make sure to add it as a dependency:
    ```java
    import io.bitdrift.capture.ContextHolder;

    class AppExampleInitializer implements Initializer<AppExampleDependency> {
      @Override
      public AppExampleDependency create(context: Context) {
        // You can call Capture.Logger.start() here safely
        return new AppExampleDependency();
      }

      @Override
      public List<Class<Initializer<?>>> dependencies() {
        // Defines a dependency on ContextHolder so it can be
        // initialized after bitdrift Capture is initialized.
        return Arrays.asList(ContextHolder.class);
      }
    }
    ```

    #### Manual Initialization
    If you wish to avoid automatic initialization and want to manually call the bitdrift Capture initializer you can follow the steps below.

    Add this to your AndroidManifest.xml configuration to disable automatic initialization:
    ```xml
    <application>
      <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        android:exported="false"
        tools:node="merge"
      >
        <!-- Disable automatic initialization of Bitdrift ContextHolder -->
        <meta-data
          android:name="io.bitdrift.capture.ContextHolder"
          tools:node="remove"
        />
      </provider>
    </application>
    ```

    Manually call the bitdrift initializer:
    ```java
    import io.bitdrift.capture.ContextHolder;
    // This needs to run before calling Logger.start()
    AppInitializer.getInstance(context)
      .initializeComponent(ContextHolder.class);
    ```

=== "iOS (Swift)"

    ```swift
    import Capture

    Logger.start(
      withAPIKey: "<your-api-key>",
      sessionStrategy: .fixed()
    )
    ```
    !!! info
        It's recommended that you initialize the SDK as part of your application's [`application(_:willFinishLaunchingWithOptions:)`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623032-application) or [`application(_:didFinishLaunchingWithOptions:)`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/application(_:didfinishlaunchingwithoptions:)) methods. This allows the SDK to observe system events such as [`didFinishLaunchingNotification`](https://developer.apple.com/documentation/uikit/uiapplication/didfinishlaunchingnotification), which power some of the SDK's out-of-the-box events.


=== "iOS (Objective-C)"

    ```objective-c
    #import <Capture/Capture.h>

    [CAPLogger
      startWithAPIKey:@"your-api-key"
      sessionStrategy: [CAPSessionStrategy fixed]
    ];
    ```
    !!! info
        It's recommended that you initialize the SDK as part of your application's [`application(_:willFinishLaunchingWithOptions:)`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623032-application) or [`application(_:didFinishLaunchingWithOptions:)`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/application(_:didfinishlaunchingwithoptions:)) methods. This allows the SDK to observe system events such as [`didFinishLaunchingNotification`](https://developer.apple.com/documentation/uikit/uiapplication/didfinishlaunchingnotification), which power some of the SDK's out-of-the-box events.

=== "React Native"

    When using the JavaScript-based initialization (e.g. using Expo), the SDK can be initialized as follows:

    ```javascript
    import { init, SessionStrategy } from '@bitdrift/react-native';

    init("<your-api-key>", SessionStrategy.Fixed);
    ```

    When using the native initialization (e.g. ejected Expo, non-Expo React Native), the SDK can be initialized using the per platform instructions above.

!!! note ""
    See the [Quickstart Guide](../quickstart.md) section for information on how to obtain an API Key.

## Start Result Callback

<img alt="Capture SDK Support" src="https://img.shields.io/badge/Android-0.23.0-006C9C">
<img alt="Capture SDK Support" src="https://img.shields.io/badge/iOS-0.23.0-006C9C">

An optional `startResult` callback can be passed to `start` to observe whether SDK initialization succeeded or failed. The callback is invoked synchronously on the calling thread before `start` returns.

=== "Android (Kotlin)"

    ```kotlin
    Logger.start(
      apiKey = "<your-api-key>",
      sessionStrategy = SessionStrategy.Fixed,
    ) { result ->
      when (result) {
        is CaptureResult.Success -> {
          val logger = result.value
          // logger.sessionId, logger.sessionUrl, logger.deviceId
        }
        is CaptureResult.Failure -> {
          val error = result.error // SdkStartFailure
        }
      }
    }
    ```

=== "iOS (Swift)"

    ```swift
    Logger.start(
      withAPIKey: "<your-api-key>",
      sessionStrategy: .fixed(),
      startResult: { result in
        switch result {
        case .success(let logger):
          // logger.sessionID, logger.sessionURL, logger.deviceID
        case .failure(let error):
          // handle error
        }
      }
    )
    ```
