Image for post
Image for post
Gustavo Giardi | Data unwrapper, Technology | everis Brasil

How to integrate augmented reality into native Android apps

The game engine Unity has proven to be one of the best platforms for developing Augmented Reality experiences. Its graphic interface, readiness to work with 3D models and easy integration with Vuforia, ARCore and ARKit provide an ideal environment for developing apps based on these technologies.

However, this engine has also caused issues for customers. Since it was developed for game design, Unity is not the best choice for developing common apps. A company that wants to integrate an AR experience into its app rarely develops solutions with Unity, making it difficult to integrate an Augmented Reality scene into an existing app. Asking end users to download a second app to access an Augmented Reality environment is not an option either, as it would take up more space on the device’s memory and result in a poor experience.

These issues are now addressed by integrating Unity scenes with Android apps. This way, the entire AR experience is contained in the main app that has already been developed, allowing end customers to see Augmented Reality interfaces on a supermarket app, bank app, insurance app, or any other one used by them. Customers do not need to download another app to benefit from solutions that use this technology, which makes it more popular and increases the reach of its benefits.

Preparing the Unity project

The process of integrating Unity scenes into a native Android app consists of exporting the Unity project as if it were a library that is consumed by the main app. To achieve that, perform the following settings:

· Set Texture Compression to ETC2

· Check the “Export Project” checkbox.

Image for post
Image for post

This allows Unity to create a folder with the editable project in Android Studio. However, before creating that project, prepare the Unity module so that it may receive some parameters from the main app. Since we can only include one Unity project in the final product, it may be necessary to create an initial scene in charge of calling the desired scene afterwards. Create the following script for an object of the first scene:

Image for post
Image for post

This script is in charge of receiving the name of the desired scene that will be passed as a parameter by the main app and of calling the Unity scene of the same name. To pass other parameters, such as a user ID or app-specific settings, pass the parameters the same way.

After the Unity project has been exported, open it in Android Studio to set it up as a library that is consumed by the main app. In Android Studio, we import the project to perform some edits:

Image for post
Image for post

These editions are important to adapt the project that needs to be consumed by the main app. First, go to “build.gradle” and define the project as a library:

Image for post
Image for post

After that, delete the lines that define the App ID and Bundle:

Image for post
Image for post
Image for post
Image for post

Finally, in Android Manifest, delete the Intent Filter:

Image for post
Image for post

All settings are now ready. To apply these edits, you need to create two different builds, the first one to apply the build. Gradle changes and the other one to export the edited project. The *.aar files of the Unity scenes and Augmented Reality SDK *.aar files are found in the project folder:

Image for post
Image for post

Location of the *.aar file of the Unity scenes

Image for post
Image for post

Location of the Augmented Reality SDK *.aar files

These are the files you need to add the scenes developed with Unity to the native Android app. Importing these libraries into the main project allows you to call up scenes by passing parameters from one app to another.

Integrating the library with the native Android project

Open the main app project and in Android Studio, go to File > New > New Module > Import JAR/AAR Package and import the above-mentioned *.aar files. In “build.gradle”, add the following dependency:

Image for post
Image for post

This allows you to access the Unity scene through the Android app. To call it up, use the following intent, where Scene_Name is the name of the desired scene in Unity:

Image for post
Image for post

To conclude, the Unity scenes must be defined as a new process in Manifest.xml, so that they may return to the main app once they have been completed:

Image for post
Image for post

To return to the Android app, call the “Application.Quit ()” method in Unity.

In short, to integrate an Augmented Reality scene with a native Android app, export the Unity project, make it a library and add it to the main project. However, some factors should be taken into account during that process:

· You cannot use ARCore and Vuforia in the same project. Their libraries will conflict with each other even if your AR experiences are in separate scenes. Therefore, you’ll need to define which of these technologies will be used at the beginning of the development.

· You cannot integrate more than one Unity project into an Android app. The two libraries conflict with each other in the project, which does not allow combining scenes. To solve that issue, include all Augmented Reality scenes in a single project. When the main app calls the Unity scene, the same initial scene always opens first (from build index = 0). We therefore add a script that calls another scene from the same project, taking into account a parameter sent by the Android app. Thus, the main app defines which AR scene to start.

Application

Integration of Augmented Reality scenes with an Android app was used in a project developed by everis and presented at CIAB 2019. This app was intended to guide users at the company stand as it presented cases of how various technologies developed by the team are used.

While the Android app was in charge of the login flow, app currency management and face recognition features, the integrated Unity scenes provided AR experiences without the need to install a second app on a phone.

Image for post
Image for post

Using parameters that were passed to the Android app, Unity scenes were able to access user information and thus provided a customized experience and customer data management during the AR experience.

Exponential intelligence for exponential companies

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store