Step-by-Step guide for developing Android Plugin for Unity3D (I)

31 Dec

This series of post outlines the development of Android plugins for Unity3D. I had been asked to develop Android plugins for Unity3D every few days or months, each time following similar paths which I think would be useful to those who will do the same job. And here is how.

Clone the source code in GitHub here !

Tools you need:

  • Android Studio
  • Unity3D
  • Android Devices for testing
  • Some patience to go through this post

Let’s get started.

The 1st example is extremely simple. We define a function DoSthInAndroid() that dump some information using Java Log.i() function, and get this function called in Unity3D. Though simple enough, it outlines all the required procedures, and are very useful to understand the entire workflow.

  • Start Android Studio, create an Android project (AndroidAddin) with an Empty Activity.
  • In Android Studio, create another library project (AndroidLib) by clicking the menu File > New > New Module


  • Select “Android Libray” in the wizard


  • Build the project, make sure everything works so far.


In the AndroidLib module, add a class called “Helper”


In the Helper class add a function DoSthInAndroid()

    package com.androidaddin.androidlib;
    import android.util.Log;
    public class Helper {
    public static void DoSthInAndroid()
        Log.i(“Unity”, “Hi, Sth is done in Android”);

To make sure this work, in the main app, add below testing function:

protected void onCreate(Bundle savedInstanceState) {

Helper.DoSthInAndroid(); // Testing

And when you run the Android App, you can see the output in Logcat window:

… 15:59:57.276 12121-12121/com.androidaddin.androidaddin I/Unity: Hi, Sth is done in Android

If you explicitly build the AndroidLib project only, you will find below output:


The aar file is the compiled android archive or the binary distribution of an Android Library Project.

Rename the *-debug.aar as zip file, unzip the file, you will see a file called “classes.jar” which contains only java code files (classes):


Grab the classes.jar for later use in Unity3D.

Open Unity3D, Create a project, then create a folder in Assets: Assets\Plugins\Android

Add the classes.jar to the above Assets\Plugins\Android folder


Create a new MonoBehaviour script TestAndroidPlugin.cs, and attach it to the camera:

public class TestAndroidPlugin : MonoBehaviour {	
    void Start () {
        Debug.Log("Android function is to be called");

        var ajc = new AndroidJavaClass("com.androidaddin.androidaddin.Helper"); //(1)
        ajc.CallStatic("DoSthInAndroid");                                       //(2)
        Debug.Log("Android function is called");  

The AndroidJavaClass offers access to Java class object, and the CallStatic fires the java object’s method. Simple?

Save the scene, add the scene in the build setting dialog, remember to change the BundleID. Build the Unity app and run it on your device, you will see in the Logcat window what you expect.


Yeah! We have stepped the most important stride in developing Android plugins for Unity3D. There are more cool stuff than calling a log() function in Unity3D, we can do quite a lot such as adding Android UI (button, image, textview, progress bar etc.) to Unity scene, which will be discussed in a separate post!

To summarize:

  1. You need define something in Android java which is to be called in Unity3D C#;
  2. You need compile the java class into a jar or aar file (shall talk about this in directly using aar in another post);
  3. You need put the compiled jar/aar in a special folder (Assets\Plugins\Android), well not always! See my others posts in this blog!
  4. You need call the java function with the aid of AndroidJavaClass or AndroidJavaObject class which is defined in Unity3D.

UPDATE: In Unity3D 5.0+, you no longer need unpack the aar file and manually get jar file.
UPDATE: In Unity3D 5.0+, you no longer need put the file in Plugins\Android folder, anywhere in the Asset folder will do!.

You might feel cumbersome in renaming the aar –> zip –> copy files to Unity3D –> Build app etc. Yes, this is indeed tedious! In the following posts, I shall continue to propose solutions to automate this process, and everything is fine!

Here is the 2nd part of this tutorial series: Step-by-Step guide for developing Android Plugin for Unity3D (II)

Enjoy and  happy new year! See you in 2016! Clone the source code in GitHub here !



Posted by on December 31, 2015 in Programming, Unity 3d


Tags: , , , , , , ,

9 responses to “Step-by-Step guide for developing Android Plugin for Unity3D (I)

  1. cuongkimh4

    April 28, 2016 at 11:39 am

    I follow your step to (In the AndroidLib module, add a class called “Helper”). But i can’t create C class, just only can create C++ class so i can’t continue. Can u fix my problem?

  2. Hamza Lazâar

    May 5, 2016 at 8:24 pm


    Thanks for this first post.
    I’m waiting for part 2 though as this part is relatively trivial.
    There aren’t enough resources on the internet about how to make a Unity plugin for Android without extending main UnityActivity. I’m looking for best practices, tips or advice from someone else’s experience.
    The idea is that you need to handle the lifecycle of a 2nd Activity (call start, process intents, etc.) but I did not find the best way to return to UnityActivty…I tried calling finish on the other Activity…I want to avoid all the static methods and properties also if that’s possible.
    I’m also interested in your automated process of copy/pasting files to Unity folders from Android Studio?


  3. xinyustudio

    May 5, 2016 at 8:33 pm

    Hi Hamza, sorry for the delay, I will try to update this asap.

  4. bhavani

    July 25, 2016 at 2:16 pm

    Hi. Is this possible to integrate android studio layouts to unity?. If possible please help me with any links.

  5. Kashif Tasneem

    August 8, 2016 at 1:28 pm

    Hello. If I need context, what should I do?

  6. xinyustudio

    August 8, 2016 at 1:50 pm

    AndroidJavaClass jc = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
    AndroidJavaObject jo = jc.GetStatic(“currentActivity”);
    jo will be the context you need!

  7. Kashif Tasneem

    August 8, 2016 at 5:54 pm



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: