# ProcessCameraProvider.Companion

Artifact: [androidx.camera:camera-lifecycle](https://developer.android.com/jetpack/androidx/releases/camera) [View Source](https://cs.android.com/search?q=file:androidx/camera/lifecycle/ProcessCameraProvider.kt+class:androidx.camera.lifecycle.ProcessCameraProvider.Companion) Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/camera#1.4.0)

*** ** * ** ***

<br />

```
public static class ProcessCameraProvider.Companion
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final void` | `@https://developer.android.com/reference/androidx/camera/lifecycle/ExperimentalCameraProviderConfiguration https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#configureInstance(androidx.camera.core.CameraXConfig)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/core/CameraXConfig cameraXConfig)` Perform one-time configuration of the ProcessCameraProvider singleton with the given `https://developer.android.com/reference/androidx/camera/core/CameraXConfig`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider>` | `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#getInstance(android.content.Context)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context)` Retrieves the ProcessCameraProvider associated with the current process. |

| ### Extension functions |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider` | `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProviderExtKt.https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider.Companion#(androidx.camera.lifecycle.ProcessCameraProvider.Companion).awaitInstance(android.content.Context)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider.Companion receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context )` Retrieves the `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider`. |

## Public methods

### configureInstance

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/camera#1.4.0)

```
@ExperimentalCameraProviderConfiguration
public static final void configureInstance(@NonNull CameraXConfig cameraXConfig)
```

Perform one-time configuration of the ProcessCameraProvider singleton with the given `https://developer.android.com/reference/androidx/camera/core/CameraXConfig`.

This method allows configuration of the camera provider via `https://developer.android.com/reference/androidx/camera/core/CameraXConfig`. All initialization tasks, such as communicating with the camera service, will be executed on the `https://developer.android.com/reference/java/util/concurrent/Executor.html` set by `https://developer.android.com/reference/androidx/camera/core/CameraXConfig.Builder#setCameraExecutor(java.util.concurrent.Executor)`, or by an internally defined executor if none is provided.

This method is not required for every application. If the method is not called and `https://developer.android.com/reference/androidx/camera/core/CameraXConfig.Provider` is not implemented in `https://developer.android.com/reference/android/app/Application.html`, default configuration will be used.

Once this method is called, the instance configured by the given `https://developer.android.com/reference/androidx/camera/core/CameraXConfig` can be retrieved with `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#getInstance(android.content.Context)`. `https://developer.android.com/reference/androidx/camera/core/CameraXConfig.Provider` implemented in `https://developer.android.com/reference/android/app/Application.html` will be ignored.

Configuration can only occur once. Once the ProcessCameraProvider has been configured with `configureInstance()` or `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#getInstance(android.content.Context)`, this method will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html`. Because configuration can only occur once, **usage of this method from library code is not recommended** as the application owner should ultimately be in control of singleton configuration.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/core/CameraXConfig cameraXConfig` | The configuration options for the singleton process camera provider instance. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | If the camera provider has already been configured by a previous call to `configureInstance()` or `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#getInstance(android.content.Context)`. |

### getInstance

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/camera#1.4.0)

```
public static final @NonNull ListenableFuture<@NonNull ProcessCameraProvider> getInstance(@NonNull Context context)
```

Retrieves the ProcessCameraProvider associated with the current process.

The instance returned here can be used to bind use cases to any `https://developer.android.com/reference/androidx/lifecycle/LifecycleOwner` with `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#bindToLifecycle(androidx.lifecycle.LifecycleOwner,androidx.camera.core.CameraSelector,kotlin.Array)`.

The instance's configuration may be customized by subclassing the application's `https://developer.android.com/reference/android/app/Application.html` class and implementing `https://developer.android.com/reference/androidx/camera/core/CameraXConfig.Provider`. For example, the sample implements `https://developer.android.com/reference/androidx/camera/core/CameraXConfig.Provider#getCameraXConfig()` and initializes this process camera provider with a `https://developer.android.com/reference/androidx/camera/camera2/Camera2Config` from `https://developer.android.com/reference/androidx/camera/camera2/package-summary`, and with a custom executor.

```kotlin
import androidx.camera.camera2.Camera2Config
import androidx.camera.core.CameraXConfig

@Override
fun getCameraXConfig(): CameraXConfig {
    return CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
        .setCameraExecutor(executor)
        .setSchedulerHandler(handler)
        .build()
}
```

If it isn't possible to subclass the `https://developer.android.com/reference/android/app/Application.html` class, such as in library code, then the singleton can be configured via `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#configureInstance(androidx.camera.core.CameraXConfig)` before the first invocation of `getInstance(context)`, the sample implements a customized camera provider that configures the instance before getting it.

```kotlin
import androidx.camera.camera2.Camera2Config
import androidx.camera.core.CameraProvider
import androidx.camera.core.CameraXConfig
import androidx.camera.lifecycle.ExperimentalCameraProviderConfiguration
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.lifecycle.ProcessCameraProvider.Companion.configureInstance
import androidx.concurrent.futures.await

var configured = false // Whether the camera provider has been configured or not.

@androidx.annotation.OptIn(ExperimentalCameraProviderConfiguration::class)
suspend fun getInstance(context: Context): ProcessCameraProvider {
    synchronized(CameraProvider::class.java) {
        if (!configured) {
            configured = true
            configureInstance(
                CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
                    .setCameraExecutor(executor)
                    .setSchedulerHandler(scheduleHandler)
                    .build()
            )
        }
    }
    return ProcessCameraProvider.getInstance(context).await()
}
```

If no `https://developer.android.com/reference/androidx/camera/core/CameraXConfig.Provider` is implemented by `https://developer.android.com/reference/android/app/Application.html`, or if the singleton has not been configured via `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#configureInstance(androidx.camera.core.CameraXConfig)` a default configuration will be used.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | The application context. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider>` | A future which will contain the ProcessCameraProvider. Cancellation of this future is a no-op. This future may fail with an `https://developer.android.com/reference/androidx/camera/core/InitializationException` and associated cause that can be retrieved by `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-throwable/cause.html`. The cause will be a `https://developer.android.com/reference/androidx/camera/core/CameraUnavailableException` if it fails to access any camera during initialization. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if CameraX fails to initialize via a default provider or a `https://developer.android.com/reference/androidx/camera/core/CameraXConfig.Provider`. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#configureInstance(androidx.camera.core.CameraXConfig)` |   |

## Extension functions

### ProcessCameraProviderExtKt.awaitInstance

Artifact: [androidx.camera:camera-lifecycle](https://developer.android.com/jetpack/androidx/releases/camera) [View Source](https://cs.android.com/search?q=file:androidx/camera/lifecycle/ProcessCameraProviderExt.kt+function:awaitInstance)

```
public static final @NonNull ProcessCameraProvider ProcessCameraProviderExtKt.awaitInstance(
    @NonNull ProcessCameraProvider.Companion receiver,
    @NonNull Context context
)
```

Retrieves the `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider`.

This is a suspending function unlike `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#getInstance(android.content.Context)` which returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | The application context. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider` | A fully initialized ProcessCameraProvider for the current process. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/camera/core/InitializationException` | If failed to retrieve the ProcessCameraProvider, use `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-throwable/cause.html` to get the error cause. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider#getInstance(android.content.Context)` |   |