前言
首先看一下项目目录:
jni文件夹下是用于调用pcap库的C文件。
Android.mk
文件开头必须是 LOCAL_PATH :=
(callmy−dir),这个命令的意思是当前目录就是Android.mk文件所在目录,接下来都相对于这个路径去寻找。第二行是:include
(CLEAR_VARS), GNU在makefile的时候会删除一些本地本来那个,这里会帮我们重新引入回来。
第三行是 LOCAL_MODULE:=,指定的是编译出的模块的名称,会根据当前是否有lib前缀选择是否添加。
第四行是 LOCAL_SRC_FILES:= ,指明了我们编写的C或C++本地方法的位置。
最后一行是 include $(BUILD_SHARED_LIBRARY),指明了一个GNU 编译的脚本,决定编译什么以及如何编译。
Ndk调用流程
首先还是要下载Ndk并配置环境,这部分内容比较简单不在这里做介绍。
编写Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
//主要在于找到Source File的位置。
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
编写Application.mk
//这个是和手机与CPU架构适配相关的,一般默认即可。
APP_ABI := all
本地方法
编写C、C++文件
/*
* Copyright (C) 2009 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.
*
*/
#include <string.h>
#include <jni.h>
/* This is a trivial JNI example where we use a native method
* to return a new VM String. See the corresponding Java source
* file located at:
*
* apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java
*/
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
#if defined(__arm__)
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a/NEON (hard-float)"
#else
#define ABI "armeabi-v7a/NEON"
#endif
#else
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a (hard-float)"
#else
#define ABI "armeabi-v7a"
#endif
#endif
#else
#define ABI "armeabi"
#endif
#elif defined(__i386__)
#define ABI "x86"
#elif defined(__x86_64__)
#define ABI "x86_64"
#elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */
#define ABI "mips64"
#elif defined(__mips__)
#define ABI "mips"
#elif defined(__aarch64__)
#define ABI "arm64-v8a"
#else
#define ABI "unknown"
#endif
return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI ".");
}
函数返回类型jstring,输入参数有一个jobject类型,这些类型是JNI的具体类型。可以参阅Oracle官方文档:http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html。
对于native方法的命名也有规则 限制:
1、将Java_放在最前面
2、添加对应的java方法的包名
3、添加响应的类名
4、添加其中的方法。
可见,最终方法名是 :Java_com_example_hellojni_HelloJni_stringFromJNI,对应包com.example.hellojni中的HelloJni类的stringFromJni方法。
实现Java方法
最后实现java方法:
/*
* Copyright (C) 2009 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.
*/
package com.example.hellojni;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class HelloJni extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* Create a TextView and set its content.
* the text is retrieved by calling a native
* function.
*/
TextView tv = new TextView(this);
tv.setText( stringFromJNI() );
setContentView(tv);
}
/* A native method that is implemented by the
* 'hello-jni' native library, which is packaged
* with this application.
*/
public native String stringFromJNI();
/* This is another native method declaration that is *not*
* implemented by 'hello-jni'. This is simply to show that
* you can declare as many native methods in your Java code
* as you want, their implementation is searched in the
* currently loaded native libraries only the first time
* you call them.
*
* Trying to call this function will result in a
* java.lang.UnsatisfiedLinkError exception !
*/
public native String unimplementedStringFromJNI();
/* this is used to load the 'hello-jni' library on application
* startup. The library has already been unpacked into
* /data/data/com.example.hellojni/lib/libhello-jni.so at
* installation time by the package manager.
*/
static {
System.loadLibrary("hello-jni");
}
}
静态代码块中加载了我们的库文件hello-jni,然后后面通过native关键字调用了方法。这里System.loadLibrary()中的参数,只要和我们的源文件名相同,本例中hello-jni.c对用hello-jni即可。