** [!] 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 .
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')
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
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)
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.
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
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?
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.
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.
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
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.
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?
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