1.Create android project : JNITest
packageName: com.lxy
Activity:JNITest.java

2.Edite java code:
JNITest.java as follow
==============================================================
package com.lxy;

import android.app.Activity;
import android.os.Bundle;

public class JNITest extends Activity {

static{
System.loadLibrary("jni-test");
}

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

this.setTitle(getString() + " 1 + 2 = " + add(1, 2));

}

public native String getString();
public native int add(int a, int b);
}
----------------------------------------------------------------

3.Build .h file
goto the project path in command line
cd ....../JNITest/
mkdir jni
javah -classpath bin -d jni com.lxy.JNITest
cd jni
ls
com_lxy_JNITest.h is existed.
vim com_lxy_JNITest.h
content as follow:
===============================================================
1 /* DO NOT EDIT THIS FILE - it is machine generated */
2 #include <jni.h>
3 /* Header for class com_lxy_JNITest */
4
5 #ifndef _Included_com_lxy_JNITest
6 #define _Included_com_lxy_JNITest
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 /*
11 * Class: com_lxy_JNITest
12 * Method: getString
13 * Signature: ()Ljava/lang/String;
14 */
15 JNIEXPORT jstring JNICALL Java_com_lxy_JNITest_getString
16 (JNIEnv *, jobject);
17
18 /*
19 * Class: com_lxy_JNITest
20 * Method: add
21 * Signature: (II)I
22 */
23 JNIEXPORT jint JNICALL Java_com_lxy_JNITest_add
24 (JNIEnv *, jobject, jint, jint);
25
26 #ifdef __cplusplus
27 }
28 #endif
29 #endif
----------------------------------------------------------------
closs the file.

4.Eidt jni-test.c (named by .so file's name) file according to formate .h file
vim jni-test.c
jin/jni-test.c as follow:
=============================================================
1 #include<string.h>
2 #include<jni.h>
3
4 jstring Java_com_lxy_JNITest_getString(JNIEnv * env, jobject thiz)
5 {
6 return (*env)->NewStringUTF(env,"o(∩∩)o...哈哈,我知道:");
7 }
8
9 jint Java_com_lxy_JNITest_add(JNIEnv * evn, jobject thiz, jint a, jint b)
10 {
11 return a + b;
12 }
----------------------------------------------------------------
save the file and close it

5.Create file named Android.mk
vim Android.mk
and content of jni/Android.mk as follow:
==============================================================
1 LOCAL_PATH:=$(call my-dir)
2
3 include $(CLEAR_VARS)
4
5 LOCAL_MODULE:=jni-test
6 LOCAL_SRC_FILES:=jni-test.c
7
8 include $(BUILD_SHARED_LIBRARY)
------------------------------------------------------------------
Whate is meaning of this sentence?

6.Build .so lib
cd ..
ndk-build
(About android-NDK and ndk-build Goto:
http://blog.csdn.net/liu_zhen_wei/article/details/6972517)

Compile thumb : jni-test <= /home/lxy/lxy/MyWorkspace/JNITest/jni/jni-test.c
SharedLibrary : libjni-test.so
Install : libjni-test.so => /home/lxy/lxy/MyWorkspace/JNITest/libs/armeabi

7.Refresh and run the project

pic

android JNI demo_第1张图片








更多相关文章

  1. Android根据不同语言切换图片
  2. Android 图片处理工具类汇总
  3. android里图片下载工具类AsyncImageLoader分析
  4. android 自由缩放图片
  5. android中实现图片的上下移动
  6. android图片压缩并转为base64字符串
  7. android > 图片旋转
  8. android 滚动的缓冲图片
  9. android:设计一个能在图片上涂鸦的程序。

随机推荐

  1. Android(安卓)aapt 工具介绍(转载)
  2. Android(安卓)下载文件 显示进度条
  3. android 常用代码备份
  4. Downloadmanager in android
  5. 时钟控件布局
  6. Android(安卓)studio报错:找不到匹配的任
  7. android 3.0以上对usb设备的访问USB
  8. android双击返回键退出程序
  9. Android(安卓)error:Lint found fatal er
  10. 判断应用是安装还是卸载了