Google is committed to advancing racial equity for Black communities. See how.

Configure an ARCore session in Android NDK

Configure an ARCore session to build AR experiences for your app.

What is a session?

All AR processes, such as motion tracking, environmental understanding, and lighting estimation, happen inside an ARCore session. ArSession is the main entry point to the ARCore API. It manages the AR system state and handles the session lifecycle, allowing the app to create, configure, start, or stop a session. Most importantly, it enables the app to receive frames that allow access to the camera image and device pose.

The session can be used to configure the following features:

Verify that ARCore is installed and up to date

Before creating an ArSession, verify that ARCore is installed and up to date. If ARCore isn't installed, session creation fails and any subsequent installation or upgrade of ARCore requires an app restart.

// Verify that ARCore is supported and up to date.
bool isARCoreSupportedAndUpToDate(JNIEnv* env, void* context) {
  ArAvailability availability;
  ArCoreApk_checkAvailability(env, context, &availability);
  switch (availability) {
    case AR_AVAILABILITY_SUPPORTED_INSTALLED:
      return true;
    case AR_AVAILABILITY_SUPPORTED_APK_TOO_OLD:
    case AR_AVAILABILITY_SUPPORTED_NOT_INSTALLED:
      ArInstallStatus install_status;
      // ArCoreApk_requestInstall is processed asynchronously.
      CHECK(ArCoreApk_requestInstall(env, context, true, &install_status) == AR_SUCCESS);
      return false;
    case AR_AVAILABILITY_UNSUPPORTED_DEVICE_NOT_CAPABLE:
      // This device is not supported for AR.
      return false;
    case AR_AVAILABILITY_UNKNOWN_CHECKING:
      // ARCore is checking the availability with a remote query.
      // This function should be called again after waiting 200 ms to determine the query result.
    case AR_AVAILABILITY_UNKNOWN_ERROR:
    case AR_AVAILABILITY_UNKNOWN_TIMED_OUT:
      // There was an error checking for AR availability. This may be due to the device being offline.
      // Handle the error appropriately.
  }
}

Create a session

Create and configure a session in ARCore.

// Create a new ARCore session.
CHECK(ArSession_create(env, context, &ar_session_) == AR_SUCCESS);

// Create a session config.
ArConfig* ar_config = nullptr;
ArConfig_create(ar_session_, &ar_config);

// Do feature-specific operations here, such as enabling depth or turning on
// support for Augmented Faces.

// Configure the session.
CHECK(ArSession_configure(ar_session_, ar_config) == AR_SUCCESS);

Close a session

ArSession owns a significant amount of native heap memory. Failure to explicitly close the session may cause your app to run out of native memory and crash. When the AR session is no longer needed, call ArSession_destroy() to release resources.

// Release memory used by the AR session
ArSession_destroy(ar_session_);

Next steps