Android Augmented Reality – Android ARCore Example

Filed Under: Android

In this tutorial, we’ll be discussing Augmented Reality and implement a hello world sample in our android application using Sceneform.

Augmented Reality

Augmented Reality is gaining more popularity day by day. It’s used to show you virtual 3d models/graphics in a real world through your mobile’s camera. It’s useful in demonstrating simulated stuff in the real world such as paints/decoration you’d like to see on your wall.

Augmented Reality lies somewhere between Virtual Reality and Real environment.

Android ARCore

Google has come up with ARCore library to enhance AR development.

ARCore does motion tracking in the real world through the camera to create paths on planes/surfaces over which we can place our 3D Models and graphics.

SceneForm is a 3D framework that’s come up recently and is a better alternative to OpenGL. OpenGL looks intimidating to code.

SceneForm allows us to quickly render 3d objects without the need to learn graphics or OpenGL.

We can download the Google Sceneform Tools plugin in our Android Studio to view and render the 3d models.

You can go to and download a sample model. Don’t forget to credit the creator!

Typically, the OBJ and GLTX formats are used for rendering augmented images.

Now let’s build our first AR application where we’ll use the above 3D model as our AR image.

Augmented Reality Android Example Project Structure

Augmented Reality Android App Example

In the above project, we’ve created a sampledata directory, where we add the obj and mtl file downloaded earlier.

Add the following dependency in your project’s build.gradle:

classpath ''

Add the following in your app’s build.gradle :

implementation ''

You need to set the ar plugin in your app’s Gradle file as well.
Add the following below the dependencies:

apply plugin: ''

We have used the following image, you can download it from this link and put it in the sampledata directory:
android poly google image

Now right click on the obj file and choose import sceneform assets:
android sceneform import path

This would automatically create the Renderable AR sceneform assets. It would automatically add the following lines at the end of the app’s build.gradle:

sceneform.asset('sampledata/Coffee Cup_final.obj',
        'sampledata/Coffee Cup_final.sfa',
        'src/main/assets/Coffee Cup_final')

The build.gradle of the app looks like this finally:
android ARCore app build gradle

Android ARCore Sceneform requires Java 8 or higher version.

The sja and sjb are the Sceneform Asset Description and Sceneform Binary files.

The sjb file is visible in the 3D viewer. It is shipped with the APK.

The sja file is used to set properties for the sjb file.

You can change the scale of the model in the sja file.

Android Augmented Reality Example Code

To configure ARCore in your application add the following permission and metadata to your AndroidManifest.xml file.

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

metadata goes inside the application tag.

            android:value="required" />

The code for the activity_main.xml class is given below:

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""


            app:popupTheme="@style/AppTheme.PopupOverlay" />


    <include layout="@layout/content_main" />

        app:srcCompat="@android:drawable/ic_input_add" />


The code for the content_main.xml is given below:

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""

        app:layout_constraintTop_toTopOf="parent" />


We’ve set the fragment as ArFragment. The ArFragment checks if the phone is compatible with ARCore.

Also, it checks if the camera permission is granted. If it isn’t, it’ll ask for it automatically.

Also, it’ll ask you to download the ARCore by Google Application.

ARCore is currently supported in this list of devices.

The code for class is given below:

package com.journaldev.androidarsceneform;

import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;


import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

public class MainActivity extends AppCompatActivity {

    ArFragment arFragment;

