Sitemap

Enable ARCore -AR Required in AR apps -Part 1

3 min readApr 4, 2019

--

There are two types of AR apps: AR Required and AR Optional.

We are discussing here AR Required which required minimum API level 24. Apps that are not usable without AR are referred to as AR Required apps.

  • The Play Store makes your app available only on devices that support ARCore.
  • When users install an AR Required app, the Play Store automatically installs ARCore. However, your app must still perform additional runtime checks in case ARCore is later uninstalled or an updated version of ARCor

For more information, see Publishing AR Apps in the Play Store.

Create an android studio project with minimum API level 24.

Get Akshay Kumar S’s stories in your inbox

Join Medium for free to get updates from this writer.

To declare your app to be AR Required, modify your AndroidManifest.xml to include the following entries:

uses-permission android:name="android.permission.CAMERA" />

<!-- Indicates that app requires ARCore ("AR Required"). Ensures app is only
visible in the Google Play Store on devices that support ARCore.
-->
<uses-feature android:name="android.hardware.camera.ar" />

<application>
…
<!-- Indicates that app requires ARCore ("AR Required"). Causes Google
Play Store to download and install ARCore when the app is installed.
-->
<meta-data android:name="com.google.ar.core" android:value="required" />
</application>

Add build dependencies

To add ARCore to your Android Studio project perform these steps:

  • Make sure your project’s build.gradle file includes Google's Maven repository:
allprojects {
repositories {
google()
  • Add the latest ARCore library as a dependency in your app’s build.gradle file:
dependencies {
implementation 'com.google.ar:core:1.8.0'
}

Request camera permission

AR Required apps must ensure that the camera permission has been granted before creating an AR Session. The hello_ar_java sample includes a CameraPermissionHelper class which can be copied into your project and should be called from your activity's onResume() method:

// ARCore requires camera permission to operate.
if (!CameraPermissionHelper.hasCameraPermission(this)) {
CameraPermissionHelper.requestCameraPermission(this);
return;
}

Your AR activity must also implement onRequestPermissionsResult(…), as seen below:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
if (!CameraPermissionHelper.hasCameraPermission(this)) {
Toast.makeText(this, "Camera permission is needed to run this application", Toast.LENGTH_LONG)
.show();
if (!CameraPermissionHelper.shouldShowRequestPermissionRationale(this)) {
// Permission denied with checking "Do not ask again".
CameraPermissionHelper.launchPermissionSettings(this);
}
finish();
}
}

Check whether ARCore is installed

Apps must also call ArCoreApk.requestInstall() before creating an ARCore session, to check whether a compatible version of ARCore is installed. This will prompt the user to install or update ARCore if needed.

// Set to true ensures requestInstall() triggers installation if necessary.
private boolean mUserRequestedInstall = true;
private Session mSession;
@Override
protected void onResume() {
super.onResume();

// Check camera permission.
…

// Make sure ARCore is installed and up to date.
try {
if (mSession == null) {
switch (ArCoreApk.getInstance().requestInstall(this, mUserRequestedInstall)) {
case INSTALLED:
// Success, create the AR session.
mSession = new Session(this);
break;
case INSTALL_REQUESTED:
// Ensures next invocation of requestInstall() will either return
// INSTALLED or throw an exception.
mUserRequestedInstall = false;
return;
}
}
} catch (UnavailableUserDeclinedInstallationException e) {
// Display an appropriate message to the user and return gracefully.
Toast.makeText(this, "TODO: handle exception " + e, Toast.LENGTH_LONG)
.show();
return;
} catch () { // Current catch statements.
return; // mSession is still null.
}
}

This flowchart illustrates the logic in the preceding code sample:

Press enter or click to view image in full size

If requestInstall() returns INSTALL_REQUESTED, the current activity pauses and the user is prompted to install or update ARCore from google play store.

You can find the sample project here and the next part of this article here.

--

--

Akshay Kumar S
Akshay Kumar S

Written by Akshay Kumar S

Android | Java | Kotlin | Flutter | Dart | .Net Core