0

I have been trying to call a Java method in unity. Not working for me, even the simplest example from the docs

using System.Collections;
using UnityEngine;

public class ExampleClass : MonoBehaviour {
    void Start () {
        AndroidJavaObject jo = new AndroidJavaObject ("java.lang.String", "some string");
        int hash = jo.Call<int> ("hashCode");
        Debug.Log ("hash=" + hash);
    }
}

Unity console prints hash=0, which is not the hash code for provided String. Even if I change and use java.lang.StringInvalidClass as class name, unity still reports same result to the console without notifying errors. I can even try to call toString, toStringa, toInvalid2 and they always return empty string without showing errors.

This is a brand new 2d project with only script displayed above, attached to camara object. I am using Ubuntu, Unity 2019.4 and project platform is Android.

Thanks for the assistance.

6
  • "fails without errors", either it has errors, or it doesn't fail. Maybe it doesn't do what you expect it to do, but that doesn't mean it fails. It's more likely an issue with the code. You might want to check this: codejava.net/coding/… Commented Jun 29, 2020 at 5:09
  • As far as I can see from unity documentation both AndroidJavaObject class and Call method expect 2nd parameter to be an array. Not sure if string qualifies in that situation as character array. Commented Jun 29, 2020 at 6:39
  • Hi @stultuske. As mentioned, even if I use java.lang.StringInvalidClass, unity prints the same. Of course StringInvalidClass does not exists and it should throw an error. So, something is failing without actually throwing or displaying an error in the console. I used a simple example to avoid complexity. Commented Jun 29, 2020 at 14:15
  • Hi @user3647971. Yes, AndroidJavaObject and Call works using an array as second parameter, but if no parameter required or it just requires one, it is acceptable to not provide it and provide just one. In fact, the code provided is directly copied from uninty docs: docs.unity3d.com/es/530/ScriptReference/… Commented Jun 29, 2020 at 14:17
  • @Jhovanni I noticed that documentation is for older version of unity. See here It should be included in UnityEngine.AndroidJNIModule. Probably not the issue, but worth checking out. Commented Jun 30, 2020 at 1:41

1 Answer 1

1

Answering myself after some time working with unity.

All examples in the web and unity documentation doesn't mention it, which is weird since it is something simple to mention and about confusions: code needs to run as an android application. Editor or even unity remote does not work, in order to use AndroidJavaObject and related classes, your code needs to run as an android application installed in the phone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.