NativeScript / NativeScript/android

Kotlin issues (companion object)

Offen
#1,806 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
C++
Sterne
563
Forks
144
Ø Merge
10 Std. 46 Min.
Gemergte PRs (30 T.)
14

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Kotlin-Beispiel in App_Resources/Android/src/main/java und dem verknüpften ns-android-kotlin.zip-Beispielprojekt. Führe die Demo-App aus und überprüfe die Konsolenausgabe auf die companion-object-Felder. Vergleiche sie mit den Feldern und der Methode, die funktionieren. Erledigt bedeutet, dass die @JvmField- und const-Werte wie erwartet von NativeScript aus zugänglich sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
android, kotlin
Bereich
mobile
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.