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 hirenkacha · Oct 11, 2012 at 06:20 AM · jniandroidjni

Calling .jar function in unity

I want to integrate .jar files which has facebook sdk for android. Also want to integrate flurry and twitter functionality using JNI. The problem is I dont have any idea about JNI. I have read the classes in reference but could not make out. When to use AndroidJNI, AndroidJNIHelper?

for example I have jar file having code,

 package jni;
 
 public class TestClass 
 {
     private String a;
     
     public TestClass()
     {
         a="Hi all";
     }
     
     public String func()
     {
         return a;
     }
 }

I tried to do this but not getting

 using System;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using UnityEngine;
 
 public class TestJNI : IDisposable 
 {
     public static TestJNI instance;
     private String a;
     private AndroidJavaClass cls_Test = new AndroidJavaClass("jni.TestClass");
     
     
     public String print()
     {
         a=cls_Test.Call<string>("func");
         return a;
     }
     
     public void Dispose()
     {
         cls_Test.Dispose();
     }
 }
 

I tried printing string using

 print(TestJNI.instance.print());

but got this : "NullReferenceException: Object reference not set to an instance of an object"

Thanks

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

3 Replies

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

Answer by hirenkacha · Mar 06, 2013 at 06:22 AM

I got it working for sample code if I have a jar file having class name "classA" with static function "func_A()" which is supposed to be called. Package "com.research.pkgA"

 using System;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using UnityEngine;
 
 public class JNIcall : IDisposable
 {
     private static JNIcall _instance;
     public static JNIcall Instance
     {
         get
         {
             if(_instance == null) 
                 _instance = new JNIcall ();
             return _instance;
         }
     }
     
     private AndroidJavaClass cls_jni = new AndroidJavaClass("com.research.pkgA.classA");
     
     public void Share()
     {
         
         using(AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 
         {
             
             using(AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 
             {
                 
                 cls_jni.CallStatic("func_A",obj_Activity);
                 
             }
         }
     }
     public void Dispose()
     {
         cls_jni.Dispose();
     }
 };


And your AndroidManifest.xml file should include following activity.

 <activity android:name=".classA"  
                   android:label="@string/app_name"
                   android:configChanges="keyboardHidden|orientation" >
         </activity>
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 Kacho · Apr 01, 2013 at 08:15 AM 1
Share

This one helped me alot.. Thanks for sharing

avatar image MFen · Mar 12, 2014 at 05:35 PM 0
Share

Would you be able to pack this into a example project? I'm working on implementing the IABHelper (in-app billing for android) and would need to access there .jar functions.

avatar image hirenkacha · Mar 13, 2014 at 05:58 AM 0
Share

cant do that from here.. sorry..

avatar image praveengoparaju · Feb 16, 2017 at 08:43 AM 0
Share

@hirenkacha - where did you placed your java lib? I have a java lib called - HelloWorldlib.jar which I placed in Assets/ folder of the project. However am always receiving "Null Ptr" while trying to fetch the java object. Here is the exception am getting - Exception: JNI: Init'd AndroidJavaClass with null ptr! UnityEngine.AndroidJavaClass..ctor (IntPtr jclass) (at /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:556) UnityEngine.AndroidJavaObject.get_JavaLangClass () (at /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:534) UnityEngine.AndroidJavaObject.FindClass (System.String name) (at /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:525) UnityEngine.AndroidJavaClass._AndroidJavaClass (System.String className) (at /Users/builduser/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:545) UnityEngine.AndroidJavaClass..ctor (System.String className) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/AndroidJavaBindings.gen.cs:94) PlayerController..ctor ()

Any idea or inputs?

avatar image
0

Answer by andisopany · Oct 16, 2012 at 06:06 AM

In the least you need to instantiate your TestJNI class.

TestJNI test_class = new TestJNI();

then you can call

test_class.print();

The "instance" variable in your class is never assigned a value, that is why you get the exception.

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 hirenkacha · Oct 16, 2012 at 06:46 AM 0
Share

then where to use AndroidJNI or AndroidJavaCalss?? As TestJNI is a jar file not cs file.

avatar image andisopany · Oct 17, 2012 at 03:58 PM 0
Share

not sure what you mean by "where to use." the class.

avatar image
0

Answer by Paulius-Liekis · Oct 15, 2012 at 01:27 PM

Where do you set value of "instance"? I can't see it here. Which line throws the exception?

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 hirenkacha · Oct 16, 2012 at 05:03 AM 0
Share

actually I have not done JNI before. So I am totally blank in this.

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

13 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

Related Questions

Is it possible to cast AndroidJavaObject to its java subclass in C#? 1 Answer

Unable to call method via AndroidJNI.CallStaticVoidMethod 0 Answers

AndroidJNI reports lots of exceptions that don't propagate to Unity 0 Answers

JNI - local reference table overflow 0 Answers

Getting byte[] or ByteBuffer[] from native Java 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