Blogs

How to Create an ARCore App Using Kotlin

BLOG by 

CitrusBits
February 10, 2020
#XR #VR #UX #UI

This is a step-by-step guide for creating a basic ARCore app with Kotlin using Android Studio, with a little bit of an Introduction on ARCore and Kotlin. We start with knowing what ARCore is, moving to Kotlin and then the tutorial. In the end we compiled some questions to explore what a developer’s perspective would be on ARCore and Kotlin.

What is ARCore

ARCore is a software development kit (SDK) by Google. It is a developer’s tool for creating augmented reality apps on Android.

But it doesn’t end at that. In fact, there are many sequels to it. Not too long ago – Feb’ 6th of 2020 to be exact – version 1.15.0 of ARCore was released by Google. What does this latest launch mean?

It means that now some of the devices will now be able to support some additional and exciting features. For instance, some devices will be able to yield new supported camera configs and that too with a GPU texture resolution lower than that of the device’s default GPU texture resolution.

Exciting right?
With previous versions there were some limitations involved but Google is known to ‘always’ have some neat tricks up its sleeve and never disappoints.

Now, if you are hearing the word ‘ARCore’ for the first time, let me shine some light on the matter.

A Walk Down the Memory Lane

For Google to jump the AR bandwagon and be able to compete with Apple on their Augmented Reality platform (ARKit), something big was missing. Tango was the initial intended AR breakthrough for Google, but didn’t quite pan out that well.
To fill this void, Google created ARCore, a competitor to ARKit, by using data from the Tango Project, and today it’s doing better than great.

Apps like Pokemon Go and IKEA Palace are prime examples of what augmented reality apps look like.

If you want to know more about Google’s ARCore, check out this article here.

Why Use Kotlin with ARCore

Kotlin is a statically typed programming language that runs on a Java Virtual machine (JVM). It is an officially supported programming language on Android Studio 3.0 or above besides Java.

So what makes Kotlin so exciting as a new language and that too, why try it out with ARCore?

  1. Because it’s new, and we are always excited about trying new tech, finding better and innovative ways of doing things.
  2. Kotlin has some really amazing features like the inbuilt ‘null safety’ – Goodbye Null pointer exceptions!
  3. The code is so concise! Compare java code with Kotlin code and you will see the difference.

These are just a few reasons that surfaced at the top of my mind and should be enough to get you started on a Kotlin-fied Augmented Reality journey.

Before we begin, lets find out:

What Do You Need To Get Started With ARCore Using Kotlin

First thing is first, you need Android studio 3.0 or above – because this version of Android has full support for Kotlin. You might get lucky with Android 2.3.3 but you will have to download the Kotlin plug-in yourself and test your luck.

We tested with Android 3.0.1 but it did not have emulator support for ARCore. So we tried 3.1 beta 4 version and it worked perfect with the ARCore sample app running on a virtual Pixel device. (Read ahead to know why you would need an emulator and a virtual device).

Also, we used to a Mac device to create this tutorial, things will be a little different on a windows based device.

If you have Andoid Studio installed on your machine, skip the following steps.

Step 1: Locate Downloaded file on your System

Step 2: Run the file and wait for the setup to finish.

Step 3: You should be prompted with this screen.

Step 4: Click Android Studio and you should arrive at Android Setup Wizard. Let it download the required SDK components.

Step 5: Choose the Installation type

It is best to go with the standard option, unless you REALLY know what you want in your Android Studio, because if you don’t then this will become a huge nuisance when you start to develop.

Step 6: Choose Your UI – This is just the look and theme of your Android Studio. You can even change this after the installation is complete.

Step 7: Let the Android magic begin – Note: You might be prompted to let Android change some setting, just type in your password and open security preferences for Android.

Congratulations! You have successfully downloaded and Installed Android Studio on your device.

Download ARCore SDK for Android Studio

ou can either download it from google or clone the repository from Github. You need this SDK for utilizing AR elements and features.So you will download this on your device.

