Android Studio 2.2 更方便地创建JNI项目-CMake

前段时间写了篇Android Studio 第一个NDK例子,它是在使用Android Studio2.1版本的实现方案,最近发现2.2稳定版本已经出来了,所以更新了版本,发现使用该版本创建Jni项目更加方便了。

使用Android Studio 2.2创建JNI项目(基于CMake)

1. 创建一个新项目(Create New Project)

点击File — New — New Project,把Include C++ Support前面的CheckBook勾上。

《Android Studio 2.2 更方便地创建JNI项目-CMake》

接下来的步骤跟创建普通项目一样。

2、配置C++支持功能(Customize C++ Support)

Customize C++ Support界面默认即可。

《Android Studio 2.2 更方便地创建JNI项目-CMake》

  • C++ Standard
    指定编译库的环境,其中Toolchain Default使用的是默认的CMake环境;C++ 11也就是C++环境。两种环境都可以编库,至于区别,后续会跟进,当前博文使用的是CMake环境

  • Exceptions Support
    如果选中复选框,则表示当前项目支持C++异常处理,如果支持,在项目Module级别的build.gradle文件中会增加一个标识 -fexceptionscppFlags属性中,并且在so库构建时,gradle会把该属性值传递给CMake进行构建。

  • Runtime Type Information Support
    同理,选中复选框,项目支持RTTI,属性cppFlags增加标识-frtti

3、认识CMakeLists.txt构建脚本文件

CMakeLists.txt文件用于配置JNI项目属性,主要用于声明CMake使用版本、so库名称、C/CPP文件路径等信息,下面是该文件内容:

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

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 it for you.
# Gradle automatically packages shared libraries with your APK.

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

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
  • cmake_minimum_required(VERSION 3.4.1)
    CMake最小版本使用的是3.4.1。

  • add_library()
    配置so库信息(为当前当前脚本文件添加库)

    • native-lib
      这个是声明引用so库的名称,在项目中,如果需要使用这个so文件,引用的名称就是这个。值得注意的是,实际上生成的so文件名称是libnative-lib。当Run项目或者build项目是,在Module级别的build文件下的intermediates\transforms\mergeJniLibs\debug\folders\2000\1f\main下会生成相应的so库文件。
  • SHARED
    这个参数表示共享so库文件,也就是在Run项目或者build项目时会在目录intermediates\transforms\mergeJniLibs\debug\folders\2000\1f\main下生成so库文。此外,so库文件都会在打包到.apk里面,可以通过选择菜单栏的Build->Analyze Apk…*查看apk中是否存在so库文件,一般它会存放在lib目录下。

  • src/main/cpp/native-lib.cpp
    构建so库的源文件。

STATIC:静态库,是目标文件的归档文件,在链接其它目标的时候使用。
SHARED:动态库,会被动态链接,在运行时被加载。
MODULE:模块库,是不会被链接到其它目标中的插件,但是可能会在运行时使用dlopen-系列的函数动态链接。
更详细的解释请参考这篇文章:C++静态库与动态库

下面的配置实际上与自定义的JNI项目(自定义的so库)没有太大关系。

  • find_library()
    这个方法与我们要创建的so库无关而是使用NDK的Apis或者库,默认情况下Android平台集成了很多NDK库文件,所以这些文件是没有必要打包到apk里面去的。直接声明想要使用的库名称即可(猜测:貌似是在Sytem/libs目录下)。在这里不需要指定库的路径,因为这个路径已经是CMake路径搜索的一部分。如示例中使用的是log相关的so库。

  • log-lib
    这个指定的是在NDK库中每个类型的库会存放一个特定的位置,而log库存放在log-lib中

  • log
    指定使用log库

  • target_link_libraries()
    如果你本地的库(native-lib)想要调用log库的方法,那么就需要配置这个属性,意思是把NDK库关联到本地库。

  • native-lib
    要被关联的库名称

  • ${log-lib}
    要关联的库名称,要用大括号包裹,前面还要有$符号去引用。

实际上,我们可以自己创建CMakeLists.txt文件,而且路径不受限制,只要在build.gradle中配置externalNativeBuild.cmake.path来指定该文件路径即可。

4、gradle脚本引用CMakeLists.txt文件

Run或者Build项目时,想要执行CMakeLists.txt构建脚本,需要把脚本配置到模块级的build.gradle中。

android {
    externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
}

5、Run Module就能看到结果

《Android Studio 2.2 更方便地创建JNI项目-CMake》

6、拓展之构建NDK源代码

实际上NDK除了有预置的库还有一个源代码(c/cpp),如果本地库想要关联这些代码可以这样做:

add_library( app-glue
             STATIC
             ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )

# You need to link static libraries against your shared native library.
target_link_libraries( native-lib app-glue ${log-lib} )
  • app-glue
    仍然是自定义库的名称

  • ANDROID_NDK
    这个是Android Studio已经定义好的变量,可以直接使用它指定的是NDK源代码的根目录。

7、拓展之使用第三方so库

在一些情况下,我们没有能力开发so库,当别人抛一个库过来的时候我们直接使用就好了。

首先,我们告诉脚本我们只需要导入so库,不需要构建操作。

add_library( imported-lib
             SHARED
             IMPORTED )
  • IMPORTED
    表示只需要导入,不需要构建so库。

接着,我们要设置so库的路径了

set_target_properties(target1 target2 ...
                      PROPERTIES prop1 value1
                      prop2 value2 ...)

举例:

set_target_properties(
                      imported-lib // so库的名称
                      PROPERTIES IMPORTED_LOCATION // import so库
                      libs/libimported-lib.so // so库路径
)

当使用已经存在so库时,不应该配置target_link_libraries()方法,因为只有在build 库文件时才能进行link操作。

拓展阅读:https://developer.android.com/ndk/guides/build.html

    原文作者:科大向阳
    原文地址: https://www.jianshu.com/p/4eefb16d83e3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