This code works on Java. But after migration to Kotlin, compiler higlits method native fun stringFromNative(): String
as error with following text:
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
}
}
Thanks @KenVanHoeylandt!
Andswer is:
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
}
}
If you wish to use this native function in another class you can specify the class which encloses it as in:
val aStringFromNative : String = MainActivity().getStringFromNative()
Put external fun stringFromNative(): String
outside of the companion object and into the MainActivity
.
(I found the answer by looking at https://github.com/ligee/kotlin-ndk-samples)
Kotlin :
call method from ndk file
external fun stringFromJNI(): String
load c++ file
companion object {
init {
System.loadLibrary("native-lib")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With