如何在AndroidStudio中使用cmake编译NDK - Go语言中文社区

如何在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:UserssuzhimingAppDataLocalAndroidSdkcmakebin  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  )。


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u012761076/article/details/79064878
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-27 22:09:09
  • 阅读 ( 855 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