Unable to build for ML2 in Android studio

I am trying to build a plugin that I can call through a unity project on ML2.

The plugin builds on Android Studio for x86_64 release. However on ML2, the library is unreachable. It seems to me I don't have the right config to build for ML2 on Android studio.

I have followed the "Android Studio Workflow" guide on the ML2 developer documentation but I can't seem to figure out the issue.
How to enable a build variant for ML2 on android studio?

Tried calling a function in the android x86_64 plugin to add 2 number (passed in as 2 arguments) and return the result. Its only returns the first operand as the result. Seemed like a calling convention issue to me. Tried with calling conventions Cdecl, StdCall, ThisCall, FastCall through Unity. All had the same result. The android plugin package (.so) is configured as x86_64 executable in Unity.

Here is the snapshot of my current config:

ML2 OS version: Build: B3E.221020.13-R.039
MLSDK version: SDK Version 29
Host OS: Windows 10

Common Packages

C API Samples: 1.1.0-dev2
ML C SDK: 1.1.0-dev2
ML C SDK CMake Support: 1.0.6.0
ML OS: 1.1.0-dev2 (Secure)

Unity Packages

Unity Editor: 2022.2.0b14.112.4874
Unity Examples / package / Doc / App Sim: 1.1.0-dev1
Unity Soundfield Audio: 3.2.1-221024.37.0d182e6_1.1.0-dev1

Android Studio

Version: Chipmunk 2021.2.1 Patch 2
SDK Platform: Android 10.0(Q) / API level 29
SDK Build-Tools 33.0.1
NDK 25.0.8775105
SDK Command Line tools 8.0
CMake 3.22.1
Android Platform-Tools 33.0.3
Android SDK Tools 26.1.1
Magic Leap SDK (Tools) 1.0.6
Magic Leap SDK (v1.1.0-dev2)

*** ML2 Addon was added to SDK Update sites ***
*** Command line options in Settings->Build,Exec, Deployment->Compiler was set to :
-Proot_path=C:\Users\robot\MagicLeap\mlsdk\v1.1.0-dev2

1 Like

@robot is the issue that the APIs are not being recognized or that you are unable to build the library file? Have you been able to build the Magic Leap Native Samples?

1 Like

The APIs are recognized. I can call a function in the plugin and it returns a result. Just not the correct one. Seems like a calling convention issue. Like the parameters being passed and getting manged/truncated or something..

eg. calling an API from unity test_add(15,25) returns 15 from the plugin on the device.
the function signature in Unity (C#) is :
[DllImport(libname, CallingConvention = CallingConvention.Cdecl)]
private static extern int test_add(int handle, int opa, int opb);

The code in the plugin on android studio (C++) is :
extern "C"
{
int test_add(int handle, int opa, int opb);
}

the function simply does return opa + opb;

1 Like

@robot To identify where the issue is coming from, try the following:

Build a Standard Android Application that uses the plugin you created.

  1. Create a new Unity Project using Unity 2022.2.x
  2. Set the Build target to Android and the player settings to IL2CPP, x86_64 , Vulkan
  3. Create a Native Plugin for Android and add it into your Unity Project
  4. Create a script that makes sure the plugin is initialized and calls the test functions
  5. Build the Application

Deploy the Android Application to Android Studios Device Emulator then Magic Leap 2

  1. Use Android Studio to create a Device Emulator that targets Android 10, x86_64 without Google Play services.
  2. You can also try running the Android Application directly on your Magic Leap 2. The application will open as a standard 2D Android App.

Test

  1. Finally, run the application and see if the test returns the correct sum for the two integers.
1 Like