Calling Kotlin static method

Hi!
I would like to call a static method in Kotlin from Unity but it’s throwing me the famous NoSuchMethodError.
I’m able to call no static functions without problem, and also I can call Java static and non static methods.

I tried the following .kt

package com.example.testkotlin
class MyClass {
    fun logHello() {
        println("Hello from Kotlin")
    }

    fun logHelloWithParams(name: String) {
        println("Hello from Kotlin $name")
    }

    fun returnValue(name: String): String {
        return "Hi $name. How Are you?"
    }

    companion object {
        fun execute() {
            println("Executing from inside a companion object")
        }
    }
}

And from C#

        const string TEST = "com.example.testkotlin.MyClass";
        public static void TestExecute()
        {
            using (var javaClass = new AndroidJavaClass(TEST))
                javaClass.CallStatic("execute");
        }

I had to add in the mainTemplate.gradle the kotlin dependency

Hello! Can u please tell me how do u added kotlin dependency?

Hi @miuoctav

You need to create a mainTemplate.gradle under Assets/Plugins/Android.
If you don’t have a mainTemplate already you can go to

/Applications/Unity/Hub/Editor/UNITY_VERSION/PlaybackEngines/AndroidPlayer/Tools/GradleTemplates/mainTemplate.gradle (in Mac)

Inside you will find a dependencies property, you need to add the following dependency:

implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.72'

thanks, dude

i copied it, but got an error after copy it in plugins/Android… :frowning: can u please provide the mainTTemplate pls?

This is the mainTemplate.gradle for a 2018.4.16 Unity, but they are similar, just check dependencies property.

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

buildscript {
    repositories {**ARTIFACTORYREPOSITORY**
        mavenCentral()
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'
**BUILD_SCRIPT_DEPS**}
}

allprojects {
    repositories {**ARTIFACTORYREPOSITORY**
        mavenCentral()
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

// Android Resolver Repos Start
([rootProject] + (rootProject.subprojects as List)).each { project ->
    project.repositories {
        def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")
        maven {
            url "https://maven.google.com"
        }
        mavenLocal()
        jcenter()
        mavenCentral()
    }
}
// Android Resolver Repos End
apply plugin: 'com.android.application'
**APPLY_PLUGINS**

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
// Android Resolver Dependencies Start
    implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.72'
// Android Resolver Dependencies End
**DEPS**}

// Android Resolver Exclusions Start
android {
  packagingOptions {
      exclude ('/lib/arm64-v8a/*' + '*')
      exclude ('/lib/armeabi/*' + '*')
      exclude ('/lib/mips/*' + '*')
      exclude ('/lib/mips64/*' + '*')
      exclude ('/lib/x86_64/*' + '*')
  }
}
// Android Resolver Exclusions End
android {
    compileSdkVersion **APIVERSION**
    buildToolsVersion '**BUILDTOOLS**'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion **MINSDKVERSION**
        targetSdkVersion **TARGETSDKVERSION**
        applicationId '**APPLICATIONID**'
        ndk {
            abiFilters **ABIFILTERS**
        }
        versionCode **VERSIONCODE**
        versionName '**VERSIONNAME**'
    }

    lintOptions {
        abortOnError false
    }

    aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**]
    }**SIGN**

    buildTypes {
        debug {
            minifyEnabled **MINIFY_DEBUG**
            useProguard **PROGUARD_DEBUG**
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD**
            jniDebuggable true
        }
        release {
            minifyEnabled **MINIFY_RELEASE**
            useProguard **PROGUARD_RELEASE**
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD****SIGNCONFIG**
        }
    }**PACKAGING_OPTIONS****SPLITS**
**BUILT_APK_LOCATION**
    bundle {
        language {
            enableSplit = false
        }
        density {
            enableSplit = false
        }
        abi {
            enableSplit = true
        }
    }
}**SPLITS_VERSION_CODE****REPOSITORIES****SOURCE_BUILD_SETUP**

after one week banging my head against the wall, your postings brings me to success.
If you were around I would invite you for a beer!!!
Thanks a lot!

1 Like

Really happy to hear that! :beers:

Can you do it without mainTTemplate? I want another way to do this, without mainTemplate.

If you are using External Dependency Manager you can create a dependency xml and add the kotlin dependency needed

implementation ‘org.jetbrains.kotlin:kotlin-stdlib:1.3.72’