• 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
Question by robw00t · May 10, 2016 at 05:59 AM · androidandroidpluginandroid-manifestandroidjavaobject

Unable to retrieve return values from android library (aar file) to c# in unity 5.3.4p3 (string,int,bool) using AndroidJavaClass.Call()

Hello I'm trying it include an android library in my unity project. My android code is as follows:

 package com.androidtester;
 
 public class ATests {
     public String teststr() {
         return "testing...";
     }
     public boolean testbool() {
         return true;
     }
     public int testint() {
         return 123;
     }
 }
 

My c# code is as follows:

 public class GameControl : MonoBehaviour {
     void Start ()
     {
         try {
             AndroidJavaClass atests = new AndroidJavaClass ("com.androidtester.ATests");
             Debug.Log ("Test Str:" + atests.Call<string> ("teststr"));
             Debug.Log ("Test Int:" + atests.Call<int> ("testint"));
             Debug.Log ("Test Bool:" + atests.Call<bool> ("testbool"));
         } catch (Exception e) {
             Debug.Log ("ATests Error..." + e.ToString ());
         }
     }
 }
 

This is within a blank unity 2D project with an empty game object containing GameControl. My folder structure is as so:

alt text

When I run my unity application on an android device I see this in adb's logcat:

 I/Unity   (29910): GameControl:Start() (at ./Assets/GameControl.cs:10)
 I/Unity   (29910):  
 I/Unity   (29910): (Filename: ./Assets/GameControl.cs Line: 10)
 I/Unity   (29910): 
 I/Unity   (29910): Test Str:
 I/Unity   (29910): UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
 I/Unity   (29910): UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
 I/Unity   (29910): UnityEngine.Logger:Log(LogType, Object)
 I/Unity   (29910): UnityEngine.Debug:Log(Object)
 I/Unity   (29910): GameControl:Start() (at ./Assets/GameControl.cs:13)
 I/Unity   (29910):  
 I/Unity   (29910): (Filename: ./Assets/GameControl.cs Line: 13)
 I/Unity   (29910): 
 I/Unity   (29910): Test Int:0
 I/Unity   (29910): UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
 I/Unity   (29910): UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
 I/Unity   (29910): UnityEngine.Logger:Log(LogType, Object)
 I/Unity   (29910): UnityEngine.Debug:Log(Object)
 I/Unity   (29910): GameControl:Start() (at ./Assets/GameControl.cs:14)
 I/Unity   (29910):  
 I/Unity   (29910): (Filename: ./Assets/GameControl.cs Line: 14)
 I/Unity   (29910): 
 I/Unity   (29910): Test Bool:False
 I/Unity   (29910): UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
 I/Unity   (29910): UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
 I/Unity   (29910): UnityEngine.Logger:Log(LogType, Object)
 I/Unity   (29910): UnityEngine.Debug:Log(Object)
 I/Unity   (29910): GameControl:Start() (at ./Assets/GameControl.cs:15)
 I/Unity   (29910):  
 I/Unity   (29910): (Filename: ./Assets/GameControl.cs Line: 15)
 I/Unity   (29910): 

For some reason the return values from android are all wrong. If I enter a different function name or return type they generate exceptions so I know the signatures are matching up. I should mention that if I switch the android function to 'public static' then use 'AndroidJavaClass.CallStatic() in c# everything works fine. It's the plain AndroidJavaClass.Call() that gives incorrect return values.

screenshot-2016-05-09-173630.png (75.6 kB)
Comment
Piyush_Pandey

People who like this

1 Show 1
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 robw00t · May 10, 2016 at 04:21 PM 0
Share

I should mention that using this approach does work:

         Debug.Log ("Test Str:" + atests.CallStatic<string> ("teststr"));
         Debug.Log ("Test Int:" + atests.CallStatic<int> ("testint"));
         Debug.Log ("Test Bool:" + atests.CallStatic<bool> ("testbool"));

when I change the android functions to static functions:

 public static String teststr() {
     return "testing...";
 }
 public static boolean testbool() {
     return true;
 }
 public static int testint() {
     return 123;
 }

but it does not work when they are not static & I use the atests.Call() method. What I need is for the Call() approach to work properly without static function usage.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by meat5000 · May 10, 2016 at 09:08 PM

In my last plugin I had to Create an instance inside the plugin class.

