Android JNI practice use Android Studio 1.5
=================
1.creata an [empty activiry] android project.
2.set textview id to TextView1.
3.add below code in MainActivity.java, before onCreate()
--
static {
System.loadLibrary("JniDemo");
}
public native String getStringFromNative();
--
4.use Terminal, switch path to app/src/main/java
submit command to create com_usi_testjni_MainActivity.h
javah -jni com.usi.testjni.MainActivity
5.create JNI folder under main.
Project-> Testjni > app > main, right click >new > folder > JNIfolder
move main\com_usi_testjni_MainActivity.h
to
jni\com_usi_testjni_MainActivity.h
6.create HelloNDK.c undr jni folder
jni > right click > new > file > file name HelloNDK.c
add below code in HelloNDK.c
--
#include <com_usi_testjni_MainActivity.h>
JNIEXPORT jstring JNICALL Java_com_usi_testjni_MainActivity_getStringFromNative
(JNIEnv * evn, jobject obj)
{
return (*evn)->NewStringUTF(evn,"Hello NKD demo!!!!!!!!!!!!");
}
--
7.add ndk.dir,sdk.dir in local.properties
open Testjni > local.properties, add
ndk.dir=C\:\\Users\\mark\\AppData\\Local\\Android\\sdk\\ndk-bundle
sdk.dir=C\:\\Users\\mark\\AppData\\Local\\Android\\sdk
8.add android.useDeprecatedNdk=true under Testjni >gradle.properties
open Testjni > gradle.properties, add
android.useDeprecatedNdk=true
9.add ndk module name in app > build.gradle
open Testjni > app > build.gradle, add
--
ndk{
moduleName "JniDemo"
}
--
after versionName "1.0"
10.add code to show ndk message in MainActivity.java
open MainActivity.java, add below code in onCreate function.
--
TextView tmpview = (TextView)findViewById(R.id.TextView1);
tmpview.setText(getStringFromNative());
--
11.now android app can call c/c++ function via JNI.
On device, result show as below:
Note:
.if javah can not find ,please check your java path and add to PATH fow windows platform.
my java is locate
c:\Program Files\Java\jdk1.7.0_75\bin\
.if [Error: Cannot determine signature for Bitmap message] show , try below to solve it
solve method:
javah -classpath C:\Users\mark\AppData\Local\Android\sdk\platforms\android-22\android.jar;. com.usi.imagerpreview.MainActivity
.default SDK path
C:\Users\mark\AppData\Local\Android\sdk
.default NDK path
C:\Users\mark\AppData\Local\Android\sdk\ndk-bundle
全站熱搜
留言列表