Android Application Development: How to build ARCore Android App
In my previous article, I have talked about installing the android studio in the system and the simple Hello World application in android. Today, we are going to talk about how we can develop the AR android application using Sceneform Android Library. So, let’s dig into it.
Step 1: Create a new project
You can refer my previous android article that how you can create a new project.
Note:
Select the Java programming language.
Choose the ‘MINIMUM SDK’ as ‘API 24: Android 7.0 (Nougat)’
Step 2: Getting the 3D Model
Sceneform 1.17.0 supports only glTF files. Now .glb files are the binary versions of the GL Transmission Format. These types of 3d models files are used in VR, AR because it supports motion and animation.
In the android studio, Right-click on the res directory. Go to New > Android Resource Directory. Change the Resource Type to Raw: Click OK. A raw folder will be generated in the res directory. You have to place the glb file in the raw directory.
- You can create 3d model by yourself in the computer graphics software and save it in .glb format.
- You can download the .glb model from the Google Poly website or from sketchfab. I have used the gladiator .glb file from the sketchfab.
Step 3: Downloading and Setting up SceneForm 1.16.0.
For developing the AR app in android Sceneform SDK is required.
- Download the “sceneform-android-sdk-1.16.0.zip” file.
- Extract the ‘sceneformsrc‘ and ‘sceneformux‘ folders in your project folder.
- Go to the Android Studio
- Go to Gradle Scripts > settings.gradle (Project Settings)
include ‘:sceneform’
project(‘:sceneform’).projectDir = new File(‘sceneformsrc/sceneform’)
include ‘:sceneformux’
project(‘:sceneformux’).projectDir = new File(‘sceneformux/ux’)
After that go to Gradle Scripts > build.gradle (Module:app). Add this line inside the dependencies block.
api project(“:sceneformux”)
After all, these changes click ‘Sync Now‘. Then go to the app > manifests > AndroidManifest.xml. Add these lines before the “application” block
<!--This permits the user to access Camera-->
<uses-permission android:name="android.permission.CAMERA" />
<!--This helps to check a specific feature in the phone's hardware,
here it is OpenGlES version. Sceneform needs OpenGLES Version 3.0 or later-->
<uses-feature android:glEsVersion="0x00030000" android:required="true" />
<!--Indicates that this app requires Google Play Services for AR.
Limits app visibility in the Google Play Store to ARCore supported devices-->
<uses-feature android:name="android.hardware.camera.ar" android:required="true"/>
After that add this line before the “activity” block.
<!-- ARCore need to be installed, as the app does not include any non-AR features.
For an "AR Optional" app, specify "optional" instead of "required".-->
<meta-data android:name="com.google.ar.core" android:value="required" />
Step 4: Some Error Correction
Currently, android development is supporting the androidx library. Sceneform SDK is built on the andoid compat library which is not supported longer anymore. So you have to go migrate the SDK to androidx by going to Refactor > Migrate to AndroidX.
Step 5: Working with layout file.
Go to the res > layout > activity_main.xml file. Here is the code of that XML file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/fragment"
android:name="com.google.ar.sceneform.ux.ArFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 6: Working with coding part
package com.example.androidar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;
import com.google.ar.core.Anchor;
import com.google.ar.sceneform.AnchorNode;
import com.google.ar.sceneform.rendering.ModelRenderable;
import com.google.ar.sceneform.ux.ArFragment;
import com.google.ar.sceneform.ux.TransformableNode;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
private ArFragment arFragment;
private int clickNo = 0;
public static boolean checkSystem(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String openGlVersion = ((ActivityManager) Objects.requireNonNull(activity.getSystemService(Context.ACTIVITY_SERVICE))).getDeviceConfigurationInfo().getGlEsVersion();
if (Double.parseDouble(openGlVersion) >= 3.0) {
return true;
} else {
Toast.makeText(activity, "App needs OpenGl Version 3.0 or later", Toast.LENGTH_SHORT).show();
activity.finish();
return false;
}
} else {
Toast.makeText(activity, "App does not support required Build Version", Toast.LENGTH_SHORT).show();
activity.finish();
return false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkSystem(this)) {
arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
Objects.requireNonNull(arFragment).setOnTapArPlaneListener((hitResult, plane, motionEvent) -> {
clickNo++;
if (clickNo == 1) {
Anchor anchor = hitResult.createAnchor();
ModelRenderable.builder()
.setSource(this, R.raw.gladiador)
.setIsFilamentGltf(true)
.build()
.thenAccept(modelRenderable -> add(anchor, modelRenderable))
.exceptionally(throwable -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Somthing is not right" + throwable.getMessage()).show();
return null;
});
}
});
}
}
private void add(Anchor anchor, ModelRenderable modelRenderable) {
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
TransformableNode model = new TransformableNode(arFragment.getTransformationSystem());
model.setParent(anchorNode);
model.setRenderable(modelRenderable);
model.select();
}
}
That’s it you have done very good work. Now it’s time to install the app on your android. Click on the run button. It will start compiling code. If you are lucky then error won’t come.
Output:
I have created the app you can download it from here. OR you if you want to do the coding, I have created a git repository clone it to your machine and open the project in android studio and run it. It will install on your android device.