Differences between NDK-developed CMake and traditional JNI in directory structure and configuration files

Reprinted from https://blog.csdn.net/u012527560/article/details/51752070


Reprinting starts: =============================

Since AndroidStudio was updated to 2.2, there are two methods of developing NDK, CMake and traditional JNI, mainly the difference between the directory structure and build.gradle. Below we will introduce the difference between the directory and the configuration of build.gradle (hint : When using CMake for the first time, it is best to check Include C++ Support when creating a new project, so that the NDK development of this project is the CMake method. , this will be of great help to build ordinary projects in the future and then transfer to CMake to develop NDK. Later, we will talk about the transfer of ordinary projects to NDK development)

Differences in directory:

   Traditional JNI:

        

  CMake:

       

The difference between the two methods in the directory is two points:

    1. Change the previous jni directory to cpp, and the location remains the same  

    2. Before the compilation and configuration of the c file, the Android.mk file is placed in the jni directory, and now it is changed to the CMakeLists.txt file

(Hint: In fact, Android .mk files and CMakeLists.txt can be placed in any directory, but it is best to place them anywhere in the current project directory. The difference between build.gradle configuration will be known later )


Differences in build.gradle:

  Traditional JNI:
      

    

  CMake:

        

     

    As can be seen from the above two pictures, only the contents of the two red boxes are different. For traditional JNI, the contents of the second red box are actually not required, and all its configurations are in

ndk{
    moduleName "hello"
abiFilters "x86"
}    

Inside the configuration, the content in the Android.mk file will not work (hint: but it cannot be deleted, you will know when the ordinary Android project is transferred to NDK development below ), which is different from the Eclipse development environment. Another point, traditional JNI development, needs to be configured in the gradle.properties file in the project root directory

android.useDeprecatedNdk=true

Otherwise, an error will be reported when building the project.

 
 

Most of the CMake configuration is configured in the CMakeLists.txt file, and only a small part is placed in

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

configuration inside. The syntax rules of CMake can be Baidu.

Ordinary Android project to NDK development project:

For ordinary Android projects, you can right-click and select Link C++ Project with Gradle to convert to an NDK project.


By selecting CMake or ndk-build, the development NDK uses the CMake or JNI method, but this conversion method must conform to the CMake or JNI directory

Structure, that is to say, before the above operation is completed, a directory that conforms to CMake or JNI rules must be built in the project, if CMakeLists.txt

文件中有对xx.cpp/xx.c的引用,那么必须在cpp目录下新建好对应的xx.cpp/xx.c,同理,android.mk文件要是引

用cpp/c文件,那么就要在jni目录新建好对应得文件.(androidstudio2.2之后新建NDK项目只能得到CMake方式,

所以要想JNI开发,只能普通项目转)。

转载结束:=============================

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324584868&siteId=291194637