Integration Unity as a library in native Android app

** [!] Edited Sep 27, 2019. This example is outdated.** Since Unity version 2019.3.b4+ is out please refer to Version 2 of the example.

This post explains how to include Unity as a Library into native Android applications through Activities. (Just in case there is full project ready to build & deploy at the end of this doc ). You can read more about Unity as a Library .

Pre Requirements:

  • Android Studio 3.3.2+
  • Unity version 2019.3.a2+

1. Download & Uncompress

  • Unity project

  • This “Unity project” is the sample we want to include in our native Android app

  • Assets / Plugins / Android files have OverrideUnityActivity to extend UnityPlayerActivity

  • Native Android project

  • This is Basic Activity application from Android Studio templates where we want to include our Unity project

  • It have some UI, MainUnityActivity extends OverrideUnityActivity, and is prepared to start MainUnityActivity with Intent

  • For convenience uncompress both projects in same folder:
    4584565--427066--image8.png

2. Generate gradle project for Android platform

  • from Unity Editor open UnityProject

  • at Build Settings (Menu / File / Build Settings)

  • select and switch to Android Platform

  • select option “Export Project”

  • Export UnityProject to androidBuild folder, the folder structure should look like this:
    4584565--427075--image10.png

4 Likes

3. Add Unity Library module to NativeAndroidApp
Now we are going to add androidBuild/unityLibrary module to NativeAndroidApp gradle project in Android Studio:

  • open NativeAndroidApp in Android Studio

  • Open settings.gradle

  • At the end of settings.gradle file we add new project pointing to unityLibrary module:

include ':unityLibrary'
project(':unityLibrary').projectDir=new File('..\\UnityProject\\androidBuild\\unityLibrary')

  • Open build.gradle(Module: app)

  • add the following in dependencies block

