接引:Android NDK r7以上集成了cywin,在开发NDK时不用那么麻烦的在去下载与配置cywin.

Android NDK 开发步骤:

  1. Eclipse 首先配置Ndk路径,Windows –>Preferences –>Android –>NDK ,指定NDK路径:如:D:\AndroidNDK\ndk-r10e
    Android NDK 开发(笔记一)_第1张图片
    如果在Windows – Preferences –>Android下没有看到NDK配置选项,那么请下载Eclipse–NDK插件,下载地址:http://pan.baidu.com/s/1dDXBKTN ,我是Win7 x64系统,所以请根据自己系统去下载相应的EclipseNdk插件。下载完成请将下载的xxxxxxxxxxxxndk.jar,拷贝,放到Eclipse根目录,如:D:\Androiddevelop\eclipse\plugins文件下,重启Eclipse就能看到了。

  2. 新建Android project 取名,AndroidNDKDemo,新建包名com.jni,新建JNI.java,在JNI中声明需要调用的C方法。
    这里写图片描述

  3. 选择工程,右键选择 Android Tools –>Add Native Support…,建立取名为hello-jni.so的so库。点击 Finish。
    Android NDK 开发(笔记一)_第2张图片
    这时你会发现在工程目录下,多出了一个jni目录与obj目录,目录中有两个文件分别是:Android.mk,与一个 hello.cpp文件。我这里直接将hello.cpp文件换成hello.c文件了。
    Android NDK 开发(笔记一)_第3张图片

  4. cmd 进入D:\workspace\AndroidJniTest\bin\
    javah -jni com.jni.JNI 生成hello.h文件。参考如图:
    这里写图片描述

  5. 根据生成的.h文件编译相关的hello.c文件。
    Android NDK 开发(笔记一)_第4张图片
    如果需要针对不同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"#endifreturn (*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
  1. Activity中调用:
    Android NDK 开发(笔记一)_第5张图片

  2. 运行一下,如果编译成功你会发现libs目录结构发生了变化
    Android NDK 开发(笔记一)_第6张图片

效果:
Android NDK 开发(笔记一)_第7张图片

更多相关文章

  1. android往文件中保存和读取数据
  2. Android 文件打开方式
  3. Android异步加载图片
  4. Android对图片局部扩大的实现
  5. android开发日常遇坑解决笔记
  6. android 获取服务器文件流 hander
  7. Android自学笔记(番外篇):全面搭建Linux环境(五)——Eclipse Helios(3.

随机推荐

  1. Android sensor介绍
  2. [Android实例] android多点触摸demo
  3. mmsplayer V2 for android 抢先发布
  4. Android实现手机和电脑间的UDP网络通信
  5. Android的window类的常用方法
  6. android drawable 使用shape实现复杂图像
  7. Android 可下拉上拉效果的ScrollView
  8. Android 自定义控件之图片裁剪
  9. Android 高德地图 定位+围栏
  10. Android中Parcelable接口用法