Android Studio之NDK开发(二)

上一篇NDK开发系列(一),继续介绍有关NDK开发技能

一,C++和CMake.txt里面都写了什么?

native-lib.cpp中代码介绍:

这里写图片描述

app/CMakeLists.txt构建脚本翻译:

内容太多,不想看的童鞋点这里:Android CMake.txt

# 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编译器的最低版本3.4.1
cmake_minimum_required(VERSION 3.4.1)

#设置生成的so动态库最后输出的路径
#它将会把生成的so库按照你在 build.gradle 指定的 abi分别放置在 jniLibs下
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/./src/main/jniLibs/${ANDROID_ABI})

# 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.
#翻译:创建一个类库的name,设置这个类库为STATIC
#或者SHARED类型,并且设置c或者c++的源代码的的相对路径。
#你可以定义多个类库,同事CMake会为你构建。
#Gradle可以自动将shared类库打包到你的APK中。

# CMake根据指定的源文件生成库文件
add_library( # Sets the name of the library. #设置类库的名字
             native-lib

             # Sets the library as a shared library.
              #生成的库的类型[SHARED|STATIC|MODULE]
              #SHARED库会被动态链接,在运行时被加载
              #STATIC库是在链接其它目标的时候使用
             SHARED

             # Provides a relative path to your source file(s).
               #指定路径下的源文件代码,可以为这个类库指定多个.cpp文件
             src/main/cpp/native-lib.cpp )

# 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.
#翻译:搜索指定构建库并将变量作为存储路径。
#因为Cmake构建工具默认包含了系统类库,你仅仅需要指定你想要添加的公共NDK类库的name.
#CMake构建工具会在完成构建之前校验指定的类库name是否存在
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.
#翻译:指定类库 CMake构建工具会连接到你的目标类库中。
#你可以连接到多个类库中,例如:在这个CmakeLists.txt的构建脚本中定义的类库,
#预构建的第三方类库或者系统类库。

#为生成的目标类库指定需要的库文件
target_link_libraries( # Specifies the target library.
                       #生成的目标库文件
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       #需要在目标库文件中使用的库,表示可以在native-lib中使用log-lib库的内容
                       ${log-lib} )
如何查找.so
  • 方法一:app\build\intermediates\jniLibs\debug

这里写图片描述

  • 方法二:通过CMake.txt指定路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/./src/main/jniLibs/${ANDROID_ABI})

这里写图片描述

部分注意事项可以参考:https://www.cnblogs.com/android-blogs/p/5867337.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值