Android中CMake的使用之五调用第三方库的完善

Android中CMake的使用之五调用第三方库的完善

最近公司和外边的公司有一个合作项目,需要处理一下第三库的调用,发现在之前二、三的总结中,还是遗漏了一些细节,这里进行一下补充。

先把一个完整的CMakeLists.txt的文档列出来,以前的不清楚:

# For moreinformation 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.
             native-lib

             # Sets the library as ashared library.
             SHARED

             # Provides a relative pathto your source file(s).
             src/main/cpp/native-lib.cpp )

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

             # Sets the library as ashared library.
             SHARED

             # Provides a relative pathto your source file(s).
             IMPORTED )

set_target_properties(
            test-lib
            PROPERTIES IMPORTED_LOCATION
           ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libtest-lib.so)

add_library( # Sets the name of the library.
             usb1.0

             # Sets the library as ashared library.
             SHARED

             # Provides a relative pathto your source file(s).
             IMPORTED )

set_target_properties(
            usb1.0
            PROPERTIES IMPORTED_LOCATION
            ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libusb1.0.so)
# 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 theNDK 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.
                       native-libtest-lib usb1.0

                       # Links the targetlibrary to the log library
                       # included in theNDK.
                       ${log-lib} )

find_library的作用是把log库的位置赋值给变量log-lib,后面可以直接$(log-lib)来使用。写一个完整的配置文件是为了防止一些细节上的指向不明确,特别针对初学者,可能会有一些疑问,如果是一个全的,就不会有这样或那样的想法了。

这里不光有一个自已写的SO还有一个标准的开源的libusb的SO,这样大家全更清晰的明白第三方库的使用方式。

另外还需要注意的一个是app的Gradle配置中Android节:

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

如果没有SourceSet,会产生无法将本工程产生的SO编译到APK中的现象,这个在公司中有人遇到了,重新配置一下就OK了。小细节。

目前手上没有调试设备,回调及数组传参需要处理的,还是需要在验证后再更新到博客上,免得误导广大同学。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值