标签:
Android NDK 开发步骤:
Eclipse 首先配置Ndk路径,Windows –>Preferences –>Android –>NDK ,指定NDK路径:如:D:\AndroidNDK\ndk-r10e
如果在Windows – Preferences –>Android下没有看到NDK配置选项,那么请下载Eclipse–NDK插件,下载地址:http://pan.baidu.com/s/1dDXBKTN ,我是Win7 x64系统,所以请根据自己系统去下载相应的EclipseNdk插件。下载完成请将下载的xxxxxxxxxxxxndk.jar,拷贝,放到Eclipse根目录,如:D:\Androiddevelop\eclipse\plugins文件下,重启Eclipse就能看到了。
新建Android project 取名,AndroidNDKDemo,新建包名com.jni,新建JNI.java,在JNI中声明需要调用的C方法。
选择工程,右键选择 Android Tools –>Add Native Support…,建立取名为hello-jni.so的so库。点击 Finish。
这时你会发现在工程目录下,多出了一个jni目录与obj目录,目录中有两个文件分别是:Android.mk,与一个 hello.cpp文件。我这里直接将hello.cpp文件换成hello.c文件了。
cmd 进入D:\workspace\AndroidJniTest\bin\
javah -jni com.jni.JNI 生成hello.h文件。参考如图:
根据生成的.h文件编译相关的hello.c文件。
如果需要针对不同cpu生成不同规格下的libhello.so则需要在hello.c文件中进行判断
hello.c
jstring Java_com_patch_JNI_Hello(JNIEnv* env, jobject obj) {
#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, "Yes ,Hello from JNI !");
}
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 := hello-jni
LOCAL_SRC_FILES := hello.c
include $(BUILD_SHARED_LIBRARY)
Application.mk
内容:
APP_ABI := all
Activity中调用:
运行一下,如果编译成功你会发现libs目录结构发生了变化
效果:
标签:
原文地址:http://blog.csdn.net/wxc880924/article/details/50508696