Moment For Technology

JNI development under Android Studio (II) : C/C++ code writing and compilation

Posted on Nov. 26, 2023, 10:23 p.m. by 李家瑋
Category: android Tag: c++ android Android Studio
Today send two basic JNI development and configuration of the article, may everyone in many application layer development, but I feel the JNI base configuration and principle of development it is necessary to know something about, just push two very basic, everyone can understand, if you feel in less than now, you can see two eyes to know a little bit about the underlying process, It might help you in the future.

With the launch of Android Studio, more and more developers are migrating their projects from Eclipse to Android Studio, showing that Android Studio has a clear advantage over Eclipse. Also for JNI development, Android Studio is much easier to configure than Eclipse. This article introduces the JNI development configuration for Android Studio.

First, directly use the. So library


Unlike Eclipse, library is placed under the jniLibs directory, as shown below:

Review images

It works the same way as in Eclipse, using System.loadLibrary("libName") to load libraries directly.

Second, use C/C++ source code  


1. Download, install and configure the NDK

The first step is to download the NDK. You can download the NDK yourself, then unpack it, then specify the NDK directory, and configure the path of the NDK in the file, as shown in the following figure:

Review images

You can also set it up in Android Studio to download the corresponding version of the NDK. The steps are as follows:

● On the menu bar, go to "File"-"Settings" to open the Settings screen.

● Go to "Appearance Behavior"-"System Settings"-"Android SDK" and switch to the "SDK Tool" TAB;

● Then find the NDK tick;

● Click "Apply" button, and then click "OK" in the pop-up window, it will automatically download;

● Wait for the automatic download and installation to complete, click "Finish" to complete the installation.

As shown below:

Review images

Review images

Review images

The default NDK installation directory for Android Studio is the SDK directory. After the installation is complete, the NDK path Settings in the file will be automatically updated.

2. Add NDK modules to Gradle

Open the build.gradle file under app module and add NDK module under defaultConfig module, as shown in the figure below:

ModuleName is the name of the LOCAL_MODULE variable in the android. mk file in Eclipse development, which corresponds to the name of loaded by system.loadLibrary (). For example, moduleName is set to "JniTest", so the file name is "libJniTest. So ", and the loading name is "system.loadLibrary ("JniTest");

AbiFilters specifies the platform to be allocated, or if not, all supported platforms will be compiled. Currently, seven supported platforms are armeabi, ArmeabI-V7A, ARM64-V8A, MIPS, MIPS64, x86, and X86_64.

LdLibs is the library to link to, which was originally specified by the LOCAL_LDLIBS variable in

3. Add C/C++ files

By default, C/C++ files are stored in the [module]/ SRC /main/jni/ directory, as shown below:

Review images

Of course, you can also modify the build.gradle configuration to specify other paths. In the "android.sourcesets. main" module, use "jni.srcDirs" to specify the path of jNI, as shown below:

Review images

This concludes the basic configuration of JNI development in Android Studio, and the next article will cover C/C++ coding and compilation in Android Studio.

Review images

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.