How to create a Native Android Plugin for Unity


Building a Unity plugin for Android in Java is pretty straightforward, although if you have never done it, this is what you need to know.

Assuming that you have Unity installed on your machine and you have already started a project, download and install the Android SDK with the ADT Bundle, this also contains Eclipse which will be used to create the JAR file.

– Set up the project

Open up Ecplise and create a new Android Application Project, name it as you wish, make sure to use the same package name you used in Unity Player Settings.

* Just as in Unity:

And include the “classes.jar” that you can find in this folder:

“C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androiddevelopmentplayer\bin\classes.jar”

To include a JAR right click on the project and click “Properties” then go to “Java Build Path” tab “Libraries” and click on “Add External JARs…

– Create the bridge

This is the bridge class, in this example there is a method with an int return and another method that shows how to handle parameters injection and callback to Unity.

To generate a JAR file out of this file, right click on it, select “Export” then export as “JAR file”.

– Unity side

In Unity create a folder in Assets named Plugins and a subfolder in Plugins named Android (Assets/Plugins/Android), in this last one put the JAR file you just created.

In the same folder create an AndroidManifest.xml as described below:

Also create a C# script and attach it to the Main Camera.

This script calls a method on click passing some parameters, this call back a method in Unity which then calls again a method in Java to return an integer.

You will not have to go this route every time, although I wanted to expose all the possibilities.

It should look like this:

In the above script there is a GUI Text that displays the message, simply create a Game Object Gui Text and drag it in the Script field using the Unity Inspector.

That’s all, if everything went fine you should be able to click Build and Run in Unity and see the app on your phone.


15 thoughts on “How to create a Native Android Plugin for Unity

  • Tevfik

    Hi great tutorial,
    But i have a question. You created a Bridge class and extended it from UnityPlayerActivity.
    What if i want to also used the Main.java, more than one class and some Intent and Service classes, then how i should convert them to jar? I tried like Properties -> Android-> Is library checked and create a jar but when i add it to Unity, it crashes. Can you help me on this?

  • Juanjo

    I tried your code and it showed two errors:

    1. At NativeBridge.cs, line 23: Cannot implicitly convert type int' to string’. I fixed it this way:

    //string myInt = androidClass.CallStatic(“getMyInt”);
    guiText.text = “My Int: ” + androidClass.CallStatic(“getMyInt”);

    2. Same file, line 34. There is a missing “object” after “new”.

  • JD

    Hello,

    thanks for the tutorial…its very helpful to understand how this work…I have just started learning unity and android. I have a question. and its about how to pass a video stream to an android plugin, run faceDetecter analysis on it, that is built into android, and return the position back to unity. or there is a better way of doing it…please guide me ..would really appreciate it.
    thanks.

  • JD

    Also i have run your tutorial…i understand the idea but i dont see the numbers getting printed on the mobile device…it just keep displaying GUI Text…please let me know if i have done something wrong here..thanks,,waiting for your reply =)

  • ikhazen

    hello, thanks for this great tutorial.
    now I’d like to ask if this native plugin can access the android camera or picking photos from gallery using unity3d?

    thanks

  • hieudev

    im combining GooglePlayGamesServices and AdMob plugin with a new plugin that calls a share-text intent. but a i can make it to work. GPGS an Admob run right but not the shartext plugin.

    To do the plugin i followed several tutorials on internet and yours to. i think i´ve made the same things you say here but i don´t realize wheres my fault. maybe compatibility between multiple android-java plugins.

    i must say that i’ve erased all duplicated classes.

    is there a known solution?

  • lesiry

    Assets/NativeBridge.cs(34,34): error CS0029: Cannot implicitly convert type int[]' to object[]’
    Assets/NativeBridge.cs(34,41): error CS0826: The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly
    Assets/NativeBridge.cs(23,32): error CS0029: Cannot implicitly convert type int' to string’

  • Peter

    Hi, your tutorial looks pretty good, but I have a problem in last script. Could you help me alittle bit? Thanks

    Assets/Plugins/Android/NativeBridge.cs(23,32): error CS0029: Cannot implicitly convert type int' to string’

    Assets/Plugins/Android/NativeBridge.cs(34,41): error CS0826: The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly

    Assets/Plugins/Android/NativeBridge.cs(34,34): error CS0029: Cannot implicitly convert type int[]' to object[]’

  • Gabi

    Hello Lorenzo,

    I am very interested in this topic. I have a game made in Unity and would like to make automated tests using MonkeyTalk, but for that I need a bridge with Android. I tried the steps described by you but it doesn’t seem to work.

    The one thing that stands out to me is that I get errors on NativeBridge.cs. For example on this:
    string myInt = androidClass.CallStatic(“getMyInt”);
    This is saying string = int so obviously it won’t work.

    Do you have a tested version for these files? Or could you help me with some advice?
    At the moment, I get an error on
    androidClass = new AndroidJavaClass(“com.hello.world.UnityBridge”);
    saying androidClass is instantiated with NULL.

    Thank you for your time,
    Gabi

  • gulshan ara

    hi,
    i also want to do something like this and everything is working fine in test project where there is only one activity by the name of .UnityBridge but when i tried to integrate it in my own application i already have UnityPlayerProxyActivity, UnityPlayerActivity and UnityPlayerNativeActivity where UnityPlayerProxyActivity has Main Action like this

    so how can i integrate both? .UnityBridge activity and UnityPlayerProxyActivity? because if i remove UnityPlayerProxyActivity my application stops working and if i remove Main Action from .UnityBridge Activity then my code of eclipse is not called and communication between unity and eclipse will be stopped.
    any solution for this scenario?

Leave a comment

Your email address will not be published. Required fields are marked *