implementation project(':unityLibrary')

  • Open build.gradle(Project: NativeAndroidApp)

  • add the following in allprojects{repositories{ block

flatDir {
   dirs "${project(':unityLibrary').projectDir}/libs"
}

2 Likes

Project is ready
Everything is ready to build, run and debug:


If all went successfully at this point you should be able to run NativeAndroidApp:

Please note after installation to device there will be to icons to launch application, remove …
from AndroidManifest.xml at unityLibrary to leave only integrated version.

Full Project
If you have issues following integration steps you can try out fully prepared project ready to build and deploy:
Download & Uncompress Full Project, open NativeAndroidApp in Android Studio

8 Likes

There is a problem with this example: the unload buttons actually quit the app instead of just destroying the activity. (tested on Android 5.1, API level 22)

3 Likes

We have observed that when the UnityPlayerActivity hosting the UnityPlayer object is destroyed, the UnityPlayer calls Process.killProcess(myPid) as part of it’s destroy() method. This causes our application to crash and restart since UnityPlayerActivity shares the same process as the application. Is there workaround for this behavior? We do not want to run the the UnityPlayerActivity as a separate process since makes integration with our application much more complicated. Can Unity add support for both types of activities? This would make the Unity library feature much more usable by a wide variety of Android applications, which we believe is the goal of this feature.

3 Likes

We have filed a bug report for the Unity Player crashing when performing ‘Unload’ in the NativeApp since the UnityPlayerActivity calls Process.killProcess instead of exiting cleanly: Case 1163573

1 Like

Thank you for submitting a bug report, we going to investigate the case and update/fix example/unity accordingly.

2 Likes

Tried this out right now and it seems to work great!
One weird thing I noticed… I see two “NativeApp” apps installed on my test android device when I build / deploy the NativeApp project from AndroidStudio.

When I tap on one of them, it launches the Native app correctly and I can go back and forth between Unity and the Android view. But if I launch the other NativeApp app, it launches me straight into the Unity view (i.e. with the spinning cube) and skips the whole Native app layer (obviously, I don’t see the native buttons in the middle as well). If I click on the Unity button to take me back to the MainView, I get a NullReference in showHostMainWindow probably beacuse the Main View does not even exist.

Dev / Test environment: Pixel 2 XL running Android 9 pie. I think I built both the UnityLibrary and the NativeApp using SDK API 28 (Pie). Not sure if this is related but when I tried to do the Gradle sync, after modifying the gradle scripts, it complained that the minSDK version of the NativeApp project was 16 while the minSDK version of the UnityLibrary was 19. So, I changed the NativeApp minSDK value to 19 to get it to build / deploy

Did I mess something up? Or is it expected that two apps get installed on the device, one native app and another the UnityLibrary?

1 Like

Expected, it is one app with to 2 … in AndroidManifest.xml from unityLibrary and another one from app module. For production last most likely should be removed as for this example we tried to keep bare minimum integration steps should be ok, but your right it is confusing doc updated.

Hi,

nice to see such a feature coming with Unity. Also a very nice and detailed explanation, thanks for that.
However I wanted to mention that I had to add a string to my strings.xml with name game_view_content_description in order to get it running.
If the string was not defined the UnityActivity crashed when starting it.

Thought I leave it here in case someone faces the same issue.

3 Likes

It got me too - hopefully this is in big, bold letters in the documentation. :stuck_out_tongue_winking_eye:

2 Likes

Any plans to add iOS support in the same way?

@robin_notts
I didn’t try yet but iOS should already be supported, see Integration Unity as a library in native iOS app

1 Like

Thanks for this integration.

To make it work on my own project, I needed in addition to add

<string name="game_view_content_description">Game view</string>

to my app’s strings.xml file

and

defaultConfig {
        ndk {
            abiFilters 'armeabi-v7a', 'x86'
        }
    }

to my app build.gradle file.
Otherwise, the app crashed with following error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp/com.unity3d.player.UnityPlayerActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0

2 Likes

Not sure if this is the place to ask, but where could I find more information about combining this with AR Foundation? (supposedly one of the use-cases of Unity as a library). I managed to put my own game into the example app provided here without encountering any problems, but I’m now trying to put the AR example explained here: https://www.youtube.com/embed/ml9qVRdEH4k into the android app and I’m encountering the following error:

ERROR: Unable to resolve dependency for ':unityLibrary@debug/compileClasspath': Could not find :arcore_client:

Can anyone here give me any guidance on how to fix this, or what to google?

EDIT:

I managed to resolve the error by changing:
The dependencies in unityLibrary’s build.gradle to

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/arcore_client.aar')
    compile files('libs/unityandroidpermissions.aar')
    compile files('libs/UnityARCore.aar')
}

Now it compiles can open the app, but crashes on pressing the show unity button with the following error No implementation found for void com.unity3d.player.UnityPlayer.nativeRestartActivityIndicator() (tried Java_com_unity3d_player_UnityPlayer_nativeRestartActivityIndicator and Java_com_unity3d_player_UnityPlayer_nativeRestartActivityIndicator__)

EDIT:

Switching to IL2CCP, and turning off Instant Run in Android Studio resolves all error and lets me use launch the AR Core example, very nice.

2 Likes

I only want to use Audio of Unity in Delphi. Will it be possible with Unity 2019.3a2 to compile a library that I can use in an Android project for Delphi? And IOS? And which banner of Unity will I have to include with the Community edition?

Hi, I tested the example project and it worked correctly.

Now I’m testing a Unity project with Vuforia exported as an Android Library, and I can’t make it work.

When I launch it from an Android Project it crashes.

2019-07-31 12:10:53.171 17975-17975/com.unity.mynativeapp E/Unity: Unable to find main
2019-07-31 12:10:53.209 17975-17975/com.unity.mynativeapp I/HwPointEventFilter: do not support AFT because of no config
2019-07-31 12:10:53.226 17975-17975/com.unity.mynativeapp E/art: No implementation found for void com.unity3d.player.UnityPlayer.nativeRestartActivityIndicator() (tried Java_com_unity3d_player_UnityPlayer_nativeRestartActivityIndicator and Java_com_unity3d_player_UnityPlayer_nativeRestartActivityIndicator__)
2019-07-31 12:10:53.228 17975-17975/com.unity.mynativeapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.unity.mynativeapp, PID: 17975

Has anyone tried to use a Vuforia Project as a Library?, if so what’s missing?

Thanks.

2 Likes

I am working with XCode. Can you provide an example for this, please?

Can you provide an example for Unity Integration with Swift?

ERROR: Unable to resolve dependency for ‘:unityLibrary@debug/compileClasspath’: Could not find :android_component:.
Show Details
Affected Modules: unityLibrary

ERROR: Failed to resolve: :core-1.1.0:
Affected Modules: unityLibrary

ERROR: Unable to resolve dependency for ‘:unityLibrary@debug/compileClasspath’: Could not find :unity_android_bridge:.
Show Details
Affected Modules: unityLibrary

ERROR: Unable to resolve dependency for ‘:unityLibrary@debugAndroidTest/compileClasspath’: Could not find :android_component:.
Show Details
Affected Modules: unityLibrary

ERROR: Unable to resolve dependency for ‘:unityLibrary@debugAndroidTest/compileClasspath’: Could not find :unity_android_bridge:.
Show Details
Affected Modules: unityLibrary

ERROR: Unable to resolve dependency for ‘:unityLibrary@debugUnitTest/compileClasspath’: Could not find :android_component:.
Show Details
Affected Modules: unityLibrary

ERROR: Unable to resolve dependency for ‘:unityLibrary@debugUnitTest/compileClasspath’: Could not find :unity_android_bridge:.
Show Details
Affected Modules: unityLibrary

ERROR: The minSdk version should not be declared in the android manifest file. You can move the version from the manifest to the defaultConfig in the build.gradle file.
Remove minSdkVersion and sync project
Affected Modules: unityLibrary

WARNING: The targetSdk version should not be declared in the android manifest file. You can move the version from the manifest to the defaultConfig in the build.gradle file.
Remove targetSdkVersion and sync project
Affected Modules: unityLibrary

I got this errror while adding my game scene