Step 1: Search for ‘ARCore’ and you will see Google’s Official ARCore page.

Step 2: Find Resources and click on the downloads option.

Step 3: Donwload ARCore SDK for Android Studio.

If you want to clone it from Github – Follow on till step 2 and click on the Github link.

And now to the bane of all that is ARCore.

For creating an augmented reality app, you need an ARCore supported smartphone.

Google, originally, limited ARCore to a handful of Google pixel and Samsung galaxy smartphones BUT promised to bring ARCore to a million Android devices soon.

The promise of ‘SOON’ was met at the Mobile World Congress and the ‘million devices’ so to speak, boiled down to a few more handful devices.

Following is the list of ARCore supported devices.

Google Phones

  • Pixel
  • Pixel XL
  • Pixel 2
  • Pixel 2 XL

Samsung Phones

  • Galaxy S8
  • S8+
  • Note8
  • S7 edge

LG Phones

  • V30
  • V30+ (Android O only)

Asus Phones

  • Zenfone AR

OnePlus Phones

  • OnePlus 5

If you don’t have one of the above mentioned devices, here are two ways you can work this around.

Try following this guide on how to use ARCore on unsupported devices OR Run your app in an emulator.

Here is How you setup an Emulator in Android Studio

Step 1: Find ‘Preference’ option.

Step 2: On the side panel, Find ‘System Settings’ and in that you will find Android SDK.

Click on Android SDK and find Google APIs Intel x86 Atom System Image”. If it is checked with a blue tick, that means it is already installed, if not you need to install it. Click on the box and press OK. I am adding other required SDk files as well. Make sure you have all of these installed.

In addition to the emulator, you will also need to setup a virtual device. Its only logical (pun intended) to use an ARCore supported device as a virtual device. Scroll to the list above.

How to setup a Virtual Device

Step 1: Click Tools, navigate to Android and in that find ‘AVD Manager’. You will see this screen.

Step 2: When you click on ‘Create Virtual Device’, Android Studio shows you a list of devices of which you can use to create a virtual environment.

Since we are creating an ARCore app with Kotlin, Choose an ARCore supported device. We chose Pixel.

Step 3: Next choose the OS for the virtual device.

Step 4: Name your Virtual Device

Step 5: Finalize and Verify.

Step 6: One last thing. Before you finalize, navigate to Verify Configurations/ Show Advanced Settings and make sure you have the option checked for Camera back as a VirtualScene.

This is the very option that allows the emulator to use the camera feature for ARCore related tasks.

At this point, you have successfully created a virtual Pixel 2.

You can follow the same steps to create any other virtual device.

Almost there, covering the part where you get your mobile device ready.

If you have a supported device, you don’t need to download anything. All ARCore services come integrated with the device. However, if you are using an unsupported device, you will need the modified version the Google ARCore service files.

You can get those from HelloAR and tango service.

You would require some basic level knowledge of OpenGL.

Why?

Well, sadly ARCore does not have its own library for modeling 3D/2D objects.

So you need OpenGL to create 3D/2D objects, without which there will be no AR objects in your app. So some basic understanding of using OpenGL will do you great.

Think of OpenGL as ARKit’s Scenekit equivalent for ARCore.

As of this writing, ARCore is compatible with OpenGL ES 2.0 – 3.x.

Although this sample app doesn’t require you to write OpenGL code but in the future when you are creating your own AR objects, the understanding you develop here will become your stepping stone for more advanced tasks.

With this, lets conclude the pre – reqs and actually start on creating our Arcore app.

Creating an ARCore Sample App with Kotlin

Open your Android studio and create a project with Blank activity and with Kotlin support enabled.

Once you are done, you should arrive at a Project like this:

This is MainActivity.kt file.

This is activity_main.xml file.

First of all, I would suggest you fix you build.gradle files.

In your project’s build.gradle, i.e build.gradle (project: *YOUR PROJECT NAME*)

Add the following to the code

mavenLocal()

