利用JNI技术在Android中调用C++代码

JNI是Java Native Interface的简称,也就是Java本地接口,它提供了若干的API实现Java和其它语言的通信(主要是C&C++)。

1、打开Eclipse,File-->New-->Project…-->AndroidApplication Project-->Application Name:TestJNI,PackageName:com.testjni.android, Minimum Required SDK:API 10, Android 2.3.3(Gingerbread),Next-->不勾选 Createcustom launcher icon, 勾选Create activity,Next-->选中 BlankActivity,Next-->Activity Name:TestJNIProjectActivity, -->Finish,运行AndroidApplication显示正确;

2、选中src-->com.testjni.android,打开TestJNIProjectActivity.java文件内代码改为:

package com.testjni.android;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class TestJNIProjectActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
    		TextView tv = new TextView(this);  
    		tv.setText(StringFromJni());  
    		setContentView(tv);  
	}

	public native String StringFromJni();

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

3、编译该工程,会在bin\classes\com\testjni\android文件夹下生成TestJNIProjectActivity.class文件;

4、打开命令行窗口,将其定位到\bin\classes目录下,输入命令:javah –classpath   D:\ProgramFiles\Android\android-sdk\platforms\android-10\android.jar;com.testjni.android.TestJNIProjectActivity,会在classes文件夹下生成com_testjni_android_TestJNIProjectActivity.h(说明:*.jar也可以是其它版本);

5、选中TestJNI工程,点击右键-->New-->Folder新建一个jni文件夹,选中jni, -->New-->File,新建2个文件,名称分别为hello-jni.c,Android.mk;

6、编写hello-jni.c文件,代码为:

#include <string.h>
#include <jni.h>

JNIEXPORT jstring JNICALL Java_com_testjni_android_TestJNIProjectActivity_StringFromJni
  (JNIEnv * env, jobject thiz)
{
     return (*env)->NewStringUTF(env, "Hello from JNI !");
}

7、 创建Android.mk文件,其内容为:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := hello-jni

LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)

8、利用NDK生成.so文件:选中工程,点击右键-->Properties-->Builders-->New,新建立一个Builder,在弹出的对话框上点中Program,点击OK;在弹出对话框EditConfiguration中,配置选项卡Main:Location中填入NDK安装目录,D:\ProgramFiles\Android\android-sdk\android-ndk-r9\ndk-build.cmd;WorkingDirectory中填入工程的根目录,E:\Test\Android\HelloJni,点击Apply;配置选项卡Refresh,勾选Refreshresources upon completion, The entire workspace, Recursively includesub-folders,点击Apply;配置Build Options选项卡,勾选Allocate Console(necessary for input), After a “Clean”, Duringmanual builds, During auto builds, Specify working set of relevant resources,点击SpecifyResources..,勾选TestJNI工程的jni目录,点击Finish,点击Apply,点击OK,会在\libs\armeabi目录下生成相应的libhello-jni.so库;

9、运行该工程,一切显示正确。

以上是.c文件的操作步骤,若将.c文件该为.cpp文件,则需改动两个文件:

1、Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := hello-jni

LOCAL_SRC_FILES := hello-jni.cpp

include $(BUILD_SHARED_LIBRARY)

2、hello-jni.cpp:

#include <string.h>
#include <jni.h>

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jstring JNICALL Java_com_example_testjni_TestJNIProjectActivity_StringFromJni
  (JNIEnv * env, jobject thiz)
{
     return env->NewStringUTF("Hello from JNI !");
}

#ifdef __cplusplus
}
#endif


参考文献:

1、  http://blog.csdn.net/haha_mingg/article/details/6332115

2、  http://blog.csdn.net/cghs123/article/details/7044826

3、   http://blog.csdn.net/laczff21/article/details/7542236

4、 android-ndk-r9-windows-x86_64中例子代码




你可能感兴趣的:(利用JNI技术在Android中调用C++代码)