程序员

Android Studio 加载第三方C/C++库

2017-09-02  本文已影响0人  桂慧要努力当个攻城师

1 修改app目录下的CMakeLists.tx

  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).
             src/main/cpp/native-lib.cpp ) # 依赖的源码文件
include_directories(src/main/cpp)
  add_library(
            hello-lib #需要导入第三方库的别名,自定义。
            SHARED #动态库
            IMPORTED) #因为库已经存在所以使用导入
  set_target_properties( # Specifies the target library.
                       hello-lib
                       # Specifies the parameter you want to define.
                       PROPERTIES IMPORTED_LOCATION
                       # Provides the path to the library you want to import.
                       # /${ANDROID_ABI}表示处理器架构 第三方库必须存放在这类路径下
                      #如果有第三方库不支持或目标不需要支持的处理器架构,需要在app目录下的                        
                      #build.gradle文件中进行配置        
                      #路径一定要设置正确,不然会报依赖错误
                    ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libpsamcard.so )

 target_link_libraries( # Specifies the target library.
                       native-lib
                       # Links the target library to the log library
                       # included in the NDK.
                      hello-lib
                       ${log-lib} )

2修改app目录下的build.gradle

ndk {
    abiFilters  "armeabi"  // 指定要ndk需要兼容的架构(这样其他依赖包里mips,x86,"armeabi-v7a",arm-v8之类的so会被过滤掉)
}

如此我们编译链接生成的so库就只包含armeabi架构的,而其他架构的则不包含。

3编译生成目标库

生成的so库存放路径.png

我们可以把obj目录下的文件夹和文件一起拷贝出来给其他工程使用。

4 C/C++混合编程需要注意的事项

上一篇 下一篇

猜你喜欢

热点阅读