此代码适用于
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,Toast.LENGTH_LONG).show() } init { System.loadLibrary("_ndkkt") } external fun getStringFromNative(): String } }
解决方法
将外部有趣的stringFromNative():字符串放在随播对象之外并放入MainActivity中.
(我通过查看https://github.com/ligee/kotlin-ndk-samples找到了答案)