Berikut adalah salah satu cara menggunakan Android NDK:
- Buat program Java.
- Buat program C/C++.
- Generate header.
Contoh:javah -o HalloJNI.h -jni -classpath ../src com.example.hallojni.TestJNI
HalloJNI.h -> File hasil generate.
com.example.hallojni.TestJNI -> class TestJNI.java - Include header file pada program C/C++;
- Jalankan program.
Beberapa error yang mungkin ditemui:
01-01 09:56:00.686: E/AndroidRuntime(3229): FATAL EXCEPTION: main 01-01 09:56:00.686: E/AndroidRuntime(3229): java.lang.UnsatisfiedLinkError: getMessage 01-01 09:56:00.686: E/AndroidRuntime(3229): at com.example.hallojni.TestJNI.getMessage(Native Method) 01-01 09:56:00.686: E/AndroidRuntime(3229): at com.example.hallojni.MainActivity.onCreate(MainActivity.java:19) 01-01 09:56:00.686: E/AndroidRuntime(3229): at android.app.Activity.performCreate(Activity.java:4492) 01-01 09:56:00.686: E/AndroidRuntime(3229): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 01-01 09:56:00.686: E/AndroidRuntime(3229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 01-01 09:56:00.686: E/AndroidRuntime(3229): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 01-01 09:56:00.686: E/AndroidRuntime(3229): at android.app.ActivityThread.access$600(ActivityThread.java:123) 01-01 09:56:00.686: E/AndroidRuntime(3229): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 01-01 09:56:00.686: E/AndroidRuntime(3229): at android.os.Handler.dispatchMessage(Handler.java:99) 01-01 09:56:00.686: E/AndroidRuntime(3229): at android.os.Looper.loop(Looper.java:137) 01-01 09:56:00.686: E/AndroidRuntime(3229): at android.app.ActivityThread.main(ActivityThread.java:4424) 01-01 09:56:00.686: E/AndroidRuntime(3229): at java.lang.reflect.Method.invokeNative(Native Method) 01-01 09:56:00.686: E/AndroidRuntime(3229): at java.lang.reflect.Method.invoke(Method.java:511) 01-01 09:56:00.686: E/AndroidRuntime(3229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 01-01 09:56:00.686: E/AndroidRuntime(3229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-01 09:56:00.686: E/AndroidRuntime(3229): at dalvik.system.NativeStart.main(Native Method)
Solusi:
- Pastikan nama class sama.
- Pastikan header file sudah di include.
Recent Comments