由于并不是每个app都会用到NDK,事实上,大多数app都不需要用到NDK,因此,NDK的安装独立成一节,不感兴趣的筒靴可以跳过,NDK的安装相对简单,只需要到NDK Downloads或者AndroidDevTools下载NDK的压缩包,然后解压到本地目录中即可。

    接着在Android工程根目录的local.properties文件中新增ndk.dir变量,其值指向ndk绝对路径,例如:

    1. ## This file is automatically generated by Android Studio.
    2. # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
    3. #
    4. # This file should *NOT* be checked into Version Control Systems,
    5. # as it contains information specific to your local configuration.
    6. #
    7. # Location of the SDK. This is only used by Gradle.
    8. # For customization when using a Version Control System, please read the
    9. # header note.
    10. sdk.dir=/Users/guhaoxin/Library/Android/sdk
    11. ndk.dir=/Users/guhaoxin/Library/Android/android-ndk-r10

    NDK的使用主要有两种形式,一种是拿到第三方的so文件,我们通过JNI封装其提供的native接口给Java层使用另一种是自己使用C/C++编写底层代码,然后编译生成so文件,接着重复第一种的做法。具体实现还是比较麻烦的,篇幅比较大,需要用到的筒靴可以参考以下这几篇文章:

    等有时间再结合实际项目整理一篇Android Studio+NDK的入门指引。