AAR library with Kotlin

Hi,
I wrote a library in java that uses a java sdk (estimote mirror sdk) but this sdk is partly written in Kotlin.
I put all the AAR files in my unity project folder and I build with gradle.
The problem is that I can figure out how to include Kotlin so when I run my project I have this error. I posted on estimote forum and they redirect me here for this issue, see the original post

If someone can help me :slight_smile:

1 Like

Hi, I wrote a library in Kotlin, then I put the AAR files in unity project, build APK and run on my android devices, the error as follow:

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

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>
  1. 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.

  2. 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!

2 Likes

In step 2, it will stuck in 96% when resolving dependencies with following $XDependencies.xml

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

I think it is a typo. It can resolve succeeded after I modify the file as following:

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