本文介绍了Kotlin和Unity开发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Android Studio和Unity开发,将库打包成aar文件,然后将aar文件作为Unity插件.当我使用Java时,没问题,但是当使用Kotlin时,它将引发异常.谢谢!

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!

例外:

 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

活动

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

}

.cs脚本

    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文件:

推荐答案

您不能将Kotlin用于Unity开发.

You cannot use Kotlin for Unity development.

10-29 06:49