javaandroidkotlincocos2d-x

Calling Kotlin from Java (cannot find symbol)


I am trying to call a function in Kotlin from Java.
Documentation says that it is easy. I have follow this post without luck so far.

https://kotlinlang.org/docs/java-to-kotlin-interop.html#package-level-functions

When typing in Java, the IDE recognizes the class but when I compile it says error: cannot find symbol

Any help is more than welcome.

app.kt

package org.cocos2dx.cpp

fun getTime() { /*...*/ }

AppActivity.java

    public void test1() {
        org.cocos2dx.cpp.AppKt.getTime();
    }

Project structure

/proj.android
   /app
       /src
          /org.cocos2dx.cpp
              app.kt
              AppActivity

project level build.gradle

buildscript {
    ext.kotlin_version = "1.5.0"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.8'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.0'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral() 
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Module level build.gradle

import org.gradle.internal.os.OperatingSystem

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()

    defaultConfig {
        applicationId "demo.1"
        minSdkVersion PROP_MIN_SDK_VERSION
        targetSdkVersion PROP_TARGET_SDK_VERSION
        versionCode 43
        versionName "0.43"

        externalNativeBuild {
            cmake {
                targets 'MyGame'
                arguments "-DCMAKE_FIND_ROOT_PATH=", "-DANDROID_STL=c++_static", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_NEON=TRUE"
                cppFlags "-frtti -fexceptions -fsigned-char"
            }
        }

        ndk {
            abiFilters = []
            abiFilters.addAll(PROP_APP_ABI.split(':').collect{it as String})
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    sourceSets.main {
        java.srcDir "src"
        res.srcDir "res"
        manifest.srcFile "AndroidManifest.xml"
        assets.srcDir "../../Resources"
    }

    externalNativeBuild {
        cmake {
            path "../../CMakeLists.txt"
        }
    }

    signingConfigs {
       release {
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                storeFile file(RELEASE_STORE_FILE)
                storePassword RELEASE_STORE_PASSWORD
                keyAlias RELEASE_KEY_ALIAS
                keyPassword RELEASE_KEY_PASSWORD
            }
        }
    }

    buildTypes {
        release {
            debuggable false
            jniDebuggable false
            renderscriptDebuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                signingConfig signingConfigs.release
            }
        }

        debug {
            debuggable true
            jniDebuggable true
            renderscriptDebuggable true
        }
    }
}

android.applicationVariants.all { variant ->
    def project_root_folder = "${projectDir}/../.."
    def dest_assets_folder = "${projectDir}/assets"

    delete dest_assets_folder
    def targetName = variant.name.capitalize()
    def copyTaskName = "copy${targetName}ResourcesToAssets"
    
    tasks.register(copyTaskName) {
        copy {
            from "${buildDir}/../../../Resources"
            into "${buildDir}/intermediates/assets/${variant.dirName}"
            exclude "**/*.gz"
        }
    }
    tasks.getByName("pre${targetName}Build").dependsOn copyTaskName
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':libcocos2dx')
    implementation 'androidx.core:core:1.0.0'
    implementation platform('com.google.firebase:firebase-bom:28.1.0')
    implementation 'com.google.firebase:firebase-crashlytics-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'
}

Error

error: cannot find symbol
        org.cocos2dx.cpp.AppKt.getTime();
                        ^
  symbol:   class AppKt
  location: package org.cocos2dx.cpp

Solution

  • Your code looks fine; but you need to have the project level build.gradle class path:

    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    buildscript {
        ext.kotlin_version = "1.5.0"
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath "com.android.tools.build:gradle:4.2.0"
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    
        }
    }
    

    And the Kotlin standard library dependencies in build.gradle module level:

    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 
    }
    

    Finally, add the following to the module-level build.gradle at top-level of the file:

    apply plugin: "kotlin-android"