Android JNI详述
2010-07-15 20:31:00 来源:WEB开发网二. 使用NDK 生成本地方法(ubuntu and windows )
1. 安装NDK :解压,然后进入NDK 解压后的目录,运行build/host-setup.sh (需要Make 3.81 和awk )。若有错,修改host-setup.sh 文件:将#!/bin/sh 修改为#!/bin/bash ,再次运行即可。
2. 在apps 文件夹下建立自己的工程文件夹,然后在该文件夹下建一文件Application.mk 和项project 文件夹。
Application.mk 文件:
APP_PROJECT_PATH := $(call my-dir)/project
APP_MODULES := myjni
3. 在project 文件夹下建一jni 文件夹,然后新建Android.mk 和myjni.c 。这里不需要用javah 生成相应的.h 文件,但函数名要包含相应的完整的包、类名。
4. 编辑相应文件内容。
Android.mk 文件:
# 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 := myjni
LOCAL_SRC_FILES := myjni.c
include $(BUILD_SHARED_LIBRARY)
myjni.c 文件:
#include
#include
jstring
Java_com_hello_NdkTest_NdkTest_stringFromJNI( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from My-JNI !");
}
myjni 文件组织:
更多精彩
赞助商链接