Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kevin321an · Sep 01, 2016 at 05:32 AM · androidjnibridge

How to use AndroidJavaClass.Call() to pass a data object

Hi, All,

I've not had much experience to play around with the JNI bridge. Now I have a problem that not being able to pass a customized Class to a Java method. What I need to achieve is basically pass the info from C# to the method in the Android Java Class. Thank you.

 private AndroidJavaClass ajc;
 private AndroidJavaObject ajo;
 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 AndroidJavaClass videoObj = new AndroidJavaClass("com.example.sdk.VidoSchema);
 
 ajo = jc.GetStatic<AndroidJavaObject>("currentActivity"); 
 ajc = new AndroidJavaClass("com.example.sdk.ExampleSDK");

How to pass the VidoSchema object to the method in java file?

 ajc.CallStatic("lanuchSDKMethod", ajo, SERIAL,videoObj);

Also, how to pass the arraylist of this VidoSchema class?

 ArrayList<VidoSchema> videoObjArrayList;
 ajc.CallStatic("lanuchSDKMethod", ajo, SERIAL,videoObjArrayList);





In java ExampleSDK file, I have methods:

 public static void lanuchSDKMethod(Activity activity, String serialKey, VidoSchema cArrayList) {
       ...
       ...
     }
 
     
 public static void lanuchSDKMethod(Activity activity, String serialKey, ArrayList<VidoSchema> cArrayList) {
       ...
       ...
     }
 
 
   

  **This is the data structure file:** 
     
     public class VidoSchema implements Parcelable {
 
         public boolean isEntryVideo;
         public String branchId;
         public String video_location;
         public String xml_location;
         public String subtitle_location;
         public String assets_folder;
 
         public VidoSchema(Parcel in) {
             isEntryVideo = in.readByte() != 0x00;
             branchId = in.readString();
             xml_location = in.readString();
             subtitle_location = in.readString();
             assets_folder = in.readString();
         }
 
         public VidoSchema() {
         }
 
         @Override
         public int describeContents() {
             return 0;
         }
 
         @Override
         public void writeToParcel(Parcel dest, int flags) {
             dest.writeByte((byte) (isEntryVideo ? 0x01 : 0x00));
             dest.writeString(branchId);
             dest.writeString(xml_location);
             dest.writeString(subtitle_location);
             dest.writeString(assets_folder);
         }
 
         @SuppressWarnings("unused")
         public static final Creator<VidoSchema> CREATOR = new Creator<VidoSchema>() {
             @Override
             public VidoSchema createFromParcel(Parcel in) {
                 return new VidoSchema(in);
             }
 
             @Override
             public VidoSchema[] newArray(int size) {
                 return new VidoSchema[size];
             }
         };
     }

  
   






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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by liortal · Sep 01, 2016 at 05:57 AM

First, you should understand the difference between AndoidJavaObject and AndroidJavaClass:

An AndroidJavaObject is a representation of an object instance in the JVM. When you create an AndroidJavaObject, you are essentially calling the Java constructor of that type (that matches the signature according to the parameters you pass in), then you can call methods and access fields of that object instance.

An AndroidJavaClass is the "type" representation of a JVM class. This is similar to the Type class in C#. Using this, you can query the class's members (static members) or access "reflection-like" information about the type (not a specific instance of that type!).

With this knowledge, let's see how to invoke your Java method:

 private AndroidJavaClass ajc;
 private AndroidJavaObject ajo;
 
 // For static members you can use the AndroidJavaClass
 // (you are not accessing any instance of the class)
 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 ajo = jc.GetStatic<AndroidJavaObject>("currentActivity");
 
 // Again, this is OK in case the methods you are trying to call are static
 var exampleSDK = new AndroidJavaClass("com.example.sdk.ExampleSDK");
 
 // Construct a new VideoSchema object. This is equivalent to calling the empty ctor
 // If you need to use another ctor, add the parameters after the type name:
 // var videoObj = new AndroidJavaObject("com.example.sdk.VidoSchema", "1", "2");
 var videoObj = new AndroidJavaObject("com.example.sdk.VidoSchema");
 
 exampleSDK.CallStatic("lanuchSDKMethod", ajo, SERIAL, videoObj);

Comment
Add comment · Show 4 · 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 brunoleos · Jan 05, 2017 at 06:47 PM 0
Share

The difference doesn't makes sense to me either. $$anonymous$$ore specifically, I see no use for AndroidJavaClass.

The signature of the classes are:

 public class AndroidJavaObject : IDisposable{...}
    
 public class AndroidJavaClass : AndroidJavaObject
 {
     public AndroidJavaClass(string className);
 }

So, one can see that AndroidJavaClass inherits from AndroidJavaObject, and have no overrides. Besides, one can call static methods also from AndroidJavaObject.

avatar image liortal brunoleos · Jan 05, 2017 at 07:11 PM 0
Share

I agree that it's actually a bit confusing, not all classes or APIs are the best example of software engineering principles.

avatar image Bunny83 brunoleos · Jan 05, 2017 at 11:38 PM 0
Share

AndroidJavaClass and AndroidJavaObject are just wrapper classes. The important difference is in the way it's initialized. The full class looks like this:

     public class AndroidJavaClass : AndroidJavaObject
     {
         public AndroidJavaClass(string className)
         {
             this._AndroidJavaClass(className);
         }
         internal AndroidJavaClass(IntPtr jclass)
         {
             if (jclass == IntPtr.Zero)
             {
                 throw new Exception("JNI: Init'd AndroidJavaClass with null ptr!");
             }
             this.m_jclass = AndroidJNI.NewGlobalRef(jclass);
             this.m_jobject = IntPtr.Zero;
         }
         private void _AndroidJavaClass(string className)
         {
             base.DebugPrint("Creating AndroidJavaClass from " + className);
             using (AndroidJavaObject androidJavaObject = AndroidJavaObject.FindClass(className))
             {
                 this.m_jclass = AndroidJNI.NewGlobalRef(androidJavaObject.GetRawObject());
                 this.m_jobject = IntPtr.Zero;
             }
         }
     }

The AndroidJavaClass has a different constructor and actually uses the JNI interface to actually get the class reference. Also as you can see the "m_jobject" pointer is not set as the instance only references the class and not an instance.

A class and an instance of a class are two different concepts and those two classes are simply wrapper for those two concepts.

So when you create an "AndroidJavaClass" instance you basically just creating a wrapper object which represents a certain Java class. When you create an AndroidJavaObject you also create a wrapper in C# but also an instance of the Java class inside the JV$$anonymous$$.

$$anonymous$$eep in $$anonymous$$d that both classes have different constructors!! AndroidJavaClass implements a new constructor that overrides the internal parameterless constructor of AndroidJavaObject. So both classes server different purposes but share some common fields / methods. It would have been a bit cleaner if they had implemented an a abstract base class for both, but since only the constructors are different this inheritance works just fine.

avatar image liortal Bunny83 · Jan 06, 2017 at 05:59 AM 0
Share

I$$anonymous$$O, the inheritance doesn't really work fine. For example, you can write something like this, which is pretty much meaningless:

 new AndroidJavaClass("aa.bb.cc").Call("some$$anonymous$$ethod");

The JavaClass cannot access any instance members (fields, methods). It would've been cleaner to model these classes differently to reflect this concept.

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

6 People are following this question.

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

Related Questions

How to add/edit onActivityResult for the activity created by currentActivity 1 Answer

How to use AndroidJavaClass.Call() to pass a ArrayList or Array 1 Answer

How to call an android notification plugin if it's not the main activity? 1 Answer

CreateJavaRuntime problem 0 Answers

JNI Exception using Android Plugin 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