android JNI demo

本文介绍如何在Android应用中使用JNI技术调用C/C++代码,包括创建项目、编辑Java代码、生成并编辑JNI头文件、实现C/C++源代码及构建共享库等步骤。
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.youkuaiyun.com/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









评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值