First, download the configuration android studio ndk
1. Open sdkManager download CMake and LLDB
2. Configure ndk
New Project
After the establishment of the project is completed, the project directory as follows, cpp folder is automatically generated
3. The method of custom navite
Then begin to write a native method custom Hello.java create a new file, write native methods which add a sum, as follows
Generate c ++ header files
Then execute javac hello.java in windows console Terminal hello.java enter the directory where the following
After the implementation of the same directory hello.java below will generate a Hello.class documents to prove this step is successful it!
Then the console or in the above Terminal fallback directory to app / src / main execute the following command
javah -d jni -classpath ./java com.example.myapplication.hello, followed by the package name + file name without the suffix, please change your package name, as
Generating a command executed before the app / src / main directory following a jni folder, a folder below .h file extension, as shown:
New cpp file method to achieve navtive
The
com_example_myapplication_hello.h
Copy the contents to myjni.cpp and add
com_example_myapplication_hello.h
Reference (reference marks to change, or will be reported in the following method oh red), delete other codes as
After the establishment of test Cmake must be obtained in order to compile cpp file
CMakeLists.txt configuration file
Next is key in android studio3.0 did not have before this step, we look at the project directory which has a CMakeLists.txt file, which is a new project is automatically generated, which has been written system automatically generated
cpp / native-lib.cpp configuration file, the file open CMakeLists.txt, as follows,
Call native methods
到此我们基本上已经成功了,然后在Hello.java中添加代码如下,添加的library名字一定要和上图中添加的一样:
最后在activity中调用add方法,
出错的地方总结:
cmake 编译会经常提示编译失败
出现cmake编译失败请在这里找寻错误