7 Steps to integrate Google ARCore Unity project to a native Android app

Rishiraj Randive
4 min readOct 28, 2017

I am working on an Augmented Reality project for one of my courses at San Jose State University. We are using Google ARCore with Unity to develop the project but we also want to integrate this Unity project into a native Android app to use some of the android-specific functionalities in the project.

So for the task of integrating Unity project into Android app, I struggled for some time. I found some helpful blogs or articles but I had to collect bits and pieces from different sources. Also, after doing all this, I was facing some issues which I’ll describe below.

This will give you a step by step guide for integrating Google ARCore based Unity project into a native Android app, the prerequisite for this guide is:

  1. Should have completed the basic Google ARCore tutorial for Unity.
  2. Know about Android development.

Let’s start

Step 1:

Export your Google ARCore Unity project

If you have completed the Google ARCore tutorial for Unity you should be familiar with build settings, if not now is the best time to do it. Below are the build settings you need to specifically pay attention to, apart from settings shown in Google’s tutorial:

  1. Change Texture compression to ETC2
  2. Change Build system to Gradle
  3. Open Player settings -> Other settings
  4. Change Install location to Automatic (I am not completely sure why other options didn’t work for me, but I think it’s best not to specify the location)

Screenshot for reference:

Settings for exporting the Google ARCore Unity project

Step 2:

Now import the exported project in Android Studio

If you are using Android Studio 3.0, select Import Project option and browse and point to the exported Unity project. You might get a popup complaining about Gradle wrapper, click Ok.

Gradle build might show error shown in screenshot below, which we will be fixing in next steps.

Error you might see as soon as you import the project

Step 3:

Change the imported Android project to library

  1. Update the Gradle to latest (I used com.android.tools.build:gradle:3.0.0-rc2)
  2. Comment the <intent-filter> lines from AndroidManifest.xml as this library will be invoked from other Android activity. Shown below:
Commenting the <intent-filter> in manifest file

Finally, in build.gradle file:

  1. Change apply plugin: ‘com.android.application’ to apply plugin: ‘com.android.library’
  2. Remove applicationId under defaultConfig

Step 4:

Assemble the project and generate the .aar file

Find the Gradle task for assemble and click on it. Once done two .aar files (release and debug) will be generated under build/outputs/aar.

Step 5:

Create a new native Android project and include the .aar

  1. Once you have created a new Android project, create a new folder under src/mains/ called libs.
  2. Copy the release .aar file generated in Step 4 to src/mains/libs
  3. Add dependency in module level build.gradle file like shown below(don’t sync yet)

compile(name: ‘your-aar-file-name’, ext: ‘aar’)

Add below line to project level build.gradle file for successfully compiling the library (now you can sync)

flatDir {dirs ‘src/main/libs’}

Step 6:

Resolve merger issue

After Gradle sync in Step 5, you might get an error

Manifest merger failed with multiple error, see logs

Add tag to AndroidManifest.xml file:

xmlns:tools=”http://schemas.android.com/tools"

And below line in <application> tag AndroidManifest.xml

tools:replace=”android:icon,android:theme”

Step 7:

Start UnityPlayerActivity

Then wherever you want your Unity project to kick in you can start the UnityPlayerActivity intent from your package. (While importing you will see classes from two packages, use the one from package you created while exporting the project from Unity).

Troubleshooting

Ideally, after following all the 7 steps your app should run fine on your device but the app might crash and in logs, you might see an error like this:

Google ARCore Error: java.lang.ClassNotFoundException: Didn’t find the class “com.unity3d.unitygar.GoogleAR”

For some strange reason the .aar file didn’t take care of its libraries, so I copied the libraries from exported Unity project’s (folder) to new Android project src/main/libs folder. And then, add these lines to include these libraries in your build.

compile(name: ‘libtango_3d_reconstruction_api’, ext:’aar’)

compile(name: ‘lighting_estimation’, ext:’aar’)

compile(name: ‘unityandroidpermissions’, ext:’aar’)

compile(name: ‘unitygar’, ext:’aar’)

Recommended to Clean and Rebuild it.

Note: If I am doing something wrong or unnecessary please feel free to comment and add your suggestions.

--

--