NDK development _ Note 0, ndk Development notes
Google's attitude towards China, the world's second largest market, has been cool since Google's move out of China. However, on July 15, December 8, Beijing time, the Google 2016 Developer Conference was held in Beijing, and the Google developer website targeting China was launched: https://developers.google.cn /. Google's website domain name for Chinese developers ends with cn. The website contains all the products required by Google developers, including sdks, Android Studio, and APIs for search, MAP, Chrome, and other products required for Android development. They are all official and free to use.
OK. Before developing and learning the NDK, first download Android Stdio and SDK. I installed the latest version of AndroidStdio2.2.3.
Next, copy the notes for learning NDK from this website:
· Use Cases
· Download NDK and related tools
· Create a local project
· Build and run sample applications
·APK Analyzer
Getting Started
Native Development Kit (NDK) is a set of tools that allow android Developers to program using C/C ++ code.
Use Cases
1. The pursuit of high performance for devices (gaming, adding physical hardware, etc)
2. Reuse the C/C ++ library developed by yourself or others
The development environment of Android Stdio2.2 and later versions integrates the build system, you can use NDK to compile C/C ++ code into an native library and package it into your apk using the Gradle tool. In this case, your Java code can call functions in Native Library through the JNI interface.
To learn more about Gradle and android Build systems, visit Configure Your Build.
The default local library build tool for Android Stdio is CMake, but ndk-make is also supported, because many projects still use this tool to build local libraries. If you want to create a local library from scratch, use CMake.
Download NDK and related tools
To compile and debug local code, you need to download the following tools:
The Android Native Development Kit (NDK ):
A set of tools that allow you to use C/C ++ in Android.
CMake:
External build tools independent of Gradle are used to build your local library. If you want to build with ndk-build, you do not need to install this component.
LLDB:
Android Studio is a debugger used to debug local code.
You can install the above three components through SDK Manager:
Tools> Android> SDK Manager> SDK Tools
Select LLDB, CMake, and NDK for download.
Create a local project
Once you have installed the development environment, you can easily Create a Project that supports C/C ++. For detailed steps, see Create a New Project with C/C ++ Support.
Obtain the following project directory according to the operation requirements of the preceding website.
(1) In the cpp group, you can find all native source files, headers, and pre-built libraries of the project.
For a new project, Android Studio creates an example C ++ source file native-lib.cpp and places it in the src/main/cpp/directory of the application module. The code in this example provides a simple C ++ function stringFromJNI (), which can return the string "Hello from C ++ ". To learn how to add other source files to a project, see section about how to create a new Native source file.
(2) In the External Build Files group, you can find the build script of CMake or ndk-Build.
Like the build. gradle file instructs Gradle how to build an application, CMake and ndk-build require a build script to understand how to build your native library. For a new project, Android Studio creates a CMake build script CMakeLists.txt and places it in the module root directory. For more information about this build script, see section about how to create a Cmake build script.
Build and run sample applications
After you click Run to Run the application from the menu bar, Android Studio will build and start an application that displays the text "Hello from C ++" on my mobile phone.
The following overview describes the events that occur when you build and run an example application:
(1) Gradle calls your external build script CMakeLists.txt.
(2) CMake compiles the C ++ source file native-lib.cpp to the shared object library according to the command in the build script, and name it a libnative-lib.so, Gradle then encapsulates it into the APK.
(3) When running, the MainActivity of the application uses System. loadLibrary () to load the native library. Now, the application can use the library's native function stringFromJNI ().
(4) MainActivity. onCreate () calls stringFromJNI (). This will return "Hello from C ++" and update TextView with the text.
The following figure shows the operational flowchart based on your understanding. If it is not correct, you are welcome to criticize and correct it. Thank you.
Note: Instant Run is not compatible with projects using native code. Android Studio automatically disables this function.
APK Analyzer
If you want to verify that Gradle has encapsulated the native library into the APK, You can use APK Analyzer:
(1) Select Build> Analyze APK.
(2) Select apk from the app/build/outputs/APK/directory and click OK.
(3) 3, you will see the libnative-lib.so under lib/<ABI>/In the APK analyzer window.