Android NDK 开发(笔记一)

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

Android NDK 开发步骤:

  1. 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就能看到了。

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

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

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

  5. 根据生成的.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
  1. Activity中调用:
    这里写图片描述

  2. 运行一下,如果编译成功你会发现libs目录结构发生了变化
    这里写图片描述

效果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值