The process of adding jni to android studio native java project


1, Configuration steps:

The first step is to create a cpp folder in / src/main /
The second step is to create two files in the folder, one is native-lib.cpp and CMakeLists.txt
Step 3 add the following contents to the above two documents:

#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
cmake_minimum_required(VERSION 3.4.1)

add_library( # Sets the name of the library.
             # Sets the library as a shared library.
             # Provides a relative path to your source file(s).
             native-lib.cpp )
find_library( # Sets the name of the path variable.
              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )
target_link_libraries( # Specifies the target library.
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

Step 4: add relevant information of CMakeLists.txt and native-lib.cpp to the build.gradle file under src directory for association. The specific steps are as follows:

(1) Add in the defaultConfig subkey to support C + + compilation options:

  externalNativeBuild {
      cmake {
          cppFlags "-std=c++11"

(2) Add the associated CMakeLists.txt information in the android subitem, where the Cmake version number can be left blank

    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.10.2'

(3) Click Sync Now in the upper right corner of the file to synchronize


2, Problems encountered in compilation

1. Generally, if our project configuration is generated by our own IDE, there will be no error
2. If you take the old project or the project generated by others and then add JNI support, the following errors may be reported:
(1) Errors like the following may be caused by the difference in the version number of gradle in another general build.gradle in the project.
At this time, there should be no problem to modify the version to match your ide environment. Since I re create a native C + + project, the default version of the IDE is 4.0.1, so I modify 3.2.1 of the original old project to 4.0.1, and then synchronize it.

A problem occurred configuring project ':app'.
> java.lang.NullPointerException (no error message)
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':app'.
	at org.gradle.configuration.project.LifecycleProjectEvaluator.wrapException(
	... 115 more

(2) If there are other problems, you may need to pay attention to the following points, which should be helpful to the configuration:
1. Is the ndk version correct
2. Whether the configured ndk path is correct, including the path in the system environment variable
3. Is the CMake version correct

3, Summary

This paper mainly records the problems and solutions of adding JNI support to the original native Java Android project. If the direct new JNI project is not within the scope of this paper, I hope to provide an effective solution for developers who encounter such problems in daily development.

Reference link:

Tags: Java C++ Android JNI

Posted on Fri, 10 Sep 2021 03:23:38 -0400 by kyin