1. 程式人生 > >如何在AndroidStudio中使用cmake編譯NDK

如何在AndroidStudio中使用cmake編譯NDK

一、概述  

   之前由於AS沒有強制,所以也就守舊一直沿用以前的NDK開發方法,gradle.properties新增android.useDeprecatedNdk=true的屬性。可是AS3.0以後被強制必須使用Cmake編譯了,不得已才來學學,終於發現了這東東的好處。CmakeAS2.2中提出的更加方便的JNI開發的構建工具,相當於之前使用是NDK-BUILDE

Cmake的優勢:作為一個java語言開發者,每次要接觸C/C++的時候內心都是拒絕的,今天用了Cmake。終於發現寫C++語言也可以輕而易舉。居然帶自動補全功能,而且所有步驟包括標頭檔案生成,javac編譯等等全部不需要你來弄,只需要編寫完程式碼放入對應位置,然後編寫配置

CMakeLists.txt即可。

二、如何使用CMAKE

       1.首先安裝Cmake外掛,File->setting->SDK->找到SDK Tools 如下。 選中圖中的CmakeLLDB外掛(這個工具就是用來在AS中除錯native程式碼的)

如果沒有下外掛的話會報如下錯:Gradle sync failed: Unable to get the CMake version located at: C:\Users\suzhiming\AppData\Local\Android\Sdk\cmake\bin  Consult IDE log for more details (Help | Show Log) (936ms)

2.moduleapp)的build.gradle中新增 
externalNativeBuild {
    cmake {
        cppFlags ""
    }
}

  如下圖位置:在defauldConfig

3.接下來就是建立CMakeLists.txt檔案。在app目錄下,內容如下,注意標紅的4個地方的修改

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
             SerialPort 設定Lib庫的名字,也就是你的C/C++類名

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/SerialPort.c ) 你的c/c++檔案路徑

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

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

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
                       SerialPort 庫名,就是java中呼叫system.Loadlibrary()的名字。 

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
 
4.CMakeLists檔案加到build.gradle參與編譯
externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}
  位置如下圖


5.build專案,生成.externalNativeBuild檔案。

   build成功後,會在app根目錄下生成這個目錄。說明OK,配置成功了。這時候我們進入c/c++檔案編寫就可以發現編寫過程中,就跟寫java程式碼一樣會自動關聯,自動提示了

附:

JNI的方法名全部以規定都是Java開頭,然後包名的點變為_,最後加類名加方法名,例如我這裡的是Java_protocol_sdk_shbst_com_protocolsdk_SerialPort_open,參照為1.Java 2.包名protocol.sdk.shbst.com.protocolsdk 3.類名SerialPort  4.方法名open  )。