    protected void onCreate(Bundle savedInstanceState) {
        Toolbar toolbar = (Toolbar) findViewById(;

        FloatingActionButton fab = (FloatingActionButton) findViewById(;
        fab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                addObject(Uri.parse("Coffee Cup_Final.sfb"));

        arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(;

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == {
            return true;

        return super.onOptionsItemSelected(item);

    private void addObject(Uri parse) {
        Frame frame = arFragment.getArSceneView().getArFrame();
        Point point = getScreenCenter();
        if (frame != null) {
            List<HitResult> hits = frame.hitTest((float) point.x, (float) point.y);

            for (int i = 0; i < hits.size(); i++) {
                Trackable trackable = hits.get(i).getTrackable();
                if (trackable instanceof Plane && ((Plane) trackable).isPoseInPolygon(hits.get(i).getHitPose())) {
                    placeObject(arFragment, hits.get(i).createAnchor(), parse);

    private final void placeObject(final ArFragment fragment, final Anchor createAnchor, Uri model) {
        ModelRenderable.builder().setSource(fragment.getContext(), model).build().thenAccept((new Consumer() {
            public void accept(Object var1) {
                this.accept((ModelRenderable) var1);

            public final void accept(ModelRenderable it) {
                if (it != null)
                    MainActivity.this.addNode(arFragment, createAnchor, it);
        })).exceptionally((new Function() {
            public Object apply(Object var1) {
                return this.apply((Throwable) var1);

            public final Void apply(Throwable it) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                AlertDialog dialog = builder.create();
                return null;

    private void addNode(ArFragment fragment, Anchor createAnchor, ModelRenderable renderable) {

        AnchorNode anchorNode = new AnchorNode(createAnchor);
        TransformableNode transformableNode = new TransformableNode(fragment.getTransformationSystem());

    private Point getScreenCenter() {
        View vw = findViewById(;
        return new Point(vw.getWidth() / 2, vw.getHeight() / 2);

The anchor is the position where the model or the Node is placed in the screen. It’s the center of the screen here.

In the addObject() the ARFragment gets the hit points on the plane using motion tracking.

placeObject() asynchronously sets the node to the center of the screen.

Following are the outputs when the application is launched:

android ARCore example

android augmented reality example app

augmented reality in android apps example

In the final output, the cup is positioned on the plane which consists of white dots.

The above screenshot is from Samsung S8.

Now try changing the model scale in SFA file!

This brings an end to this tutorial. You can download the final AndroidArSceneform Project from the link below.

The size of the project is big, thanks to the OBJ file.

You can also download the android studio project from our Github Repository.


  1. Avishek Roy says:

    Sir how would I create a Google AR Core based app to be available for all or at least 80% of models available in India..

  2. jinkal says:

    I ‘m trying to clicked on th obj file but did not showing import sceneform assets

  3. Joan Camilo Olmos Durán says:

    Hello, I would like to know if you could make an explanatory video on how to use images as markers for augmented reality in android studio, because I do not find much information on how to do it through android studio and if you can make the video using the latter version of android studio because it could not do it with the latest version.

  4. imran akhtar says:

    hello sir, I want to know how to make AR measure application as it is in demand. kindly upload the tutorials for AR Measure application.

  5. Tinto says:

    Getting the below error. 3D model is not showing

    ARCore-LocationProvider: Exception during FLP api client construction:

  6. Natnael Adane says:

    Hello sir … I would like to thank you for your all works you are amazing. I want to ask you one question please do a tutorial on Location-based Augmented Reality that mark the place
    Thank you

  7. vibhuti patel says:

    i’m facing problem your resource linking failed error is showing.

  8. Joel says:

    Maybe you can still help me, if you see this at all.
    What I wanted to do was , instead of using the button ,I wish to something when I click the 3d model itself.
    When I try to do what you did to the button to a node I always get an error

    1. mdshahim says:

      I want to place an object without tapping on a particular location how to place ?

  9. dheeraj kumar says:


    i have seen your tutorials for how to use AR core but your code is not working properly for detect image with obj file

  10. Lynn Myat Minn says:

    Hello Anupam Chugh,
    I am newbie to ARCore, Sceneform and I’m looking for some tutorial or sample of targeting image with buttons (maybe buttons are floating just like in Sceneform Solar System Sample). I hope you can help me! Thanks for your great tutorial sir.

  11. krutika says:

    Not working

    1. Anupam says:

      Can you elaborate on the error you’re getting?

  12. pradeep trivedi says:

    hello sir i am facing error Coffee Cup_Final.sfb file not found , how to solve please guide me

    1. captain says:

      i modified in assets the file to coffe.sfb, and it worked

        1. Gayatri says:

          I ‘m trying to right click on th obj file but did not showing import sceneform assets.
          I’m using Android Studio 4.1 .

          There one more problem :
          Plugin Error: Plugin “Google Sceneform Tools (Beta)” is incompatible (supported only in IntelliJ IDEA).

          Thanks in Advance

          1. udit says:

            Some of the plugins are incompatible with the latest version of Android Studio. Try the above mentioned way. It worked for me. I hope it works for you too!

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors