Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AAR library with Kotlin

Discussion in 'Formats & External Tools' started by ThibaultAr, Feb 1, 2018.

  1. ThibaultAr

    ThibaultAr

    Joined:
    Feb 1, 2018
    Posts:
    1
    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 :)
     
    PierreNoel likes this.
  2. liusilong007

    liusilong007

    Joined:
    Jul 7, 2015
    Posts:
    1
    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:

    Code (CSharp):
    1. AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
    2.                                    java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
    3.                                        at com.lsl.plugin.PluginActivity.showToast(PluginActivity.kt)
    4.                                        at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    5.                                        at com.unity3d.player.UnityPlayer.c(Unknown Source)
    6.                                        at com.unity3d.player.UnityPlayer$c$1.handleMessage(Unknown Source)
    7.                                        at android.os.Handler.dispatchMessage(Handler.java)
    8.                                        at android.os.Looper.loop(Looper.java)
    9.                                        at com.unity3d.player.UnityPlayer$c.run(Unknown Source)
    10.                                     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]]
    11.                                       at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    12.                                       at java.lang.ClassLoader.loadClass(ClassL
     
  3. Behisoft

    Behisoft

    Joined:
    Jul 14, 2014
    Posts:
    1
    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)
    Code (Boo):
    1.     <dependencies>
    2.       <androidPackages>
    3.         <androidPackage spec="org.jetbrains.kotlin:kotlin-stdlibjdk7:1.2.61" />
    4.       </androidPackages>
    5.     </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!
     
    mfdeveloper and behnam-lum like this.
  4. charlieyangasus

    charlieyangasus

    Joined:
    Nov 12, 2019
    Posts:
    1
    In step 2, it will stuck in 96% when resolving dependencies with following $XDependencies.xml
    Code (Boo):
    1.     <dependencies>
    2.       <androidPackages>
    3.         <androidPackage spec="org.jetbrains.kotlin:kotlin-stdlibjdk7:1.2.61" />
    4.       </androidPackages>
    5.     </dependencies>
    I think it is a typo. It can resolve succeeded after I modify the file as following:
    Code (Boo):
    1.     <dependencies>
    2. <dependencies>
    3.   <androidPackages>
    4.     <androidPackage spec="org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.61" />
    5.   <androidPackages>
    6. </dependencies>
    7.