Unity On Android Studio

Working on Unity Project on Android Studio

Export Unity Project with below options

Export file location as <Unity Project>\Export\

Unity Studio

Create Android Interaction

Back button to exit app

Create new C# script and attach to Main Camera or GameObject. This will exit from Android app on back button press

void Update()
{
  if (Application.platform == RuntimePlatform.Android)
  {
   if (Input.GetKeyDown(KeyCode.Escape))
   {
      //AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
      //activity.Call<bool>("moveTaskToBack", true);
      Application.Quit();
   }
  }
}

 

Android Studio

1. Import Unity Project as Module

Import Unity exported package as Modules in Android Studio

  • Create a new Android Project
  • Import Unity exported android project as module
File -> New -> Import Module -> <Unity Project>\Export\MyUnityModule
  • Add dependency to the main project
File -> Project Settings -> app -> Dependencies tab -> + -> Module Dependencies -> select imported module "MyUnityModule"

This will give below line in apps build.gradle (Module: app)
compile project(‘:MyUnityModule’)

Avoid creating multiple Icons on Device (AndroidManifest.xml)

Comment below lines in imported module AndroidManifest.xml for avoid creating multiple launcher icons on device.

<!--<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>-->

Separate Unity from Android Process (AndroidManifest.xml)

Seperating Unity activity as process to avoid quitting the entire application on Unity Quit “Application.Quit()”

<activity
    android:name="com.test.app.UnityPlayerActivity"
    android:process=":UnityProcess"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:screenOrientation="portrait">

Convert Module as library (build.gradle)

Make imported module as library to avoid creating apk files conflicting main apk.

File: build.gradle (module: MyUnityModule)

Change application to library as below

apply plugin: 'com.android.library'

comment applicationId line

//applicationId 'com.spicedroid.unity.MyUnityModule'

 

 Correct Gradle Version (build.gradle)

Installed gradle versions can be found in the below path

C:\Program Files\Android\Android Studio\gradle\m2repository\com\android\tools\build\gradle

Copy gradle version from project gradle file build.gradle(Project:MyProject) to module gradle build.gradle (module: MyUnityModule)
Make sure it matches like below lines

dependencies { classpath 'com.android.tools.build:gradle:2.3.3' }

2. Add Additional plugin AAR dependencies

Importing AAR library for any module dependencies

Check if any module dependencies mentioned in modules build.gradle (module: MyUnityModule) file like below then import that aar file like above similar approach

compile(name: 'MyUnityPlugin', ext:'aar')

Unity Plugins AAR can be found under  below path

<Unity Project>\Assets\Plugins\Android\MyUnityPlugin.aar

Now add Plugin Module “MyUnityPlugin” like below

File -> New -> New Module -> Select "Import .Jar/AAR package" -> <Unity Project>\Assets\Plugins\Android\MyUnityPlugin.aar

 

Then provide aar module as dependency to imported module “MyUnityModule”

File -> Project Settings -> MyUnityModule -> Dependencies tab -> + -> Module Dependencies -> select imported module "MyUnityPlugin"

This will add below line in build.gradle (Module:MyUnityModule). Comment existing compile line
// compile(name: ‘MyUnityPlugin’, ext:’aar’)
compile project(‘:MyUnityPlugin’)

Resolve Icon Conflicts (AndroidManifest.xml)

In app AndroidManifest.xml, add tools:replace to resolve icon and theme conflicts

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.yourpackage.name">
<application
        android:allowBackup="true"
        tools:replace="android:icon,android:theme"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        ...
    </application>
</manifest>

Build And Run

Now finally do a clean build and run project

Build Project

Build -> Clean Project

Run on Device

Shift + F10