Add jni -------- static registration to the latest as project in 2020

Step 1: import static code block into so Library

static {
    System.loadLibrary("native-lib");
}

Step 2: write external functions

public native String getString();  //native stands for c\c + + implementation

Step 3: generate function header with javah

Enter the src/main/java directory of the project
 Execute javah JNI com.example.myapplication . mainactivity (need java to configure the environment)

Step 4: create a new C/C + + source code file

To add a new C/C + + source code file to an existing project, follow these steps:

If there is no cpp / directory in the main source code file set of your application, create one as follows:
Open the Project pane from the left side of the IDE and select Project view from the drop-down menu.
Go to your module > SRC, right-click the main directory, and select new > directory.
Enter cpp as the directory name, and then click OK.
Right click the cpp / directory and select new > C / C + + source file.
Enter a name for your source file, such as native lib.
From the Type drop-down menu, select a file extension for your source file, such as. cpp.
You can add other file types (such as. cxx or. hxx) to this drop-down menu by simply clicking the Edit File Types icon. In the pop-up C/C + + dialog box, select another file extension from the Source Extension and Header Extension drop-down menus, and then click OK.
If you also want to create a header file, select the Create an associated header check box.
Click OK.

Step 5: configure CMake

1. Open the Project pane from the left side of the IDE and select Project view from the drop-down menu.
2. Right click the root directory of your module and select new > file.
3. Input“ CMakeLists.txt ”As the filename, click OK.
Now you can configure your build script by adding the CMake command. To instruct CMake to create native libraries from native source code, add CMake to your build script_ minimum_ Required() and add_library() command:

    cmake_minimum_required(VERSION 3.4.1)

    add_library( # Specifies the name of the library.
                 native-lib

                 # Sets the library as a shared library.
                 SHARED

                 # Provides a relative path to your source file(s).
                 src/main/cpp/native-lib.cpp )

Using Add_ When Library () adds a source code file or library to the CMake build script, Android Studio also displays the related header file in the Project view after you synchronize the Project. However, in order for CMake to find the header file at compile time, you need to add include_directories() Command and specify the path to the header file:

 add_library(...)

    # Specifies a path to native header files.
    include_directories(src/main/cpp/include/)

Android NDK provides a set of native API s and libraries that you may find very useful. Through the CMakeLists.txt Script file contains NDK Library , you can use any of these API s.

Pre built NDK libraries already exist on the Android platform, so you don't need to build them or package them into the APK. Because these NDK libraries are already in the CMake search path, you don't even need to specify the location of the locally installed NDK library. You just need to provide CMake with the name of the library you want to use and associate it with your own native library.

Add to CMake build script find_library() Command to find the NDK library and store its path as a variable. You can use this variable to reference the NDK Library in other parts of the build script. The following example can be found Android proprietary log support library , and will store its path in log lib:

find_library( # Defines the name of the path variable that stores the
                  # location of the NDK library.
                  log-lib

                  # Specifies the name of the NDK library that
                  # CMake needs to locate.
                  log )

In order for your native library to call functions in the log library, you need to use CMake to build the target_link_libraries() Command to associate these libraries:

 find_library(...)

    # Links your native library against one or more other native libraries.
    target_link_libraries( # Specifies the target library.
                           native-lib

                           # Links the log library to the target library.
                           ${log-lib} )
    

For example:

 cmake_minimum_required(VERSION 3.4.1)

    add_library( # Specifies the name of the library.
                 native-lib

                 # Sets the library as a shared library.
                 SHARED

                 # Provides a relative path to your source file(s).
                 src/main/cpp/native-lib.cpp )
    

    # Specifies a path to native header files.
    include_directories(src/main/cpp/include/)
find_library( # Defines the name of the path variable that stores the
                  # location of the NDK library.
                  log-lib

                  # Specifies the name of the NDK library that
                  # CMake needs to locate.
                  log )
    # Links your native library against one or more other native libraries.
    target_link_libraries( # Specifies the target library.
                           native-lib

                           # Links the log library to the target library.
                           ${log-lib} )
                  

Step 6: Associate Gradle with your native library

Auto configuration:

  1. 1. Open * * Project * * pane from the left side of IDE, and then select * * Android * * view.
    
    2. Right click the module you want to associate with the native library (for example, * app * * module), and select * * Link C++ Project with Gradle * * from the menu. You will see a dialog similar to figure 4.
    
    3. From the drop-down menu, select CMake or NDK build.
    
       a. If you choose * * CMake * *, please use the field next to * * Project Path * * to specify for your external CMake project` CMakeLists.txt `Script file.
    
       b. If you choose * * NDK build * *, use the field next to * * Project Path * * to specify for your external NDK build project[` Android.mk ] https://developer.android.com/ndk/guides/android_ MK) script file. If[` Application.mk ] https://developer.android.com/ndk/guides/application_ MK) file and your` Android.mk `The file is located in the same directory and will be included in Android Studio
    
       4. Click ok
    

    Manual configuration

      To configure manually Gradle To relate to your native library, you need to externalNativeBuild Block added to module level build.gradle File and use the cmake or ndkBuild Block to configure it:
      android {
          ...
          defaultConfig {...}
          buildTypes {...}
    
          // Encapsulates your external native build configurations.
          externalNativeBuild {
    
            // Encapsulates your CMake build configurations.
            cmake {
    
              // Provides a relative path to your CMake build script.
              path "CMakeLists.txt"
            }
          }
        }
        
    

Tags: cmake Android Gradle Java

Posted on Mon, 15 Jun 2020 00:41:14 -0400 by TropicalBeach