Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mkderin · Jun 11, 2015 at 10:43 AM · androidjavaandroidjavaobjectjar

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.)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LoadStar · Jun 11, 2015 at 08:12 PM 0
Share

What is the library supposed to do? How you can get different languages/programs to work together to do what you want depends on what you need it to do. Also, I would like to point out how ironic it is that we would have this problem since Android apps usually are written in Java.

avatar image LoadStar · Jun 11, 2015 at 08:21 PM 0
Share

And now, given my edit I added to my answer, this is even more ironic since the C# and Java would both probably end up as the same type of bytecode on the Android device.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by LoadStar · Jun 11, 2015 at 03:44 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Calling static jar function from Unity3D 0 Answers

How to translate ArrayList to C# using AndroidJavaClass? 1 Answer

Using AndroidJavaObject properly 1 Answer

Android manifest does not merge with generated manifest. 0 Answers

NoSuchMethodError calling android method 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges