Overhead of calling a method from .aar plugin?

I have created a native android plugin aar and have imported it into Unity. I’m going to use it for Bluetooth communication to control a real RC car using Arduino. Doing so will require me to call a java a method every time a UI control like a joystick in Unity is moved.

Like for example,

 AndroidJavaClass javaOb = new AndroidJavaClass("com.eg.comp.myApp.ExampleClass");
 javaObj.CallStatic("inputX", "200");  // Here Im calling the method InputX with the joystick value

The above call will happen multiple times when the user moves the UI controls. I want to know what is the overhead of calling native methods like this? Will it cause FPS drop in my Unity app with nothing but just some small UI controls?


This shouldn’t be too expensive. However it’s not the cheapest thing. Obviously you should cache your AndroidJavaClass and not create a new one each frame. Apart from that the AndroidJavaClass is just a wrapper of the more low level JNI interface wrapper. To avoid any memory allocation and overhead, it would probably be better to grab the actual method reference manually in Start. Also you really shouldn’t use string parameters.

So you could use the AndroidJavaClass to get the class reference. Then you can use the GetRawClass method to get the actual class reference. Now you can use AndroidJNI.GetStaticMethodID with the class reference we just got, the method name and the “parameter definition string”. With the returned reference you can directly call AndroidJNI.CallStaticVoidMethod. You should also cache the jvalue array that you need to pass the parameter(s).

As i said previously you shouldn’t use a string as parameter. Strings are objects and requires an actual string object to be allocated on the java side. You also would need to allocate a new string each time on the C# side. Declare the method on the Java side with a float, double or int parameter instead. This allows the most efficient data transfer.

To sum up it should look something like this:

using System;

// [...]

private AndroidJavaClass exampleClass;
private IntPtr exampleClassPtr;
private IntPtr inputXMethod;
private jvalue[] argumentList;

void Start()
    exampleClass = new AndroidJavaClass("com.eg.comp.myApp.ExampleClass");
    if (exampleClass == null)
        throw new Exception("Can't find native Java class 'com.eg.comp.myApp.ExampleClass'");

    exampleClassPtr = exampleClass.GetRawClass();
    if (exampleClassPtr == IntPtr.Zero)
        throw new Exception("Native Java class 'com.eg.comp.myApp.ExampleClass' not initialized / not found");

    inputXMethod = AndroidJNI.GetStaticMethodID(exampleClassPtr, "inputX", "(F)V");
    if (inputXMethod == IntPtr.Zero)
        throw new Exception("Can't locate static method 'inputX' in native java class");
    argumentList = new jvalue[1];

void CallInputX(float aValue)
    if (inputMethod == IntPtr.Zero)
    argumentList[0].f = aValue;
    AndroidJNI.CallStaticVoidMethod(exampleClassPtr, inputXMethod, argumentList);

Note that this assumes that your inputX method now takes a float argument. So it’s defined in Java as

void inputX(float someName)
    // [ ... ]

If you want to use a different argument type, you need to change a few things. First of all the method signature string need to be changed to the type you want to use. F = float, D = double, J = long, I = int. Likewise you have to use the correct “member” of tje jvalue when you assign your parameter. Unity named them just like the signature letter, but with a lower case letter. So argumentList[0].d for double or argumentList[0].i for an int value.

Of course I haven’t tested this code example. Though you may want to look up some general JNI documentation like the method signature format table. Be warned if you want to actually use a string parameter there’s no way around a per frame memory allocation. Also you have to be super careful with native string allocation and deallocation. Messing something up with JNI can instantly crash your application and the errors are in most cases pretty useless.

Since the AndroidJavaObject / AndroidJavaClass class is not very well documented you may want to have a look at the implementation of those to understand what they actually do under the hood.

Just realised that you have to use “GetStaticMethodID” instead of GetMethodID since we have a static method and not an instance method. I’ve fixed the answer.