Why Unity3d failed importing jar plugin?

Question

I have a small Unity3d project to integrate JAR with it. My (simplified) java class in Android Studio library project is like below code:

package com.playsqreen.library.api;

... imports ...

public class PlaysreenAPI {

    private static PlayscreenAPI _api;

    private PlayscreenAPI(Activity activity) {
        this.activity = activity;
    }

    // static method to create singleton
    public static PlayscreenAPI build(Activity activity, String key) {
        if (_api == null) {
            // post processing something
            // before returning instance of this class
            _api = new PlayscreenAPI(activity);
        }
        return _api;
    }

    public String doEchoThis(String msg) {
        return "ECHO: " + msg;
    }

}

So from Android Studio, I generate my JAR and dump it into ../MyProject/Assets/Plugins/Android and from Unity IDE I can see something like below:

enter image description here

Then I create a C# script like below to load my java class:

void Start () {
    _builder = new StringBuilder();
    try
    {
        _builder.Append(">>> Step 1\n");
        AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        if (activityClass != null)
        {
            _builder.Append(">>> Step 2\n");
            AndroidJavaObject activity = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
            if (activity != null)
            {
                _builder.Append(">>> Step 3\n");
                AndroidJavaClass apiClass = new AndroidJavaClass("com.playsqreen.library.api.PlayscreenAPI");
                if (apiClass != null)
                {
                    _builder.Append(">>> Step 4\n");
                    object[] args = { activity, secretKey };
                    api = apiClass.CallStatic<AndroidJavaObject>("build", args);
                }
            }
        }
    }
    catch (System.Exception e)
    {
        _builder.Append(e.StackTrace.ToString());
    }
}

I print my StringBuilder in Text UI object in order for me to capture on which Step my code brakes, and it turns out after step 3. and my Text UI object prints:

java.lang.ClassNotFoundException:com.playsqreen.library.api.PlayscreenAPI and etc...

A thread I found here suggest me to use Java Decompiler to check if the java class really included in the Jar, from this site. And so I did, and the Java Decompiler shows my java class does exists (see below)

enter image description here

So I really stuck now. How can I load my java class from Unity? Please help.

Thanks.

After following lysergic-acid advice below, I includes the rest of jars that required by my custom jar, see below pic. And everything fine :)

enter image description here


Show source
| java   | android   | android-studio   | unity3d   2016-12-09 05:12 2 Answers

Answers to Why Unity3d failed importing jar plugin? ( 2 )

  1. 2016-12-09 10:12

    You did not post the full error message you are getting at runtime. Also you did not mention which of your debug prints get printed, so i'll try to come up with a few different issues that you can check. Hopefully, one of these can assist in fixing the issue:

    • JAR name contains '.' (period character). Not sure how Unity interprets this (i've never used such a naming convention myself). Select the .JAR file in Unity and make sure that Unity marks it up as an Android plugin (should have "Android" selected in the plugin importer. I would also try to rename that to a name without any periods just to be on the safe side.
    • Wrong invocation of the Java method: in your example, the static method build in Java receives a single argument (Activity), but when you're calling it from Unity, you're passing an array of 2 arguments.
    • Missing dependencies: When your native Java code relies on other classes (e.g: from other libraries), in case you do not include these libraries with your .JAR file, your class will not be loaded at runtime and it will fail with cryptic errors such as NoClassDefFoundException. Make sure to include all dependencies as well.

    **Shameless Promotion: ** I offer services to fix Android related issues in Unity. Check it out here, in case you're interested :)

  2. 2016-12-09 15:12

    Install newest JDK version and try again.

Leave a reply to - Why Unity3d failed importing jar plugin?

◀ Go back