Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Merging Unity Project Into Native Android Application

Discussion in 'General Discussion' started by darshanpv, Apr 12, 2019.

  1. darshanpv

    darshanpv

    Joined:
    Feb 1, 2019
    Posts:
    7
    AR (Augmented Reality) is becoming popular in enterprises , there is a need of integrating AR experience built using Unity into Native Android Application that has been already developed.

    If you are looking to add Unity Player activity into your existing Android project, this post is for you.

    The steps given are simple and straight forward and should not take much time to merge the Unity project into your current android project.

    My assumption here is that
    - You have already built an AR experience using Unity ( let us call it as UnityApp)
    - You want to call this UnityApp from within your (AndroidApp) - Native android app . (say with a click of "ARButton" in MainActivity)
    Follow below mentioned steps
    - Go to "Player Setting" of UnityApp project and change your package name to that of your AndroidApp
    - Click checkbox "Export Project" in your "Build setting". Click "Export" button so that Android code is available. You are going to merge UnityApp code manually into your existing native AndroidApp
    - Copy unity-classes.jar , UnityAds.aar , VuforiaWrapper.aar (if you are using Vuforia) from /libs folder of UnityApp to /libs folder of AndroidApp.
    - Copy jniLibs folder from UnityApp to AndroidApp ( in /src/main folder)
    - Copy assets folder from UnityApp to AndroidApp (in /src/main folder)
    - Go to your Android Studio , open AndroidApp , right click on to your project , New -> Activity -> Empty Activity (uncheck "generate layout File" option). Name it as UnityPlayerActivity activity. Uncheck Backwards Compatibility option and click Finish.
    - Overwrite UnityPlayerActivity.java from UnityApp to your AndroidApp

    Now that you have UnityPlayerActivity created its time to change AndroidMainfest.xml file.

    - AndroidApp AndroidMainfest file will need set of additional parameters which we will copy from UnityApp Manifest file
    - Add all the meta-data information from UnityApp Manifest file to AndroidApp Mainfest file in "application" section. (sample example below)
    Code (CSharp):
    1. <meta-data
    2.             android:name="unity.build-id"
    3.             android:value="1420ed4b-5a1c-4e60-84f9-9cefe66ebea0" />
    4. <meta-data
    5.             android:name="unity.splash-mode"
    6.             android:value="1" />
    7. <meta-data
    8.             android:name="unity.splash-enable"
    9.             android:value="True" />
    - Add use-features and use-permission to main section of Manifest file
    - Add following tags from UnityApp Manifest file to <activity> section of UnityPlayerActivity in AndroidApp Mainfest
    - theme (android:theme="@StyLe/UnityThemeSelector" to be copied from application section)
    - configChanges
    - hardwareAcclerated
    - launchMode
    - screenOrientation
    - any other parameters based on your UnityApp project that are reflected in your Manifest file.​
    - Make changes in style.xml of AndroidApp to accommodate Unity theme styles as per style.xml of UnityApp

    Now next step is to make necessary changes in build.gradle (module level) of AndroiApp

    Please note that Unity generates just one build.gradle which we will use to copy necessary parameters into module level build.gradle
    - Add repositories, lintOptions ,aaptOptions, buildTypes, packagingOptions and bundle to android section (sample below)
    Code (CSharp):
    1. repositories {
    2.         flatDir {
    3.             dirs 'libs'
    4.         }
    5.     }
    6.  
    7.     lintOptions {
    8.         abortOnError false
    9.     }
    10.  
    11.     aaptOptions {
    12.         noCompress = ['.unity3d', '.ress', '.resource', '.obb', 'vuforia/creditcard.dat', 'vuforia/creditcard.xml']
    13.     }
    14. buildTypes {
    15.         debug {
    16.             minifyEnabled false
    17.             useProguard false
    18.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
    19.             jniDebuggable true
    20.         }
    21.  
    22.         release {
    23.             minifyEnabled false
    24.             useProguard false
    25.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
    26.             signingConfig signingConfigs.debug
    27.         }
    28.     }
    29.     packagingOptions {
    30.         doNotStrip '*/armeabi-v7a/*.so'
    31.         doNotStrip '*/x86/*.so'
    32.     }
    33.     bundle {
    34.         language {
    35.             enableSplit = false
    36.         }
    37.         density {
    38.             enableSplit = false
    39.         }
    40.         abi {
    41.             enableSplit = true
    42.         }
    43.     }
    44.  
    - Add
    ndk { abiFilters 'armeabi-v7a', 'x86' }
    to defaultConfig section
    - Add following implementation to dependencies section
    Code (csharp):
    1. implementation(name: 'UnityAds', ext: 'aar')
    2. implementation(name: 'VuforiaWrapper', ext: 'aar')
    3.  
    Now you will add click button event to "ARButton" in MainActivity so that you can invoke the Unity activity with click of this button. Sample code below
    Code (CSharp):
    1. ARButton.setOnClickListener(new View.OnClickListener() {
    2.             @Override
    3.             public void onClick(View v) {
    4.                 Toast.makeText(MainActivity.this, "Opening Unity 3D", Toast.LENGTH_LONG).show();
    5.  
    6.                 Intent unityIntent = new Intent(MainActivity.this, UnityPlayerActivity.class);
    7.                 MainActivity.this.startActivity(unityIntent);
    8.             }
    9.         });
    That's All !! Get ready now to build your project and test !!​
     
    Last edited: Apr 12, 2019
    FaiyazOfficial likes this.
  2. zimmer550king101

    zimmer550king101

    Joined:
    May 26, 2021
    Posts:
    9
    does not work, I get error that libmain.so could not be found
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,554