jni代碼
java調用jni的接口:
1, 調用動態庫java
static{
System.loadLibrary(
"led_jni");
// system/lib/libled_jni.so
}
2, 調用驅動方法名:
public native int openDev();
3, 直接去調用,不須要去實現openDev()linux
openDev();
public native
int openDev();
public native
int DevOn();
public native
int DevOff();
public native
int closeDev();
jni的接口代碼: jni代碼(*.cpp)==>libled_jni.soc++
參考模板:Native.cpp development\samples\simplejni\Jni 2599 2011-5-17app
1, JNI_OnLoad()
<
== System.loadLibrary(
"led_jni") 第一個函數
|
原型
:jint JNI_OnLoad(JavaVM
* vm,
void
* reserved)
返回值
: 正確
: JNI_VERSION_1_4
錯誤
: 小於
0;
a, 獲取jni的版本,和dvm的運行環境對象 env (提供操做jni的方法)
原型
: jint GetEnv(
void
*
* env, jint version)
使用
:
JNIEnv
* env
= NULL;
vm
-
>GetEnv((
void
*
*)
&env, JNI_VERSION_1_4)
返回值
:
正確
: JNI_OK
==
0
錯誤
: 小於
0
b, 構建映射表, 註冊給dvm
表示映射表
:
typedef
struct {
const
char
* name;
// java方法名
const
char
* signature;
// 方法的參數和返回值 描述
void
* fnPtr;
// c/c++的函數名
} JNINativeMethod;
const
static JNINativeMethod myMethods[]
={
{
"openDev",
"()I", (
void
*)open_led},
{
"DevOn",
"()I", (
void
*)led_on},
{
"DevOff",
"()I", (
void
*)led_off},
{
"closeDev",
"()I", (
void
*)close_led},
};
註冊
:
env
-
>RegisterNatives(jclass clazz,
const JNINativeMethod
* methods,jint nMethods)
參數
1
: 將native方法所在的包.類轉換成jclass, 告訴dvm,本地方法是屬於哪一個類
jclass myclz
= env
-
>FindClass(
"com/hq/ledjnitest/LedActivity");
env
-
>RegisterNatives(myclz, myMethods,
sizeof(myMethods)
/
sizeof(myMethods[
0]));
Android.mk
LOCAL_PATH
:
= $(call my
-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES
:
= led_jni.cpp
LOCAL_MODULE
:
=libled_jni
#指定鏈接動態庫
< 相似於gcc
-lxx
LOCAL_SHARED_LIBRARIES
:
= \
libcutils
#指定頭文件路徑, 相似於gcc
-Ixxx
LOCAL_C_INCLUDES
+= \
$(JNI_H_INCLUDE)
#編譯成so,須要一個標誌
LOCAL_PRELINK_MODULE
:
=
false
include $(BUILD_SHARED_LIBRARY)
編譯:
mmm mytest/jni/
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
PLATFORM_VERSION_CODENAME
=REL
PLATFORM_VERSION
=
2.
1
-update1
TARGET_PRODUCT
=fs100
TARGET_BUILD_VARIANT
=eng
TARGET_SIMULATOR
=
false
TARGET_BUILD_TYPE
=release
TARGET_ARCH
=arm
HOST_ARCH
=x86
HOST_OS
=linux
HOST_BUILD_TYPE
=release
BUILD_ID
=ERE27
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
==
make
:進入目錄
'/home/ldswfun/src_s5pc/eclair_2.1_farsight'
target thumb C
++
: libled_jni
<
= mytest
/jni
/led_jni.cpp
In file included from mytest
/jni
/led_jni.cpp
:
5
:
dalvik
/libnativehelper
/
include
/nativehelper
/jni.h
:
489
: note
: the mangling of
'va_list' has changed in GCC
4.
4
target SharedLib
: libled_jni (out
/target
/product
/fs100
/obj
/SHARED_LIBRARIES
/libled_jni_intermediates
/LINKED
/libled_jni.so)
target Non
-prelinked
: libled_jni (out
/target
/product
/fs100
/symbols
/system
/lib
/libled_jni.so)
target Strip
: libled_jni (out
/target
/product
/fs100
/obj
/lib
/libled_jni.so)
Install
: out
/target
/product
/fs100
/system
/lib
/libled_jni.so
make
:離開目錄「
/home
/ldswfun
/src_s5pc
/eclair_2.
1_farsight」
運行:
apk
==
=
>
/system
/app
so
==
>
/syste
/lib
/
cp
-raf out
/target
/product
/fs100
/system
/lib
/libled_jni.so
/opt
/myrootfs
/system
/lib
@成鵬致遠函數
(blogs:http://lcw.cnblogs.com)spa
(email:wwwlllll@126.com)對象
(qq:552158509)blog