Creating Unity Android plugin

I am currently trying to make a Unity plugin for Android via fmod Core Api.
Is it possible to use the core API in Android Studio?

Because I am making a c++ library, it means I won’t use the fmod.jar file. I have succesfully made an aar file by linking the fmod .so files and the header files included in the core dowload to my own library. In the library i have also written a simple function to test if the plugin works:

int test()
{
FMOD::System* system = nullptr;
FMOD_RESULT result = System_Create(&system);

if (result ==FMOD_OK)
    return 1;
else if(result != FMOD_OK)
    return result;

}

However, when I test the plugin in Unity, to be precise when i build an android project on my phone, this function always returns the number 28. I have checked the documentation and that means *FMOD_ERR_INTERNAL". Im not quite sure what is causing the problem, any ideas are appreciated.

Yes it is, we ships Android Studio examples in the FMOD Engine for Android. These examples are located in “api\core\examples\androidstudio”.

This is probably coming from internal JNI initialization checks that ensure the environment is setup correctly. When using FMOD in an Android application there are a few extra calls that need to be made to get setup before calling System_create, but I am not sure how that applies when creating an Android module/library. It may be enough just to link fmod.jar to your project and see if that satisfies the JNI initialization checks, if not you may also need to find a place to call org.fmod.FMOD.init inside the application using your library. In our Unity integration we do so with the following:

AndroidJavaObject activity = null;
using (var activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
    activity = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
}
using (var fmodJava = new AndroidJavaClass("org.fmod.FMOD"))
{
    if (fmodJava != null)
    {
        fmodJava.CallStatic("init", activity);
    }
}

Thank you for the reply!

Turns out I was wrong with my presumption that when building a native library in Android Studio you don’t need the .jar file. After adding the .jar file as a library the function returned the number 1 i.e FMOD_OK.

For anyone with the same problem. Just add the .jar file in the lib folder of the application. Right click on it and select Add as library and chose to add it to your Android module/library.
No calling of the org.fmod.FMOD.init was needed!

1 Like