android JNI demo
16lz
2021-12-04
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
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(安卓)Api-比如文字识别、截屏等
- 通读Android文档系列 THREE
- 你不知道的Runnable接口,深度解析Runnable接口
- 【转】你不知道一些神奇Android(安卓)Api
- Android对Linux内核的改动你知道多少
- 还在杀进程?Android你不知道的那些事儿
- Android:Google的阳谋和Apache Harmony
- 那些你不知道的Android小事儿
- 为数不多的人知道的AndroidStudio快捷键(一)