How do I use Java callbacks?

In my project I have to use a third party java library (.jar) and it’s callback class.

In Android the callback class, which I need to override:

private MyCallback myCallback = new MyCallback() {
	@Override
	public void finished(int result) {
		Log.i(TAG, "finished");
	}

In Android using the class:

MyClass myClass = new MyClass(myCallback);
myClass.doSomething();

After finishing it’s work, MyClass calls the MyCallback’s finished method which I need to use in C# Unity (getting the result).

How can I do that? How can I override a class or method for Unity? (Note that I can’t modify the third party lib and these classes.)

There is a methodology for this. In order to do this, you need to go through a few steps and jump through a hoop or two.

Check out Unity’s manual for using C, C++, and Java code as Android plugins at http://docs.unity3d.com/Manual/PluginsForAndroid.html

There is a learning curve there, especially if you have not used any of the technologies mentioned in that manual, so you may need to research some of the topics presented in it. It will take some time, but if your library you want to use is expansive and complex enough it may be worth that time.


My original answer is now obsolete for this question since I originally answered it from the point of view of a PC and not for an Android, but I will leave it below in case future readers have a similar question but not strictly for Android.


I will list a few possibilities here. Whether or not some of them work for you depends on what effects the library has; does it change the game state? does it do some kind of logging or saving? does it send a message to a server? Since you probably want it to change your game state, you will likely be more restricted in what you can do. Short answer is that you can’t directly. Long answer is that there are some tricks you can do depending on what the library does so it will help to have that information.

First, the easiest (but probably not the best) way to do this would probably be to make a small stub program in Java which does what you want, then run that program.

For example, something like…

// This is Java
public class MyCallbackStub
{
    public static void main(String[] args)
    {
        TheLib.finished(Integer.parseInt(args[1]));
    }
}

Then, in your C# code, System.Diagnostics.Process.Start(“java MyCallbackStub”) will perform the callback.

Now, this easiest way has lots of huge drawbacks. For example, this sample cannot make use of any possible return value of the library’s code (though with some modification it could get the return value). The library cannot modify the state of your program since it is technically a separate program this way; that is, it cannot shoot a projectile in your game or increase your health or make the enemy run away - at least not without creating some way for the two programs to share information, like with sockets which are usually used for network communication but can be used to make two programs on one computer talk to each other. This is sufficient for a limited set of uses.

Second, Java has a way to directly use native code. This Java API is called JNI. This lets you make Java calls to libraries written in other languages, though I have only used it with C, not C# and I’m not sure how (or if) it would work with C#, so you would have to do your own research. The main drawback for you is that this involves Java calling another language’s code, not the other way around, so this might not work for you without some very crafty hacks.

There is a tool called jni4net which might be useful. An answer here on stackoverflow mentions this tool and gives an example, but again, that example is for calling C# from Java, not Java from C#, but I’m not sure if that tool only goes 1 way or if it also allows using Java from C#; it’s worth a look.

And I saved the best for last. Take a look at IVKM, mentioned here. This sounds like your best bet, but I have not used that tool. It looks like it compiles Java byte code into .NET’s and gives you libraries you can use directly in C#. Because of this, your mileage may vary since this is a complicated process, but I don’t know for sure, maybe it can work everything out easy and without issue. Give it a look. I would be interested to know how this goes for you - let us know.