本文实现在android app中使用调用jni库调用本地C/C++方法。
本例子在工程中新建 cn.landsem.jnistudy 包,在其中新建TestManager类用于调用本地C/C++方法,该类的代码如下:
package cn.landsem.jnistudy;
import android.util.Log;
public class TestManager {
public static String TAG = "TestManager";
static {
try {
System.loadLibrary("lstest");
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG,e.getMessage());
}
Log.d(TAG, "native_init");
nativeInit();
}
public int add(int i,int j) {
Log.d(TAG,"add");
return nativeAdd(i,j);
}
private static native void nativeInit();
private static native int nativeAdd(int i,int j);
}
打开dos命令窗口,切换到工程目录下的“bin\classes”目录,输入javah -jni cn.landsem.jnistudy.TestManager命令,命令执行成功后会在该目录下生成对应的jni头文件,如本文中完成上述命令会生成 cn_landsem_jnistudy_TestManager.h 文件,文件内容如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class cn_landsem_jnistudy_TestManager */
#ifndef _Included_cn_landsem_jnistudy_TestManager
#define _Included_cn_landsem_jnistudy_TestManager
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: cn_landsem_jnistudy_TestManager
* Method: nativeInit
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_cn_landsem_jnistudy_TestManager_nativeInit(JNIEnv *, jclass);
/*
* Class: cn_landsem_jnistudy_TestManager
* Method: nativeAdd
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_cn_landsem_jnistudy_TestManager_nativeAdd(JNIEnv *, jclass, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
在工程中新建jni目录,将上部操作中生成的jni头文件拷贝到该目录,新建一个c++源文件实现jni中定义的方法,如本文在jni目录下新建cn_landsem_jnistudy_TestManager.cpp文件用于实现上述方法,该方法实现代码如下:
#include "android_runtime/AndroidRuntime.h"
#include "JNIHelp.h"
#include "jni.h"
#include "utils/Log.h"
#include "utils/misc.h"
#include "cn_landsem_jnistudy_TestManager.h"
#include
#include
#include
#include
#include
#include
#include
#define LOG_TAG "Test_JNI"
JNIEXPORT void JNICALL Java_cn_landsem_jnistudy_TestManager_nativeInit
(JNIEnv *, jclass) {
ALOGD("call Java_cn_landsem_jnistudy_TestManager_nativeInit");
}
/*
* Class: cn_landsem_jnistudy_TestManager
* Method: native_add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_cn_landsem_jnistudy_TestManager_nativeAdd
(JNIEnv *, jclass, jint, jint) {
ALOGD("call Java_cn_landsem_jnistudy_TestManager_nativeAdd");
return 0;
}
在jni目录下新建Android.mk文件用于编译生成jni库,该文件内容如下:
# Copyright (C) 2010 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := cn_landsem_jnistudy_TestManager.cpp
LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
liblog \
LOCAL_MODULE:= liblstest
include $(BUILD_SHARED_LIBRARY)
在工程libs目录下新建armeabi目录和armeabi-v7a目录,将生成的jni库放到该目录下。
在工程中调用 TestManager 类即可测试。
源码可以从如下路径下载:http://download.csdn.net/detail/yxtouch/9620436