Android:如何从Kotlin调用ndk函数?

此代码适用于 Java.但是在迁移到Kotlin之后,编译器higlits方法本机乐趣stringFromNative():String作为错误,带有以下文本:

Function without a body must be abstract

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    Toast.makeText(this, stringFromNative(), Toast.LENGTH_LONG).show()
}

companion object {

    init {
        System.loadLibrary("_ndkkt")
    }
    native fun stringFromNative(): String
}
}

谢谢@KenVanHoeylandt!

Andswer是:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    Toast.makeText(this, stringFromNative(), Toast.LENGTH_LONG).show()
}

   init {
        System.loadLibrary("_ndkkt")
    }

    external fun getStringFromNative(): String
}

}
将外部有趣的stringFromNative():字符串放在随播对象之外并放入MainActivity中.

(我通过查看https://github.com/ligee/kotlin-ndk-samples找到了答案)

本站公众号
   欢迎关注本站公众号,获取更多程序园信息
开发小院