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
1
Question by monogon · Dec 27, 2014 at 03:27 PM · arrayparameterspassjni

How do I pass a string array to Java via JNI?

Hello!

I need to pass a string array to Java with the Java Native Interface.

However I receive the following error in Java:

! getMethodID("ReceiveScenes", "(Ljava/lang/String;)V") FAILED!

My UnityCode:

 AndroidJavaClass jc;    
         
 jc = new AndroidJavaClass("com.lwptest.lwp.arrayTest");
 jc.CallStatic("ReceiveArray", arr);


"arr" is an array of string which is filled elsewhere (It's not null when I try to call the function, I checked this).

Java Code

 public static void ReceiveArray(String[] theArray) {
         
         Log.i(arrayTest.TAG, "ReceiveArray " + theArray[0]);
         
 }


When I only try to send a single string and change the methods accordingly everything works perfectly and the error is gone.

What am I missing? Why can't I pass an array? Can anybody help?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by pweeks · Dec 16, 2015 at 10:09 PM

i made an array of objects and then assigned the string array to one of the entries

 object[] objs = new object[3];
 objs[0] = string array;
 objs[1] = string;
 objs[2] = string;
 
 jo.call("javafunc",objs);


then the java signature just matches the items in each entry in the object array so

 public void Javafunc(String[] arg1, String arg2, String arg3)



Comment
Add comment · Show 2 · 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
avatar image supernat · Dec 17, 2015 at 12:23 AM 0
Share

Yep, it took me several days to figure this out a while back. If you just need a single String[], just create object[] objs = new object[1]; and assign obj[0] to the array. I know that sounds strange, but it causes the Java method declaration to be generated properly whereas passing a String[] makes it think it's only a single String.

avatar image abi-kr01 · Dec 20, 2019 at 02:56 PM 0
Share

For someone who visit this link

 Java code 

 

 package com.Abhi.test.Unity.persist;
 
 import android.content.Context;
 import android.util.Log;
 import android.widget.Toast;
  
 public class callUnity
 {
     public static String[] getStringArray()
     {
         String[] strs = { "Test1", "Test2" };
         return strs;   // Or alternatively, return null;
     }
 }

C# calling

  public void ShowData()
     {
 
       string  javapluginaddress="com.Abhi.test.Unity.persist";
        AndroidJavaClass cls = new AndroidJavaClass(javapluginaddress);
         AndroidJavaObject obj = cls.CallStatic<AndroidJavaObject>("getStringArray");
         if (obj.GetRawObject().ToInt32() != 0)
         {
             // String[] returned with some data!
             string[] result = AndroidJNIHelper.ConvertFromJNIArray<string[]>
                                   (obj.GetRawObject());
             foreach (string str in result)
             {
                 Debug.Log("Data from java is "+str);
                 // Do something with the strings
             }
         }
         else
         {
             // null String[] returned
         }
         obj.Dispose();
         cls.Dispose();
     }











avatar image
1

Answer by $$anonymous$$ · Aug 09, 2018 at 03:21 PM

All you need to do is cast the string array to an object, like this:

 string[] strings = new string[] { "string1", "string2", ...};
 myAndroidJavaObject.Call("myMethod", (object) strings);
Comment
Add comment · Show 1 · 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
avatar image Bunny83 · Aug 09, 2018 at 07:31 PM 0
Share

Right, the issue is that the Call method takes a "params" array. Therefore when you pass a single array it will treat the array elements as single parameters. The "params" keyword is actually just syntactic sugar. So if you have a method like this:

 void $$anonymous$$y$$anonymous$$ethod(params int[] aParams)
 {
 }

Then you can simply call this method like this:

 $$anonymous$$y$$anonymous$$ethod(5, 1, 42, 55);

This is just syntactic suger for this:

 $$anonymous$$y$$anonymous$$ethod(new int[]{5, 1, 42, 55});

In the case of the Call method when you cast the strings array to "object" the compiler will treat it as single value and actually does:

 myAndroidJavaObject.Call("my$$anonymous$$ethod", new object[] { strings });

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

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Undeclared transform array 0 Answers

Instantiating on passed parameters? 1 Answer

How to use texture array in shader? 1 Answer

How to Skip Parameter in C# 2 Answers

calling an void from another script and passing paramters... 1 Answer


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