NativeScript / NativeScript/android

Kotlin issues (companion object)

Open
#1,806 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
563
Forks
144
Avg merge
10h 46m
Merged PRs (30d)
14

Description

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 8.6.5
  • Cross-platform modules: 8.6.2
  • Android Runtime: 8.6.1 (also 8.7.0-alpha.1)
  • iOS Runtime (if applicable): -
  • Plugin(s): -

Describe the bug
I tried to do some basic stuff with Kotlin stuff inside a NativeScript app (as I have no experience with Kotlin development) and I am experiencing some issues with companion objects.

I added this Kotlin file to App_Resources/Android/src/main/java

// KotlinTest.kt
package com.example

class KotlinTest {
    val hello = "Hello from Kotlin!"    //working

    @JvmField
    val instanceTestField : Int = 1234  //working

    companion object {
        @JvmStatic
        fun increment(num: Int): Int {  //working
            return num + 1
        }

        @JvmField
        val message : String = "Hi!"    //not working

        const val VERSION = 9           //not working
    }
}

And I am trying to access the values like this in the NativeScript app:

const testObject = new com.example.KotlinTest();

console.log(testObject.hello);                    //working
console.log(testObject.instanceTestField);        //working
console.log(com.example.KotlinTest.increment(5)); //working

console.log(com.example.KotlinTest.message);      //not working
console.log(com.example.KotlinTest.VERSION);      //not working

As far as I can read here about @JvmField annotation and here about const modifier this is valid Kotlin so it also shold work in NativeScript, but I always get undefined for those elements.

To Reproduce
Start the demo app provided below and check the logs in console.

Expected behavior
JvmFields and const values inside companion objects should be accessible in NativeScript.

Sample project
ns-android-kotlin.zip

Additional context

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Kotlin example in App_Resources/Android/src/main/java and the linked ns-android-kotlin.zip sample project. Run the demo app and inspect the console output for the companion-object fields, comparing them with the fields and method that work. Done means the @JvmField and const values are accessible from NativeScript as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.