NativeScript / NativeScript/android
Kotlin issues (companion object)
还没有人认领这个 Issue。
- 主要语言
- C++
- 星标
- 563
- 派生
- 144
- 平均合并
- 10 小时 46 分钟
- 30 天内合并 PR
- 14
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 App_Resources/Android/src/main/java 中的 Kotlin 示例和链接的 ns-android-kotlin.zip 示例项目开始。运行演示应用,检查控制台输出中的 companion-object 字段,并将其与可正常工作的字段和方法进行比较。当可以按预期从 NativeScript 访问 @JvmField 和 const 值时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- android, kotlin
- 领域
- mobile
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100