Home > Blockchain >  AndroidJavaClass and AndroidJavaObject for C# .Net Framework
AndroidJavaClass and AndroidJavaObject for C# .Net Framework

Time:10-05

I have code in Unity that calls a function programmed in Java for Android using AndroidJavaClass and AndroidJavaObject and I would like to pass this code to a dll in C# .Net but I can't find an easy way to call Java functions using C# .Net since the AndroidJavaObject and AndroidJavaClass classes are exclusive to the Unity dll, here is an example of my code:

Java (Android):

public class DeviceManager {
    public interface DeviceListener {
        void DoSomething();
    }

    private DeviceListener deviceListener;

    public DeviceManager(DeviceListener deviceListener){
        this.deviceListener = deviceListener;
    }
    
    public String[] getSomeData(){
        return new String[]{};
    }
}

C# (Unity):

public class DeviceListener : AndroidJavaProxy
{
    DeviceManager deviceManager;

    public DeviceListener(DeviceManager manager) : base("com.myapp.DeviceManager$DeviceListener")
    {
        this.deviceManager = manager;
    }

    public void DoSomething();
}

public class DeviceManager
{
    private AndroidJavaObject jo;

    public DeviceManager(){
         jo = new AndroidJavaObject("com.myapp.DeviceManager", new DeviceListener(this));
    }

    public string[] GetData(){
        AndroidJavaObject returnedData = jo.Call<AndroidJavaObject>("getSomeData");
        return AndroidJNIHelper.ConvertFromJNIArray<string[]>(returnedData.GetRawObject());
    }
}

What is the best way to do that example in C# .Net instead of Unity?

EDIT: my main question is related to know how to get objects and call functions from an .aar file in .NET Framework inside a dll C# file for Unity

EDIT 2: I found some information about Xamarin, maybe that is what I am looking for?: https://github.com/xamarin/monodroid-samples/tree/master/JavaIntegration/AarBinding But even though I could link an .aar file to a dll in C# that doesn't quite answer my question in the best way to create a code that works the same as the example described above

CodePudding user response:

my main question is related to know how to get objects and call functions from an .aar file in .NET Framework inside a dll C# file for Unity

If you want to use AndroidJavaClass or AndroidJavaObject methods in your custom DLL, you should add references to the Unity DLLs. In Visual Studio, open the contextual menu for References in the Solution Explorer and select Add Reference. Then, select Browse > Select File.

At this stage, select the required .dll file, located in the UnityEngine folder.

using System; 
/* Unity */  
using UnityEngine;

namespace DLLTest {

    public class MyDLLTest {

    
    }
}

CodePudding user response:

It seems like you are looking to use an AAR plugin, I haven't done it myself, but Unity has a doc on how to integrate it in your project.

Then, once that library is compiled in Android studio and integrated in Unity, you can look at this example on how to call it from C# code.

I haven't done it myself, but the example should put you on the right track.

Regarding the scripting backend, I don't believe it will have any impact on this.

Edit Added Extract from the Example

Assets / Plugins / Android Place your native android .AAR file inside this folder. Now let’s write some code to call the plugin.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NativeCodeRunner : MonoBehaviour
{
    void Start(){
    CallNativePlugin();
}
//method that calls our native plugin.
public void CallNativePlugin() 
{
    // Retrieve the UnityPlayer class.
    AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    // Retrieve the UnityPlayerActivity object ( a.k.a. the current context )
    AndroidJavaObject unityActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");

    // Retrieve the "Bridge" from our native plugin.
    // ! Notice we define the complete package name.              
    AndroidJavaObject alert = new AndroidJavaObject("plugins.vsoft.com.library.Alert");

    // Setup the parameters we want to send to our native plugin.              
    object[] parameters = new object[2];
    parameters[0] = unityActivity;
    parameters[1] = "Hello World!";

    // Call PrintString in bridge, with our parameters.
    alert.Call("PrintString", parameters);
    }
}
  • Related