Translated from official website getting Started with the NDK
The NDK starter documentation contains the following content
- Download NDK and Tools
- Create or import a local project
The Local Development Kit (NDK) is a collection of tools that can use C and C + + code in Android development, and you can use the Platform libraries it provides to manage local operations and access physical device components, such as sensors and touch input. The NDK is not suitable for most novice Android programmers, they only need to use Java Code and framework APIs to develop apps. Then, if your situation fits the cause, the NDK will become very useful.
- The device maximizes performance for low latency or for running compute-intensive applications, such as gaming or physical simulations.
- Reuse a C or C + + library that you or someone else has developed.
With Android Studio 2.2 and later, you can use the NDK to compile C and C + + code into a local library, and use Gradle to package your local library into your apk, which integrates the compilation system. Your Java code calls functions in the local library through the Java Local Interface (JNI) framework. To learn more about the Gradle and Android compilation system, read the build configuration.
The tool for Android studio to compile the local library by default is CMake. Android Studio also supports Ndk-build, because a large number of existing projects use the compilation Toolkit. However, if you are creating a local library in the new, you should use CMake.
This guide gives you the information to prepare to use the NDK and to use the NDK on Android studio and run it if you do not have the latest version of Android Studio, please download and install it now.
Note: If you don't want to use Android Studio, you can use the NDK by setting up Eclipse. However, we recommend migrating to the latest version of Android studio to support Android development, Eclipse's ADT plugin will no longer provide support.
Experiment Gradle User note: consider using the 2.2.0 version plugin or a higher version if any of the following are appropriate for you to compile your local library using CMake or Ndk-build: Your local project has used CMake or ndk-build; You prefer to use a stable version of the Gradle compilation system, or you want to support additional tools such as CCache; otherwise, you can continue to use the experimental version of the Gradle and Android plugin.
Download NDK and Tools
To compile and debug local code in the app, you need the following components:
- Android Local Development Kit (NDK): A toolset that allows the use of C and C + + code in Androi.
- CMake: An external build tool to build your local library with Gradle
- lldb: Debug software, which Android Studio uses to debug local code.
You can install these components by using the You can install these component SDK Manager:
- Open a project, choose on the Main menu, select Tools > Android > SDK Manager
- Click the SDK Tools tab.
- Check lldb, CMake, and NDK, next to the box, as shown in.
Figure 1. Install LLDB, CMake, and NDK from SDK Manager.
- Click Apply, and then click OKin the next dialog box.
- When the installation is complete, click Finish, then click OK.
Create or import a local project
Once you've set up Android studio, you can simply create a project that supports C + +. However, if you are adding or importing local code to an existing Android studio project, you need to follow these basic procedures:
- Create a new source file and add it to your Android studio project.
- If you already have local code or want to import a pre-built local library, you can skip this step:
- Create a CMake build script that tells CMake how to build your local source into the library. This build script is also required if you are importing and linking to a pre-build or platform library.
- You can skip this step if existing local libraries already have
CMakeLists.txt
build scripts or use Ndk-build and include Android.mk
build scripts.
- Gradle to your local library by providing a link to a cmake or Ndk-build script file path.
Note: If an existing project uses an obsolete ndkCompile
tool, you should open your build.properties
file and delete the following line of code before you configure Gradle to use CMake or Ndk-build
Remove this line
Android. =true
- Click Run to build and run your application. Gradle Add your cmake or ndk-build process as dependencies to compile, build and package your native library with your apk.
Once your app is running on a physical device or emulator, you can use Android studio to debug your app. Otherwise, to learn more about the NDK and its components, please read the overview page.
Introduction to the "Translate" ndk