Android Studio中进行NDK开发(android studio 2.2 及 CMake)

Studio在使用C++等native代码方面做出了新的尝试,感觉更方便了。现在把使用方式记录如下:

一、下载NDK及新的构建工具CMake

Studio引入了新的Native代码构建工具,可以帮助打包成.SO文件 并提供给gradle打包的apk文件中。


如下图,选中CMake   LLDB 及NDK,进行下载即可。




二、创建新的支持C/C++代码的工程

这个其实很简单,按照正常的创建项目的步骤,只需要在第一步的时候注意选择 添加对C/C++的支持就可以了。

细节1:


细节2:创建好的工程目录格式如下图



其中,cpp目录就是存放native代码的地方啦,而CMakeLists.txt是CMake工具的配置文档,具体的在后面会讲到。

此时,你可以run一下。会看到手机屏幕显示Hello from C++字样。



三、在已经存在的工程中添加对C/C++的支持

这个主要分三步:(已C++为例)


1、创建native代码的文件:


在项目的src目录下创建新的目录,命名不做要求,例如:cpp

在新建的目录右键  New > C/C++ Source File.(如果要添加的不是C++代码,可以通过点击按钮. 来增加文件类型的支持


2、创建CMake的配置脚步  CMakeLists.txt

(是的,正如你看到的。这个文档是  txt  格式的)

1、在你的module的根目录右键New > File.名字不能错,必须是 CMakeLists

2、打开它,并输入需要的cammand。


最基本的cammand如下:


(1)必须的两个命令:

(规定Cmake工具的最低版本)


cmake_minimum_required(VERSION 3.4.1)

(添加各native文件为一个library,注意  可以通过写多个这种命令来添加多个library。如果要在一个library中添加多个native文件,文件名之间用逗号隔开即可)


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  src/main/cpp/test.cpp)

(2)

如果使用了header文件,还要增加下面的命令,参数是各header文件的路径


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

3、java代码中使用native函数

在CMake的配置文件中,如果你添加的library命名为 native-lib ,那么CMake帮助生成的.so文件为:libnative-lib.so  (即,前面增加了lib)

但在java中使用时以你命名的为准:


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


注意: 如果,你修改了CMake配置文件中的某个library的名字,或者移除了某个library,必须先clean工程(Build > Clean Project),这些修改才能生效。


4、在CMake中引入NDK 的api并与自建的library链接以使用其中的api

NDK的各个library是已经构建好的,可以直接使用的。而且,CMake知道到哪里去找到它们。我们只需要告诉CMake我们想使用哪个library,并将其链接到自己的要使用这个NDK api的library。


这里以log的api为例:

(这两个命令分别是 find_library 和   target_link_libraries)

指定要使用的NDK library:


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 )


链接到之前已经添加进来的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} )

5、添加其它已经build好的library

add_library( imported-lib
             SHARED
             IMPORTED )


以为已经build好了,所以这里要告诉CMake,我只是想import进来一个library。

 指定要import进来的library的location:


set_target_properties( # Specifies the target library.
                       imported-lib

                       # Specifies the parameter you want to define.
                       PROPERTIES IMPORTED_LOCATION

                       # Provides the path to the library you want to import.
                       imported-lib/src/${ANDROID_ABI}/libimported-lib.so )


将import进来的library与本地的library进行链接:


target_link_libraries( native-lib imported-lib app-glue ${log-lib} )

6、最后,将Gradle与native library连接

为了让gradle知道CMake的位置,需要增加externalNativeBuild配置项


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"
    }
  }
}



官方指导链接:https://developer.android.com/studio/projects/add-native-code.html#create-cmake-script



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值