由于并不是每个app都会用到NDK,事实上,大多数app都不需要用到NDK,因此,NDK的安装独立成一节,不感兴趣的筒靴可以跳过,NDK的安装相对简单,只需要到[NDK Downloads](https://developer.android.com/ndk/downloads/index.html)或者[AndroidDevTools](http://www.androiddevtools.cn/)下载NDK的压缩包,然后解压到本地目录中即可。
接着在Android工程根目录的local.properties文件中新增ndk.dir变量,其值指向ndk绝对路径,例如:
~~~
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/Users/guhaoxin/Library/Android/sdk
ndk.dir=/Users/guhaoxin/Library/Android/android-ndk-r10
~~~
NDK的使用主要有两种形式,一种是拿到第三方的so文件,我们通过JNI封装其提供的native接口给Java层使用另一种是自己使用C/C++编写底层代码,然后编译生成so文件,接着重复第一种的做法。具体实现还是比较麻烦的,篇幅比较大,需要用到的筒靴可以参考以下这几篇文章:
* [NDK with Android Studio](http://www.shaneenishry.com/blog/2014/08/17/ndk-with-android-studio/)
* [Android Studio, gradle and NDK integration](http://ph0b.com/android-studio-gradle-and-ndk-integration/)
* [Share code between projects in Android Studio](http://blog.gaku.net/share-code-between-projects-in-android-studio/)
* [Using custom Android.mk with Gradle/Android Studio](http://blog.gaku.net/ndk/)
等有时间再结合实际项目整理一篇Android Studio+NDK的入门指引。