android JNI 实现测试
来源:互联网 发布:小学知乎 编辑:程序博客网 时间:2024/06/05 08:29
1.定义JAVA类,内部具有native关键字函数
如:
package com.jni.jnitest;
public class Algorithm {
static {
System.loadLibrary("Algorithm");
}
public native int add(int a,int b);
public native int subtract(int a,int b);
}
2.用javah命令生成.h文件。
cd 到工程目录下,执行javah -classpath bin -d jni com.jni.jnitest.Algorithm 生成
bin目录同时生成com.jni.jnitest.Algorithm.h文件
如:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jni_jnitest_Algorithm */
#ifndef _Included_com_jni_jnitest_Algorithm
#define _Included_com_jni_jnitest_Algorithm
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_jni_jnitest_Algorithm
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_add
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_jni_jnitest_Algorithm
* Method: subtract
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_subtract
(JNIEnv *, jobject, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
3.实现com_jni_jnitest_Algorithm.h文件,创建com_jni_jnitest_Algorithm.c
如:
#include "com_jni_jnitest_Algorithm.h"
JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_add(JNIEnv *env,jobject c,jint a,jint b)
{
return (a+b);
}
JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_subtract(JNIEnv*,jobject c,jint a,jint b)
{
return (a-b);
}
4.定义mk文件
如:
#
# Copyright (C) 2008 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.
#
# This makefile supplies the rules for building a library of JNI code for
# use by our example of how to bundle a shared library with an APK.
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
# This is the target being built.
LOCAL_MODULE:= libAlgorithm
# All of the source files that we will compile.
LOCAL_SRC_FILES:= \
com_jni_jnitest_Algorithm.c
# All of the shared libraries we link against.
LOCAL_SHARED_LIBRARIES := \
libutils
# No static libraries.
LOCAL_STATIC_LIBRARIES :=
# Also need the JNI headers.
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE)
# No special compiler flags.
LOCAL_CFLAGS +=
# Don't prelink this library. For more efficient code, you may want
# to add this library to the prelink map and set this to true. However,
# it's difficult to do this for applications that are not supplied as
# part of a system image.
LOCAL_PRELINK_MODULE := false
include $(BUILD_SHARED_LIBRARY)
5.放到android源代码录目下,自己任意创建一个目录。进入目录,执行mm就可以生成.so了
然后将so push到手机中。 如:adb push libAlgorithm.so /system/lib/
6.创建应用程序就可以调用了。
如:
package com.jni.jnitest;
public class Algorithm {
static {
System.loadLibrary("Algorithm");
}
public native int add(int a,int b);
public native int subtract(int a,int b);
}
2.用javah命令生成.h文件。
cd 到工程目录下,执行javah -classpath bin -d jni com.jni.jnitest.Algorithm 生成
bin目录同时生成com.jni.jnitest.Algorithm.h文件
如:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jni_jnitest_Algorithm */
#ifndef _Included_com_jni_jnitest_Algorithm
#define _Included_com_jni_jnitest_Algorithm
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_jni_jnitest_Algorithm
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_add
(JNIEnv *, jobject, jint, jint);
/*
* Class: com_jni_jnitest_Algorithm
* Method: subtract
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_subtract
(JNIEnv *, jobject, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
3.实现com_jni_jnitest_Algorithm.h文件,创建com_jni_jnitest_Algorithm.c
如:
#include "com_jni_jnitest_Algorithm.h"
JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_add(JNIEnv *env,jobject c,jint a,jint b)
{
return (a+b);
}
JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_subtract(JNIEnv*,jobject c,jint a,jint b)
{
return (a-b);
}
4.定义mk文件
如:
#
# Copyright (C) 2008 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.
#
# This makefile supplies the rules for building a library of JNI code for
# use by our example of how to bundle a shared library with an APK.
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
# This is the target being built.
LOCAL_MODULE:= libAlgorithm
# All of the source files that we will compile.
LOCAL_SRC_FILES:= \
com_jni_jnitest_Algorithm.c
# All of the shared libraries we link against.
LOCAL_SHARED_LIBRARIES := \
libutils
# No static libraries.
LOCAL_STATIC_LIBRARIES :=
# Also need the JNI headers.
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE)
# No special compiler flags.
LOCAL_CFLAGS +=
# Don't prelink this library. For more efficient code, you may want
# to add this library to the prelink map and set this to true. However,
# it's difficult to do this for applications that are not supplied as
# part of a system image.
LOCAL_PRELINK_MODULE := false
include $(BUILD_SHARED_LIBRARY)
5.放到android源代码录目下,自己任意创建一个目录。进入目录,执行mm就可以生成.so了
然后将so push到手机中。 如:adb push libAlgorithm.so /system/lib/
6.创建应用程序就可以调用了。
- android JNI 实现测试
- android JNI测试记录
- Android的jni测试
- Android JNI用于驱动测试
- Android中JNI实现
- Android 远程调试 JNI 实现
- Android的JNI实现基础
- Android JNI的静态实现
- Android JNI的动态实现
- Android JNI实现计算器demo
- Android NDK JNI实现案例
- android studio中的JNI实现
- Android 远程调试 JNI 实现 ( Android JNI remote debugging )
- 实现android JNI 直接调用android驱动程序
- Android JNI和NDK学习(03)--动态方式实现JNI
- Android学习JNI,使用JNI实现字符串加密
- Android JNI和NDK学习(02)--静态方式实现JNI
- Android JNI和NDK学习(03)--动态方式实现JNI
- How to install a patch on clusterware 11.2.0.1 into grid infrastructure home
- Unable To Open Database After ASM Upgrade From Release 11.1 To Release 11.2
- 右上角关闭按钮的事件编辑
- 提示框 选择对话框 等待框
- jQuery 的 Div 标签滚动条属性及判断垂直滚动条是否到达底部
- android JNI 实现测试
- 链表反转
- poj 3624 Charm Bracelet(01背包入门题)
- com基础知识总结, 智能指针,匈牙利命名法
- 二进制文件读写
- 关于android2.3的overscroll
- POAPPLYWSIMPL1
- Upgrading ASM instance from Oracle 10.1 to Oracle 10.2. (Single Instance)
- subversion冲突解决