|
| 1 | +# Jni android sample |
| 2 | + |
| 3 | +JNI stands for Java Native Interface. This sample is showing how to call native c/c++ method to be used inside android. |
| 4 | +In order to run this sample, you need to install <b>NDK</b> in android SDK manager beforehand. |
| 5 | + |
| 6 | +If you looked through, the project contains c/c++ file which contains: |
| 7 | + |
| 8 | + #include <jni.h> |
| 9 | + #include "sample-jni.h" |
| 10 | + |
| 11 | + |
| 12 | + extern "C" |
| 13 | + JNIEXPORT jint JNICALL |
| 14 | + |
| 15 | + Java_com_pramonow_androidadvanced_JniActivity_getKey(JNIEnv* pEnv, jobject instance, jint key1) { |
| 16 | + return key1+200; |
| 17 | + } |
| 18 | + |
| 19 | +And the getKey function will be able to be called inside the activity: |
| 20 | + |
| 21 | + //Our Jni method |
| 22 | + external fun getKey(key:Int):Int |
| 23 | + |
| 24 | + companion object { |
| 25 | + init { |
| 26 | + System.loadLibrary("sample-jni") |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 31 | + super.onCreate(savedInstanceState) |
| 32 | + setContentView(R.layout.activity_jni) |
| 33 | + |
| 34 | + Toast.makeText(this,getKey(55).toString(),Toast.LENGTH_SHORT).show() // will show 255 |
| 35 | + } |
0 commit comments