Kotlin and Unity development error

五迷三道 提交于 2020-12-31 05:48:36

问题


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(this@PluginActivity.applicationContext, 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:


回答1:


You cannot use Kotlin for Unity development.

JetBrains has no plans to work on Kotlin for .NET at this time.

  • from https://discuss.kotlinlang.org/t/kotlin-on-net-platform/2142/28



回答2:


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!




回答3:


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




回答4:


If your Android Studio version is not 3.0 and your kotlin is a plug-in,you maybe build project one more again.It means that every time you update the kotlin code or clean project,you hava to build the project twice.



来源:https://stackoverflow.com/questions/49164218/kotlin-and-unity-development-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!