Kotlin and Unity development error

I use Android Studio and Unity development, the Library packaged into aar file, and then the aar file as a Unity plug-in. When I use Java, no problem, but when using Kotlin, it will throw an exception. Thanks!

Exception:

 AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
                                    java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
                                        at com.lsl.plugin.PluginActivity.showToast(PluginActivity.kt)
                                        at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
                                        at com.unity3d.player.UnityPlayer.c(Unknown Source)
                                        at com.unity3d.player.UnityPlayer$c$1.handleMessage(Unknown Source)
                                        at android.os.Handler.dispatchMessage(Handler.java)
                                        at android.os.Looper.loop(Looper.java)
                                        at com.unity3d.player.UnityPlayer$c.run(Unknown Source)
                                     Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.jvm.internal.Intrinsics" on path: DexPathList[[zip file "/data/app/com.lsl.aardemo-1/base.apk"],nativeLibraryDirectories=[/data/app/com.lsl.aardemo-1/lib/arm, /data/app/com.lsl.aardemo-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
                                        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                        at java.lang.ClassLoader.loadClass(ClassL

activity

class PluginActivity : UnityPlayerActivity() {
fun showToast(msg: String) {
    runOnUiThread {
        Toast.makeText([email protected], msg, Toast.LENGTH_SHORT).show()
    }
}

}

.cs script

    public static AndroidTools GetInstance(){
    if (instance == null) {
        lock (syncRoot) {
            if (instance == null) {
                jc = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
                jo = jc.GetStatic<AndroidJavaObject> ("currentActivity");
                instance = new AndroidTools ();
            }
        }
    }
    return instance;
}

public void ShowToast(string message){
    jo.Call ("showToast",message);
}

aar file:

enter image description here


It has nothing to do with JetBrain supporting .NET. I actually had this problem and solved it by using GoogleJarResolver.

Problem

When you pack your plugin into an AAR files, the dependencies are not packed with it assuming that the consumer (which is usually another Android Studio project and not Unity) will resolve these dependencies. However, as you'd guess, Unity does not do anything regarding resolving dependencies on AAR files, so you've got to do it yourself.

Solution

Google has a Unity plug-in which actually does that, it downloads the needed files to satisfy the dependencies and adds them to your project. As the description and instructions on Google Jar Resolver home page is beyond what you exactly need, I just give you the instructions here for that specific issue:

  1. Add GoogleJarResolver to your Unity project.
  2. Create and add a file called $XDependencies.xml (where $X is the name of your plugin) to your project. This file should contain your Gradle file dependencies in it. Assuming you are only using Kotlin stdlib, the file will look like this: (remember to use correct kotlin version)

    <dependencies> <androidPackages> <androidPackage spec="org.jetbrains.kotlin:kotlin-stdlibjdk7:1.2.61" /> </androidPackages> </dependencies>

  3. Resolve the dependencies using GoogleJarResolver by selecting the menu item in Unity which is Assets -> Play Services Resolver -> Android Resolver -> Resolve. This will download and add required dependencies to your project. These will live under Assets/Plugins/Android.

  4. Build and run your application, and see if it works now.

I hope it can resolve the issue you're having just like it fixed mine!


It is because there is no such class in the apk built by Unity (with Internal build system).

You should put Kotlin related jars into "Assets\Plugins\Android\" folder.

In General, You can download kotlin-stdlib.jar & kotlin-stdlib-jdk7.jar & kotlin-reflect.jar form MVNrepository or somewhere. (These will depend on the references of your aar)

Hope this helps