4

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:

enter image description here

Liu Silong
  • 4,902
  • 3
  • 19
  • 28
  • Possible duplicate of [Gradle Project: java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics](https://stackoverflow.com/questions/44197521/gradle-project-java-lang-noclassdeffounderror-kotlin-jvm-internal-intrinsics) – Zoe Mar 08 '18 at 06:42
  • Thanks, I see how to use "Shadow Plugin" !http://imperceptiblethoughts.com/shadow/ – Liu Silong Mar 08 '18 at 08:49

5 Answers5

9

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!

Behnam Sattar
  • 136
  • 1
  • 4
  • This solution seems to be correct, but not working for me. Any updates on this? – Mahdi-Malv Jan 30 '19 at 10:34
  • @Malv can you specify what exactly doesn't work? Have you been able to add the dependencies using GoogleJarResolver? Are you still getting the same exception as the one mentioned in the question? – Behnam Sattar Feb 11 '19 at 16:06
  • No idea how. But the issue's been fixed for a while. – Mahdi-Malv Feb 11 '19 at 18:51
  • @BehnamSattar I'm new to Unity development but I need to create an AAR that will override the default Activity for an Android application. Trying to process MotionEvents and put into Unity using mUnityPlayer.onTouchEvent(). Some of the project was developed in Kotlin. The application using the AAR as a plugin gives the same java.lang.NoClassDefFoundError referring to Lkotlin I'm confused as to adding GoogleJarResolver into the process. Is it added into Unity after I've built the AAR file, or do I include it into the AndroidStudio project where I make the AAR? – kralvarado Jan 28 '20 at 19:03
  • @kralvarado The process I mentioned above is completely separate from creating the AAR file and adding it into your project. What GoogleJarResolver does is fetching and adding dependencies you want. You tell it what dependencies you need (by creating that $XDependencies.xml file) and then run it inside Unity. After it fetches the required dependencies. So make sure you know which packages your plugin depends on. In my case it was only Kotlin standard libraries. You need to create the xml file based on your plugin dependencies. – Behnam Sattar Feb 08 '20 at 22:25
  • Hi i followed the steps and even though the dependency is resolved, i still get the exception. Has anything changed since this answer was posted? – Dane Gillis Dec 14 '20 at 12:38
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

Ken Wu
  • 101
  • 4
0

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.

0

You can try to use External Dependency Manager for Unity.

DeMartery
  • 1
  • 2
-2

You cannot use Kotlin for Unity development.

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

Gabor
  • 7,352
  • 4
  • 35
  • 56