ARCore With Kotlin: Getting Started

Learn how to make augmented reality apps with Android using ARCore! By Zahidur Rahman Faisal.

Leave a rating/review
Download materials
Save for later
Share
Update note: Zahidur Rahman Faisal updated this tutorial for Android 7.0, Android Studio 3.5 and Kotlin 1.3. Joe Howard wrote the original.

These days, most people who own a smartphone have played augmented reality-based games like Pokémon GO or Harry Potter: Wizards Unite. Did you ever wonder how those apps augment a wonderful world around you and keep you engaged? Well, find out by making your own augmented reality (AR) app.

You can build augmented reality apps using OpenGL, Unity and Unreal. In this tutorial, you’ll get started with Android ARCore by building on top of a modified version of the OpenGL sample app that Google provides.

Note: This tutorial assumes that you’re familiar with Kotlin. If you’re just getting started with Kotlin development, please check out our tutorial, Kotlin For Android: An Introduction, first.

Introduction to ARCore

At WWDC June 2017, Apple announced ARKit, its foray into the world of AR development. Two months later, Google announced ARCore, which it extracted from the Tango indoor mapping project.

Tango only works on particular devices that have a depth sensor, while ARCore is available on most modern Android devices.

ARCore relies on three key mechanics to augment the real world around you:

ARCore tracks the pose of the virtual objects in a scene while your phone moves. This lets it render those objects from the correct perspective according to your position.

  • Motion tracking: ARCore determines both the position and the orientation of a virtual — simulated — object in the real world using the phone’s camera and sensor data. This is called the object’s pose.
  • Environmental understanding: ARCore can detect horizontal or vertical surfaces like tables, floors or walls while processing input from your device’s camera. Those detected surfaces are called planes. ARCore attaches virtual objects to a plane at a fixed location and orientation. Those fixed points are called anchors.
  • Light estimation: ARCore understands the lighting of the environment. It can then adjust the average intensity and color of virtual objects to put them under similar conditions as the environment that surrounds them. This makes the virtual objects seem more realistic.

The race to conquer this emerging technology is on, now more than ever. Get ready to explore the brave new — augmented — world like a Viking!

Getting Started

Did Vikings have cannons in reality? Whether they did or not, there’s no reason Vikings can’t have cannons in augmented reality! :]

Your goal for this tutorial is to augment a scene — a Viking pointing a cannon at a target — and project the scene around you using your Android device.

Start by downloading the project materials by clicking the Download Materials button at the top or bottom of this tutorial. Open the begin project in Android Studio 3.5 or later.

The project won’t compile right now. Don’t worry, it will in a while.

Setting up the Environment

Before you start building your scene, you need a device that’s capable of running ARCore. Here are two options; you can pick the one that’s right for you.

Using a Real Device

To run ARCore apps on a real device, you need to check if your device has ARCore support.

Google offers a full list of ARCore-supported devices. If you’re lucky and find your device on this list, you’re good to go!

Open Google Play Store from your device and install Google Play Services for AR. This service contains support libraries you need to run ARCore-based apps.

If your current device doesn’t support ARCore, don’t worry. You can still run it on the Android Emulator!

Using an Emulator

To use ARCore on an emulator, you need to create an Android Virtual Device (AVD) that can run ARCore apps. Give it the following configuration:

  • When creating your AVD, you must choose a Pixel, Pixel 2 or Pixel 3 hardware profile.
  • Set the system image to Oreo: API Level 27: x86: Android 8.1 (Google APIs) or a higher API Level.
  • Now, set the back camera to VirtualScene by navigating to Verify Configuration ▸ Show Advanced Settings ▸ Camera Back and picking VirtualScene from the drop-down.

Set Virtual Device Back Camera

Next, download Google Play Services for AR for your AVD from Github.

Boot up your AVD and drag the downloaded APK onto the running emulator.

You can also install the APK using the following command while the Virtual Device is running:

$ adb install -r Google_Play_Services_for_AR_1.14_x86_for_emulator.apk

Adding ARCore Dependencies and Permissions

Before you build and run the app, you need to add the dependencies and permissions ARCore needs. Open the app-level build.gradle file and add the following line to dependencies {...}:

implementation 'com.google.ar:core:1.14.0'

Click Sync Now in the top-right corner to sync your project and your dependencies will be in place.

Next, open AndroidManifest.xml and add the following after the manifest tag:

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

<uses-feature
  android:name="android.hardware.camera.ar"
  android:required="true" />
<uses-feature
  android:glEsVersion="0x00020000"
  android:required="true" />

This code adds the necessary permission and feature requests for ARCore.

Also, add the following just before the closing application tag:

<meta-data
  android:name="com.google.ar.core"
  android:value="required" />

This adds the ARCore metadata.

Now, you’re all set to launch your first ARCore app! Build and run the project.

You’ll see a prompt to provide camera permissions. After you give permission, you’ll see a radio group at the top. You’ll use that later to select the type of object to insert into the scene.

Searching for surfaces...

You’ll see a snackbar at the bottom indicating Searching for surfaces…. You may also see a few points highlighted, which means that the app is tracking them.

Aiming the device at a flat surface, you start detecting planes:

Detecting surfaces...

Once the app detects the first plane, the snackbar disappears and you’ll see the plane highlighted on the screen.

Note: ARCore uses clusters of feature points to detect the surface’s angle. Thus, you might have trouble detecting flat surfaces without texture or light-colored planes like a white wall.

At this point, the app doesn’t do much, but take some time to check out its code to get your bearings… especially before you set a Viking up with a cannon!