After my AndroidJavaClass call I used my handle to call the AndroidJavaObject 'instance'. I use this instance handle, then, to retrieve members.

This is probably why static works. Non-static requires an instance I do believe.

The other thing is that Unity expects a .JAR file. I'm not sure it'll handle .AAR

http://docs.unity3d.com/Manual/PluginsForAndroid.html

Comment
Piyush_Pandey
minpu

People who like this

2 Show 5 · 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 robw00t · May 10, 2016 at 09:48 PM 0
Share

AAR plugin support has been added in 5.0b19. Feel free to try latest beta, your feedback is welcome. Just drop an .aar file into any folder of Unity project, it should be detected as Android-only plugin, and enjoy

posted by unity technologies on jan 16th, 2015 http://forum.unity3d.com/threads/does-unity-support-using-aar-package-as-android-plugin-library.272776/

the aar file seems to be ok it expands into a .jar during compilation I noticed. The problem seems to be directly related to context / instance life span for sure.

I've read the manual at http://docs.unity3d.com/Manual/PluginsForAndroid.html but I don't see anything that describes exactly what you're referring to. Looking at their string example:

 AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", "some_string");
 int hash = jo.Call<int>("hashCode");

its using the same approach I'm currently using for my ATests class.

Can you elaborate on the approach you took to create an instance inside your plugin class? That sounds like something worth exploring.

Do you mean doing this:

Java Code:

 public class PluginWrapper {
     public static ATests myStaticTests; //instance reference?
     
     public PluginWrapper() {
         if(!PluginWrapper.myStaticTests) {
             PluginWrapper.myStaticTests = new ATests();
         }
     }
 }

c# code:

     AndroidJavaObject wrapper = new AndroidJavaClass ("com.androidtester.PluginWrapper");
     AndroidJavaObject innerInstance = wrapper.Get ("myStaticTests");
     innerInstance.Call ("teststr");

or am I missing what you're suggestingL

avatar image robw00t · May 10, 2016 at 09:56 PM 0
Share

@meat5000 Oops I responded to your answer as a second comment above & it doesn't look like I can move it. Can you review & let me know your thoughts?

avatar image meat5000 ♦ robw00t · May 11, 2016 at 01:08 AM 0
Share

Check out my last plugin

http://answers.unity3d.com/questions/1163907/how-can-i-detect-if-the-back-button-is-virtual-or.html

Follow the class/instance pattern and respective class/object calls on the unity side. On mobile atm, ill follow up tomorrow. Btw you dont need the context part.

avatar image robw00t · May 16, 2016 at 07:02 PM 0
Share

@meat5000 you were right thanks for the nudge in the right direction.

Instead of this for non-static methods:

         AndroidJavaClass myAndroidPlugin = new AndroidJavaClass("com.myplugin");
         myAndroidPlugin.Call("..."); //does not work on AndroidJavaClass...only on AndroidJavaObject

do this (in c#):

         AndroidJavaClass myAndroidClass = new AndroidJavaClass("com.myplugin");
         AndroidJavaObject myAndroidPlugin = myAndroidClass.CallStatic<AndroidJavaObject>("instance");
         myAndroidPlugin.Call("...");

and this (in java):

 public class MyAndroidPlugin {
     private static MyAndroidPlugin m_instance;
 
     public MyAndroidPlugin() {
         m_instance = this;
     }
 
     public static MyAndroidPlugin instance() {
         if (m_instance == null) {
             m_instance = new MyAndroidPlugin();
         }
         return m_instance;
     }
 
 }
avatar image meat5000 ♦ robw00t · May 17, 2016 at 11:08 AM 0
Share

Most welcome. Click accept to mark the answer correct :)

avatar image

Answer by kasym_ · Oct 28, 2022 at 03:12 AM

Try to use AndroidJavaObject instead of AndroidJavaClass. As far as I am aware AndroidJavaObject is used for creating instances and AndroidJavaClass is used for classes with static methods.

I call non-static methods fine this way, I also .aar file for my plugin

Comment

People who like this

0 Show 0 · 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using CustomUnityPlayerActivity, which inherits from UnityPlayerActivity, will cause problems. 0 Answers

Get Phone number of android mobile device - Android native code to Unity c# 3 Answers

Using AndroidJavaProxy with abstract class 0 Answers

AndroidJavaException - java.lang.ClassNotFoundException 1 Answer

CommandInvokationFailure: Failed to re-package resources. Please Help. 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