How to code Augmented Reality Mobile Application with Android ARCore SDK — Part 1

Atul Sharma
47Billion
Published in
3 min readJan 21, 2021

Augmented Reality vs Virtual reality

Augmented Reality(AR) combines a view of the real world with computer-generated elements in real-time. It provides a different way to interact with the real world. It creates a modified version of our reality, enriched with digital information on the screen of our computer or mobile screen. Merging or combining the virtual and the real provides a new range of user experiences.

Virtual reality (VR) is a simulation created on a computer. It is an artificially created real-life environment. It isolates users from the real world. It makes the user feel as if he or she is experiencing simulated reality.

AR implementation in Mobile Apps

Android provides ARCore SDK and Apple provides ARkit for building augmented reality experiences in android and IOS.

ARCore provides API’s to enable the mobile phone to sense its environment, understand the world and interact with information. Some of the APIs are available across Android and iOS to enable shared AR experiences.

ARCore uses three key capabilities to integrate virtual content with the real world :

  • Motion tracking uses phone sensors to understand and track its position relative to the world.
  • Environmental understanding detects the size and location of different surfaces: horizontal, vertical, and angled surfaces, etc.
  • Light estimation estimates the environment’s current lighting conditions.

How to setup ARCore and Sceneform

Using Android studio we create a new project:

In build.Gradle we add the following dependencies to add support of ARcore and sceneform SDK:

implementation 'com.google.ar:core:1.16.0'
implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.15.0'
implementation 'com.google.ar.sceneform:core:1.15.0'
implementation "com.google.ar.sceneform:animation:1.15.0"

As Sceneform requires java 8 features so we need to add compileOptions in our build.Gradle:

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

Add Sceneform plugin in the build.gradle at the top:

apply plugin: 'com.google.ar.sceneform.plugin'

Gradle will only know the plugin only if we add classpath on project level Gradle file:

classpath 'com.google.ar.sceneform:plugin:1.15.0'

now we have to syn the project.

Configure Manifest

we have to provide Camera permission and specify glEsVersion in Android.manifest

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

we have to specify the minimum required opengl version (which is 3) and also specify it is required feature and without this our app can’t run

<uses-feature android:glEsVersion="0x00030000" android:required="true" />

to tell android this is an AR app we need to add

<uses-feature android:name="android.hardware.camera.ar" />

we have to add meta data to tell our app requires google play services for AR and without that our will not run, so it automatically installed when user downloads the app.

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

Adding AR fragment

in res folder create layout activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:name="com.google.ar.sceneform.ux.ArFragment"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

and in main activity load this ARFagment

class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Run the code

Run the application on the device. If it launches the camera with a hand overlay it means all the setup is done correctly and you are good to go.

Originally published at https://47billion.com on January 21, 2021.

--

--