maven { url “https://oss.sonatype.org/content/repositories/snapshots/” }

and Sync your project.

Now move to the next gradle file i.e build.gradle(Module: app)

Make sure this line is included as part of the dependencies:

implementation ‘com.google.ar:core:1.0.0

implementation ‘com.android.support:design:27.0.2’

And sync your project once again.

Once the syncing finishes, you will get this warning. This basically prompts you for a version change since you added the above mentioned line of code.

Just match the versions, and Sync again (If prompted) and you are good to go.

I would suggest removing the TextView in the center, as we have no use for it. Navigate over to the

Activity_main.xml file and remove the TextView Tag like so.

Now add your string messages, that would appear when:

  1. The app requires camera permission
  2. We are trying to detect the surface
  3. The app needs to notify that this is an unsupported ARCore device

Navigate over to the strings.xml file and add the lines like so:

<string name=“camera_permission_toast”>Camera permission is needed to run this application</string>

<string name=“searching_for_surfaces”>Searching for surfaces…</string>

<string name=“ar_device_support”>This device does not support AR</string>

Now, Go to the Androidmanifest.xml and add these permissions

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

<uses-feature android:name=”android.hardware.camera.ar” android:required=”true”/>
<uses-permission android:name=”android.permission.CAMERA”/>

Adding those strings will make more sense now. These ‘permissions’ are what prompt the user to let the app use the required hardware or software functionalities. Like using the camera, accessing contacts etc.
We, in this tutorial, are using permissions for

  1. Using the camera (hardware and software)
  2. Google’s ARCore services

Now you need to Go to the assets folder and add your object files.

Your assets contain objects and everything related to them. From the shading to the shadows, shape, texture everything. These are models converted to OpenGL.
You will add these from the ARCore sample that came with the ARCore SDK you downloaded.
This will be the path directory inside the ARCore SDK folder

arcore-android-sdk-v1.0.0 > samples > hello_ar_java > app > src > main > assets >

Now we are going to convert a java file to kotlin. In the sample folder, find ‘CAMERPERMISSIONHELPER CLASS‘
Path directory:

arcore-android-sdk-v1.0.0 > samples > hello_ar_java > app > src > main > java > com > google > ar > core > examples > java > helloar >

Before we do that, first create a kotlin class with the same name inside your own project.
Navigate over to https://try.kotlinlang.org/
Click on Convert from Java.
A new Window will open. Copy code from the sample ‘CAMERA PERMISSION HELPER CLASS’ file and Paste it on the left side (JAVA side) and click on Convert to Kotlin.
Copy the converted code in your Android Project in the class you just created.

You will also see a folder named rendering inside the ARCore SDK folder. Create a folder with the same name and the same classes. Convert the code from java to Kotlin in the same way. Make sure you have the same directory structure like so.

This just shows the rendering folder with Shader.Utill class. Add all the classes from the ARCore SDK sample rendering folder to your own app folder.
For convenience, I am listing their names below

  • BackgroundRenderer.java
  • ObjectRenderer.java
  • PlaneRenderer.java
  • PointCloudRenderer.java
  • ShaderUtil.java

You might get some errors and warnings after converting code like this. You will need to resolve those. Simply hover over to the errors and incorporate the appropriate suggestions Android Studio shows you. Pretty easy right?
Build and compile, and your ARCOre app with Kotlin should look like this.

About the Author

CitrusBits

Content Writer

Lorem ipsum dolor sit amet consectetur. Odio ullamcorper enim eu sit. Sed sed sociis varius odio vitae viverra. Eu sapien at vitae vulputate tortor massa semper vel. Lectus sed gravida blandit lorem consequat erat integer non ut. Morbi amet dui cras posuere venenatis. Laoreet sapien lacus sit sit elementum risus massa auctor. Enim ornare pharetra quis massa fusce. Nibh vitae in erat ut mollis erat. Amet cursus ut sem condimentum ultrices. Felis morbi malesuada sit amet ultrices at ut consectetur.

Newsletter

Let’s stay in touch