Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Calling Kotlin static method

Discussion in 'Android' started by Martin_Gonzalez, Aug 11, 2020.

  1. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    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
    Code (CSharp):
    1.  
    2. package com.example.testkotlin
    3. class MyClass {
    4.     fun logHello() {
    5.         println("Hello from Kotlin")
    6.     }
    7.  
    8.     fun logHelloWithParams(name: String) {
    9.         println("Hello from Kotlin $name")
    10.     }
    11.  
    12.     fun returnValue(name: String): String {
    13.         return "Hi $name. How Are you?"
    14.     }
    15.  
    16.     companion object {
    17.         fun execute() {
    18.             println("Executing from inside a companion object")
    19.         }
    20.     }
    21. }
    And from C#
    Code (CSharp):
    1.         const string TEST = "com.example.testkotlin.MyClass";
    2.         public static void TestExecute()
    3.         {
    4.             using (var javaClass = new AndroidJavaClass(TEST))
    5.                 javaClass.CallStatic("execute");
    6.         }
     
  2. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    I had to add in the mainTemplate.gradle the kotlin dependency
     
  3. miuoctav

    miuoctav

    Joined:
    Nov 21, 2016
    Posts:
    5
    Hello! Can u please tell me how do u added kotlin dependency?
     
  4. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    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:

    Code (CSharp):
    1. implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.72'
     
  5. miuoctav

    miuoctav

    Joined:
    Nov 21, 2016
    Posts:
    5
    thanks, dude
     
  6. miuoctav

    miuoctav

    Joined:
    Nov 21, 2016
    Posts:
    5
    i copied it, but got an error after copy it in plugins/Android... :( can u please provide the mainTTemplate pls?
     
  7. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    This is the mainTemplate.gradle for a 2018.4.16 Unity, but they are similar, just check dependencies property.

    Code (CSharp):
    1. // GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
    2.  
    3. buildscript {
    4.     repositories {**ARTIFACTORYREPOSITORY**
    5.         mavenCentral()
    6.         google()
    7.         jcenter()
    8.     }
    9.  
    10.     dependencies {
    11.         classpath 'com.android.tools.build:gradle:3.4.0'
    12. **BUILD_SCRIPT_DEPS**}
    13. }
    14.  
    15. allprojects {
    16.     repositories {**ARTIFACTORYREPOSITORY**
    17.         mavenCentral()
    18.         google()
    19.         jcenter()
    20.         flatDir {
    21.             dirs 'libs'
    22.         }
    23.     }
    24. }
    25.  
    26. // Android Resolver Repos Start
    27. ([rootProject] + (rootProject.subprojects as List)).each { project ->
    28.     project.repositories {
    29.         def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")
    30.         maven {
    31.             url "https://maven.google.com"
    32.         }
    33.         mavenLocal()
    34.         jcenter()
    35.         mavenCentral()
    36.     }
    37. }
    38. // Android Resolver Repos End
    39. apply plugin: 'com.android.application'
    40. **APPLY_PLUGINS**
    41.  
    42. dependencies {
    43.     implementation fileTree(dir: 'libs', include: ['*.jar'])
    44. // Android Resolver Dependencies Start
    45.     implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.72'
    46. // Android Resolver Dependencies End
    47. **DEPS**}
    48.  
    49. // Android Resolver Exclusions Start
    50. android {
    51.   packagingOptions {
    52.       exclude ('/lib/arm64-v8a/*' + '*')
    53.       exclude ('/lib/armeabi/*' + '*')
    54.       exclude ('/lib/mips/*' + '*')
    55.       exclude ('/lib/mips64/*' + '*')
    56.       exclude ('/lib/x86_64/*' + '*')
    57.   }
    58. }
    59. // Android Resolver Exclusions End
    60. android {
    61.     compileSdkVersion **APIVERSION**
    62.     buildToolsVersion '**BUILDTOOLS**'
    63.  
    64.     compileOptions {
    65.         sourceCompatibility JavaVersion.VERSION_1_8
    66.         targetCompatibility JavaVersion.VERSION_1_8
    67.     }
    68.  
    69.     defaultConfig {
    70.         minSdkVersion **MINSDKVERSION**
    71.         targetSdkVersion **TARGETSDKVERSION**
    72.         applicationId '**APPLICATIONID**'
    73.         ndk {
    74.             abiFilters **ABIFILTERS**
    75.         }
    76.         versionCode **VERSIONCODE**
    77.         versionName '**VERSIONNAME**'
    78.     }
    79.  
    80.     lintOptions {
    81.         abortOnError false
    82.     }
    83.  
    84.     aaptOptions {
    85.         noCompress = ['.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**]
    86.     }**SIGN**
    87.  
    88.     buildTypes {
    89.         debug {
    90.             minifyEnabled **MINIFY_DEBUG**
    91.             useProguard **PROGUARD_DEBUG**
    92.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD**
    93.             jniDebuggable true
    94.         }
    95.         release {
    96.             minifyEnabled **MINIFY_RELEASE**
    97.             useProguard **PROGUARD_RELEASE**
    98.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD****SIGNCONFIG**
    99.         }
    100.     }**PACKAGING_OPTIONS****SPLITS**
    101. **BUILT_APK_LOCATION**
    102.     bundle {
    103.         language {
    104.             enableSplit = false
    105.         }
    106.         density {
    107.             enableSplit = false
    108.         }
    109.         abi {
    110.             enableSplit = true
    111.         }
    112.     }
    113. }**SPLITS_VERSION_CODE****REPOSITORIES****SOURCE_BUILD_SETUP**
    114.  
     
  8. Baldor

    Baldor

    Joined:
    Jul 24, 2018
    Posts:
    4
    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!
     
    Martin_Gonzalez likes this.
  9. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    Really happy to hear that! :beers:
     
  10. DogF

    DogF

    Joined:
    Dec 31, 2013
    Posts:
    29
    Can you do it without mainTTemplate? I want another way to do this, without mainTemplate.
     
  11. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    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'