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 /
This question was closed Apr 14, 2013 at 10:36 AM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Kabbs · Apr 14, 2013 at 01:28 AM · androidjni

JNI Exception using Android Plugin

Hi,

I'm trying to integrate the DeployGate SDK into my unity game for android. Here's what it says on their website:

If your project has an Application class specified in in your AndroidManifest.xml, open that class and add following line into onCreate() function: DeployGate.install(this);

https://deploygate.com/docs/sdk

So I downloaded the deploygatesdk.jar, copied it to /Assets/Plugins/Android, and used it on startup (C# MonoBehaviour called DeployGate.cs) like this:

     void Start () {

         AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

         AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

         AndroidJavaObject app = activity.Call<AndroidJavaObject>("getApplicationContext");



         AndroidJavaClass dg = new AndroidJavaClass("com.deploygate.sdk.DeployGate");

         dg.CallStatic("install", app);

     }







When I start the game on android, LogCat reports these Errors:

 04-14 01:51:14.389: I/Unity(17702): ExecutionEngineException: JNI Exception

 04-14 01:51:14.389: I/Unity(17702):   at UnityEngine.AndroidJavaObject._CallStatic (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 

 04-14 01:51:14.389: I/Unity(17702):   at UnityEngine.AndroidJavaObject.CallStatic (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 

 04-14 01:51:14.389: I/Unity(17702):   at DeployGate.Start () [0x00034] in D:\Unity\TestGame\Assets\Scripts\DeployGate.cs:15 

 04-14 01:51:14.389: I/Unity(17702):  

 04-14 01:51:14.389: I/Unity(17702): (Filename: D Line: 0)

 

D:\Unity\TestGame\Assets\Scripts\DeployGate.cs:15 is the project path on my windows PC. This is weird. Any idea how to resolve this Exception?

EDIT: I activated debug-checkjni in adb and this exception might be more of a help:

 04-14 10:22:25.826: W/System.err(28435): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
 04-14 10:22:25.846: W/System.err(28435):     at android.os.Handler.<init>(Handler.java:197)
 04-14 10:22:25.846: W/System.err(28435):     at android.os.Handler.<init>(Handler.java:111)
 04-14 10:22:25.846: W/System.err(28435):     at com.deploygate.sdk.DeployGate.<init>(DeployGate.java:207)
 04-14 10:22:25.846: W/System.err(28435):     at com.deploygate.sdk.DeployGate.install(DeployGate.java:467)
 04-14 10:22:25.846: W/System.err(28435):     at com.deploygate.sdk.DeployGate.install(DeployGate.java:441)
 04-14 10:22:25.846: W/System.err(28435):     at com.deploygate.sdk.DeployGate.install(DeployGate.java:418)
 04-14 10:22:25.846: W/System.err(28435):     at com.deploygate.sdk.DeployGate.install(DeployGate.java:369)
 04-14 10:22:25.846: W/System.err(28435):     at com.deploygate.sdk.DeployGate.install(DeployGate.java:349)
 04-14 10:22:25.846: W/System.err(28435):     at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
 04-14 10:22:25.846: W/System.err(28435):     at com.unity3d.player.UnityPlayer.onDrawFrame(Unknown Source)
 04-14 10:22:25.846: W/System.err(28435):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
 04-14 10:22:25.846: W/System.err(28435):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

According to Google it has to run on the uithread to work. Do I have to edit the jar file to achieve that?

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

  • Sort: 
avatar image
9
Best Answer

Answer by Kabbs · Apr 14, 2013 at 10:36 AM

Ok, I'm gonna solve this myself. Here's how I did it:

 public static class DeployGate {

     private static AndroidJavaClass deployGate = null;

     public static void Install() {
         // Get Android context
         AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
         AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
         AndroidJavaObject app = activity.Call<AndroidJavaObject>("getApplicationContext");

         // Install DeployGate, make sure it will not called twice in your app
         deployGate = new AndroidJavaClass("com.deploygate.sdk.DeployGate");

         activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
         {
             deployGate.CallStatic("install", app);
         }));
     }
 }

So

             activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
             {
                 deployGate.CallStatic("install", app);
             }));

makes the call run on the uithread, additionally, I made everything static since DeployGate should only be installed once and should be persistent through level changes (but that didn't have anything to do with my errors). So it's not a MonoBehaviour anymore, instead I call it like this:

 DeployGate.Install();

I hope this'll help someone who stumbles upon this with the same problem. Here's the whole class with a few basic methods:

 using UnityEngine;
 using System.Collections;
 
 public static class DeployGate {
 
     private static AndroidJavaClass deployGate = null;
 
     public static void Install() {
 
         // Get Android context
         AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
         AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
         AndroidJavaObject app = activity.Call<AndroidJavaObject>("getApplicationContext");
 
         // Install DeployGate, make sure it will not called twice in your app
         deployGate = new AndroidJavaClass("com.deploygate.sdk.DeployGate");
 
         activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
         {
             deployGate.CallStatic("install", app);
         }));
     }
 
     public static void LogError(string text) {
         deployGate.CallStatic("logError", text);
     }
 
     public static void LogWarn(string text) {
         deployGate.CallStatic("logWarn", text);
     }
 
     public static void LogDebug(string text) {
         deployGate.CallStatic("logDebug", text);
     }
 
     public static void LogInfo(string text) {
         deployGate.CallStatic("logInfo", text);
     }
 
     public static void LogVerbose(string text) {
         deployGate.CallStatic("logVerbose", text);
     }
 }

Source: https://github.com/anchan828/deploygate-with-unity/blob/master/Assets/DeployGate/Plugins/DeployGateSDK.cs

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 beto456789 · Apr 07, 2014 at 09:30 AM 0
Share

Thanks! You saved my life!!! :D

avatar image srivatsav · May 02, 2014 at 12:25 AM 0
Share

Why does it have to run in "runOnUiThread" ?

avatar image MooOooN · Apr 24, 2015 at 07:37 AM 0
Share

I am not using deployGate rather a custom Android SD$$anonymous$$. This worked thanks.

avatar image widnig89 · Nov 04, 2015 at 09:34 AM 0
Share

Thanks a lot! :)

Follow this Question

Answers Answers and Comments

14 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

Related Questions

What is the difference between AndroidJavaClass.Call and AndroidJavaObject.Call 2 Answers

Getting native Android String constants via JNI broken in Unity 2019.2.0? 3 Answers

Getting byte[] or ByteBuffer[] from native Java 1 Answer

Passing AndroidJavaObject to C++ method using JNI 0 Answers

JNI - local reference table overflow 0 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