本文通过一个简单的java调用c语言的例子来介绍NDK的使用流程
使用流程
- 创建一个Android项目
- 创建native方法
- 编译生成头文件 .h 文件
- 编写c文件
- 编译c生成so文件,调用
1.创建一个Android项目
2.创建native方法:
public class MainActivity extends Activity {
public static native String getStringFromC();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
3.利用NDK编译生成头文件,先cd到Android项目的路径中,用javah命令生成头文件,格式为:
javah -classpath 路径1 -d 路径2
例子:
javah -classpath bin/classes;E:\Eclipse\sdk\platforms\android-19\android.jar -d jni com.example.myhellojni.MainActivity
bin/classes;E:\Eclipse\sdk\platforms\android-19\android.jar classes文件加 指定一个android.jar包
jni com.example.myhellojni.MainActivity jni 目录加 java文件的包名+类名
图片是我自己的例子,敲回车不报错就说明头文件生成了
头文件如下:
结尾处是自动创建的相应的c函数,格式是Java_包名_类名_方法名
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_myhellojni_MainActivity */
#ifndef _Included_com_example_myhellojni_MainActivity
#define _Included_com_example_myhellojni_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
#undef com_example_myhellojni_MainActivity_MODE_PRIVATE
#define com_example_myhellojni_MainActivity_MODE_PRIVATE 0L
#undef com_example_myhellojni_MainActivity_MODE_WORLD_READABLE
#define com_example_myhellojni_MainActivity_MODE_WORLD_READABLE 1L
#undef com_example_myhellojni_MainActivity_MODE_WORLD_WRITEABLE
#define com_example_myhellojni_MainActivity_MODE_WORLD_WRITEABLE 2L
#undef com_example_myhellojni_MainActivity_MODE_APPEND
#define com_example_myhellojni_MainActivity_MODE_APPEND 32768L
#undef com_example_myhellojni_MainActivity_MODE_MULTI_PROCESS
#define com_example_myhellojni_MainActivity_MODE_MULTI_PROCESS 4L
#undef com_example_myhellojni_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING
#define com_example_myhellojni_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L
#undef com_example_myhellojni_MainActivity_BIND_AUTO_CREATE
#define com_example_myhellojni_MainActivity_BIND_AUTO_CREATE 1L
#undef com_example_myhellojni_MainActivity_BIND_DEBUG_UNBIND
#define com_example_myhellojni_MainActivity_BIND_DEBUG_UNBIND 2L
#undef com_example_myhellojni_MainActivity_BIND_NOT_FOREGROUND
#define com_example_myhellojni_MainActivity_BIND_NOT_FOREGROUND 4L
#undef com_example_myhellojni_MainActivity_BIND_ABOVE_CLIENT
#define com_example_myhellojni_MainActivity_BIND_ABOVE_CLIENT 8L
#undef com_example_myhellojni_MainActivity_BIND_ALLOW_OOM_MANAGEMENT
#define com_example_myhellojni_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
#undef com_example_myhellojni_MainActivity_BIND_WAIVE_PRIORITY
#define com_example_myhellojni_MainActivity_BIND_WAIVE_PRIORITY 32L
#undef com_example_myhellojni_MainActivity_BIND_IMPORTANT
#define com_example_myhellojni_MainActivity_BIND_IMPORTANT 64L
#undef com_example_myhellojni_MainActivity_BIND_ADJUST_WITH_ACTIVITY
#define com_example_myhellojni_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L
#undef com_example_myhellojni_MainActivity_CONTEXT_INCLUDE_CODE
#define com_example_myhellojni_MainActivity_CONTEXT_INCLUDE_CODE 1L
#undef com_example_myhellojni_MainActivity_CONTEXT_IGNORE_SECURITY
#define com_example_myhellojni_MainActivity_CONTEXT_IGNORE_SECURITY 2L
#undef com_example_myhellojni_MainActivity_CONTEXT_RESTRICTED
#define com_example_myhellojni_MainActivity_CONTEXT_RESTRICTED 4L
#undef com_example_myhellojni_MainActivity_RESULT_CANCELED
#define com_example_myhellojni_MainActivity_RESULT_CANCELED 0L
#undef com_example_myhellojni_MainActivity_RESULT_OK
#define com_example_myhellojni_MainActivity_RESULT_OK -1L
#undef com_example_myhellojni_MainActivity_RESULT_FIRST_USER
#define com_example_myhellojni_MainActivity_RESULT_FIRST_USER 1L
#undef com_example_myhellojni_MainActivity_DEFAULT_KEYS_DISABLE
#define com_example_myhellojni_MainActivity_DEFAULT_KEYS_DISABLE 0L
#undef com_example_myhellojni_MainActivity_DEFAULT_KEYS_DIALER
#define com_example_myhellojni_MainActivity_DEFAULT_KEYS_DIALER 1L
#undef com_example_myhellojni_MainActivity_DEFAULT_KEYS_SHORTCUT
#define com_example_myhellojni_MainActivity_DEFAULT_KEYS_SHORTCUT 2L
#undef com_example_myhellojni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL
#define com_example_myhellojni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef com_example_myhellojni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL
#define com_example_myhellojni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
/*
* Class: com_example_myhellojni_MainActivity
* Method: getStringFromC
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_myhellojni_MainActivity_getStringFromC
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
4.编写c文件
返回一个字符串
hello.c
#include<stdio.h>
#include<stdlib.h>
#include "com_example_myhellojni_MainActivity.h"
JNIEXPORT jstring JNICALL Java_com_example_myhellojni_MainActivity_getStringFromC(
JNIEnv *env, jclass jclass) {
return (*env)->NewStringUTF(env, "Hello NDK");
}
# 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.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello //生成的so文件名
LOCAL_SRC_FILES := hello.c //目标文件
include $(BUILD_SHARED_LIBRARY)
APP_ABI := all
意思是编译全部的平台
5.编译so
在Android项目的目录下执行ndk-build
不出错so文件就生成了。
调用:
public class MainActivity extends Activity {
public static native String getStringFromC();
private TextView mTextView;
static {
// 调用库文件
System.loadLibrary("hello");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textview1);
// 调用方法
mTextView.setText(getStringFromC());
}
}