Search Unity

  1. How has 2019.2 and the beta been for you so far? Give us feedback in this thread.
    Dismiss Notice
  2. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Integration Unity as a library in native Android app

Discussion in '2019.3 Alpha' started by PavelLU, May 27, 2019.

  1. PavelLU

    PavelLU

    Unity Technologies

    Joined:
    Feb 23, 2017
    Posts:
    36
    This post explains how to include Unity as a Library into native Android application 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:
      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”
        image4.png
    • Export UnityProject to androidBuild folder, the folder structure should look like this:
      image10.png
     
    Last edited: Jun 3, 2019
  2. PavelLU

    PavelLU

    Unity Technologies

    Joined:
    Feb 23, 2017
    Posts:
    36
    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:
        Code (CSharp):
        1. include ':unityLibrary'
        2. project(':unityLibrary').projectDir=new File('..\\UnityProject\\androidBuild\\unityLibrary')
        image3.png
    • Open build.gradle(Module: app)
      • add the following in dependencies block
        Code (CSharp):
        1. implementation project(':unityLibrary')
        image6.png
    • Open build.gradle(Project: NativeAndroidApp)
      • add the following in allprojects{repositories{ block
        Code (CSharp):
        1. flatDir {
        2.    dirs "${project(':unityLibrary').projectDir}/libs"
        3. }
        repoToUnityLib.png
    • Gradle files have been changed click Sync Now
      image1.png
    • If all went successfully you should see unityLibrary module added in Android view
      image5.png
     
    Last edited: Jun 20, 2019
    cativo23 likes this.
  3. PavelLU

    PavelLU

    Unity Technologies

    Joined:
    Feb 23, 2017
    Posts:
    36
    Project is ready
    Everything is ready to build, run and debug:
    image2.png
    If all went successfully at this point you should be able to run NativeAndroidApp:
    projectReady.png

    Please note after installation to device there will be to icons to launch application, remove <intent-filter>...
    </intent-filter> 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
     
    Last edited: Jun 21, 2019
  4. btristan

    btristan

    Joined:
    Oct 15, 2018
    Posts:
    38
    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)
     
    estradap likes this.
  5. estradap

    estradap

    Joined:
    Jun 6, 2017
    Posts:
    2
    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.
     
    btristan likes this.
  6. estradap

    estradap

    Joined:
    Jun 6, 2017
    Posts:
    2
    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
     
  7. PavelLU

    PavelLU

    Unity Technologies

    Joined:
    Feb 23, 2017
    Posts:
    36
    Thank you for submitting a bug report, we going to investigate the case and update/fix example/unity accordingly.
     
  8. varun_mani

    varun_mani

    Joined:
    Sep 18, 2018
    Posts:
    7
    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?
     
  9. PavelLU

    PavelLU

    Unity Technologies

    Joined:
    Feb 23, 2017
    Posts:
    36
    Expected, it is one app with to 2 <intent-filter><action android:name="android.intent.action.MAIN" />... 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.
     
  10. DirkDenzer

    DirkDenzer

    Joined:
    May 8, 2019
    Posts:
    5
    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.
     
    biemann and btristan like this.
  11. btristan

    btristan

    Joined:
    Oct 15, 2018
    Posts:
    38
    It got me too - hopefully this is in big, bold letters in the documentation. ;P
     
    DirkDenzer likes this.
  12. robin_notts

    robin_notts

    Joined:
    Apr 7, 2010
    Posts:
    86
    Any plans to add iOS support in the same way?
     
  13. DirkDenzer

    DirkDenzer

    Joined:
    May 8, 2019
    Posts:
    5
  14. biemann

    biemann

    Joined:
    Jun 28, 2019
    Posts:
    1
    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
    Code (CSharp):
    1. defaultConfig {
    2.         ndk {
    3.             abiFilters 'armeabi-v7a', 'x86'
    4.         }
    5.     }
    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
     
    Last edited: Jun 28, 2019
    boctorberk and estradap like this.
  15. aStrangeLoop

    aStrangeLoop

    Joined:
    Feb 17, 2018
    Posts:
    5
    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

    Code (CSharp):
    1. dependencies {
    2.     implementation fileTree(dir: 'libs', include: ['*.jar'])
    3.     compile files('libs/arcore_client.aar')
    4.     compile files('libs/unityandroidpermissions.aar')
    5.     compile files('libs/UnityARCore.aar')
    6. }
    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.
     
    Last edited: Jul 8, 2019