0Pricing
Android Academy · Lesson

CameraX Overview

Why CameraX over the old camera APIs.

CameraX Overview is a free Android Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why CameraX?

Adding a camera to an Android app used to be painful. The old android.hardware.camera2 API is powerful but extremely verbose, and behaves differently across the thousands of devices in the wild.

CameraX is a Jetpack library that sits on top of Camera2 and gives you a simple, lifecycle-aware, consistent API. In this course you will use CameraX to show a live preview, take photos and record video.

What CameraX Solves

CameraX was designed around three goals:

  • Ease of use — far less boilerplate than Camera2.
  • Consistency — Google tests it against a large device lab, so quirky devices behave predictably.
  • Lifecycle awareness — the camera starts and stops automatically with your screen's lifecycle, so you don't leak the camera.

You describe what you want (a preview, a photo, a video), and CameraX figures out how to do it on the current device.

The Three Core Use Cases

CameraX is built around use cases. A use case is a self-contained piece of camera functionality. The three main ones are:

  • Preview — show what the camera sees on screen.
  • ImageCapture — take a photo.
  • VideoCapture — record video.

You create the use cases you need and bind them together to the camera.

// You create the use cases you need:
val preview = Preview.Builder().build()
val imageCapture = ImageCapture.Builder().build()
// Later you bind them to a lifecycle and a camera.

Adding the Dependencies

CameraX ships as several artifacts. You add them to your module's build.gradle.kts. A typical setup pulls in the core library, camera2 backend, lifecycle integration and view helpers.

Keep all CameraX artifacts on the same version to avoid conflicts.

dependencies {
    val cameraxVersion = "1.3.4"
    implementation("androidx.camera:camera-core:$cameraxVersion")
    implementation("androidx.camera:camera-camera2:$cameraxVersion")
    implementation("androidx.camera:camera-lifecycle:$cameraxVersion")
    implementation("androidx.camera:camera-view:$cameraxVersion")
    implementation("androidx.camera:camera-video:$cameraxVersion")
}

Declaring Permissions

To use the camera you must declare the CAMERA permission in your AndroidManifest.xml. If you also record video, you may need RECORD_AUDIO.

These are runtime permissions — declaring them is not enough; you must also ask the user at runtime (covered in later lessons).

<!-- AndroidManifest.xml -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature
    android:name="android.hardware.camera.any"
    android:required="false" />

The CameraProvider

The entry point to CameraX is the ProcessCameraProvider. It connects use cases to the camera and to your screen's lifecycle.

You get it asynchronously with getInstance(), which returns a ListenableFuture. From Kotlin you can await() it inside a coroutine.

import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.core.ExperimentalGetImage

suspend fun Context.getCameraProvider(): ProcessCameraProvider {
    return ProcessCameraProvider.getInstance(this).await()
}

Binding Use Cases to a Lifecycle

Once you have the provider, you bind your use cases to a LifecycleOwner and a CameraSelector (front or back camera).

Because binding is lifecycle-aware, CameraX automatically opens the camera when your screen is visible and closes it when it isn't — no manual cleanup needed.

val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
    lifecycleOwner,
    cameraSelector,
    preview,
    imageCapture
)

Choosing a Camera

A CameraSelector tells CameraX which physical camera to use. The two most common are the default back and default front cameras.

You can also build a custom selector to require specific characteristics, but the defaults cover almost every app.

// Back camera (most common for photos)
val back = CameraSelector.DEFAULT_BACK_CAMERA

// Front camera (selfies, video calls)
val front = CameraSelector.DEFAULT_FRONT_CAMERA

// Toggle between them by swapping the selector and re-binding.

PreviewView: The Display Surface

CameraX renders the live preview into a PreviewView. It is a regular Android View that handles the surface, scaling and rotation for you.

In Jetpack Compose you embed it with AndroidView, which lets you place a classic View inside a composable. You'll wire this up fully in the next lesson.

AndroidView(
    factory = { context ->
        PreviewView(context).apply {
            scaleType = PreviewView.ScaleType.FILL_CENTER
        }
    },
    modifier = Modifier.fillMaxSize()
)

Lifecycle Awareness in Action

The biggest practical win of CameraX is lifecycle awareness. With Camera2 you had to manually open the camera in onResume and release it in onPause, and forgetting to do so caused crashes or a stuck camera.

With CameraX, once you bindToLifecycle, the framework handles all of that. When the user navigates away, the camera is released automatically; when they return, it reopens.

// No manual open/close needed.
// bindToLifecycle ties the camera to the screen's
// lifecycle, so it follows onStart/onStop for you.
cameraProvider.bindToLifecycle(lifecycleOwner, selector, preview)

Your Mental Model

Keep this picture in mind for the rest of the course:

  • Get a ProcessCameraProvider.
  • Build the use cases you need (Preview, ImageCapture, VideoCapture).
  • Pick a CameraSelector.
  • bindToLifecycle to connect everything.

Every CameraX feature is just a variation on these four steps.

Quick Check

Test your understanding of CameraX fundamentals.

Recap

You now understand the CameraX big picture:

  • CameraX wraps Camera2 with a simpler, more consistent, lifecycle-aware API.
  • It is built around use cases: Preview, ImageCapture and VideoCapture.
  • You add the CameraX artifacts, declare the CAMERA permission, get a ProcessCameraProvider and bindToLifecycle.
  • A CameraSelector chooses front vs back; a PreviewView displays the feed.

Next, you'll wire up a real live preview on screen.

Frequently asked questions

Is the “CameraX Overview” lesson free?

Yes — the full text of “CameraX Overview” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “CameraX Overview”?

Why CameraX over the old camera APIs. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “CameraX Overview” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. CameraX Overview
  2. Showing a Camera Preview
  3. Taking Photos
  4. Recording Video
← Back to Android Academy