Now that the Android program has been basically moved from Eclipse to Android studio, the recent project needs to use the NDK programming, so it was a bit of a toss.
Development environment
- Android Studio 1.5.1
- android-ndk-r10e
- Gradle 2.8
First we create a new project called Ndkdemo, after the creation of the project directory structure is as follows
Then we open the Gradle.properties file, write a sentence at the end of the file, if not write this sentence will compile does not pass
android.useDeprecatedNdk=true
Then configure the NDK path, click File in the upper left corner and select Project Structure (or simply click the button with the red box callout) to enter the configuration interface and configure the downloaded NDK path in the red box.
To this, the configuration work is completed, the following development-related work.
I'm sure the NDK programmer at Eclipse knows we're going to write android.mk, but in Android Studio we don't have to write it at all, we just need to write a few simple statements under Gradle. Studio will automatically help us write the android.mk, very convenient.
defaultConfig { ... ndk{ "ndk-demo" //要引用module的名字 "log" //引用库的名字(选填) "armeabi" //配置需要编译出那些版本的so库(如果不写,就编译出所有版本的so库) } }
The above is the Build.gradle configuration of the app directory, just write the NDK-related configuration under the Defaultconfig node.
Then we create a new Comman class, write a local function, system.loadlibrary the parameters are just in Build.gradle configuration of the ModuleName
publicclass Command { publicstaticgetStringFromC(); static{ System.loadLibrary("ndk-demo"); }}
Then we right-click the app directory and choose Create a new JNI directory
Click on the Console tab below and type in the command, and you'll find that the magic in the JNI directory automatically generates the relevant. h file
Explain the meaning of two lines of command. The first sentence means entering into the App/src/main/java directory. In the second sentence, Javah is a tool for the JDK to generate a header file,-D. /jni indicates that the generated file is to be placed under the Jni folder of the previous directory, and Com.zwf.ndkdemo.ndk.Command is the full path of the command we just wrote.
With the header file we can start to write the relevant C file, under the Jni folder to create a new C file with just the name of the header file, write related functions.
Click to run, Success!
So where do we find the so library?
Complete! Demo Source
Android Studio NDK programming