通过JNI调用三方SO库

1. 不管怎么样搞一个带C++的项目

2. app路径下搞一个文件jniLibs放第三方so

这个路径需要配置:

地方在app下的build.gradle

android {
    //...
    sourceSets {
        main {
            jniLibs.srcDirs = ['jniLibs']
        }
    }
}

把so放到对应的abi文件夹中,比如armeabi-v7a

如何确定adi?

这是个问题

3. 修改CMakeLists.txt

上面的问题确定abi在这里就可以定下来,通过message打log,如果log看不到,改个报错出来,可能就看到了。

# 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_minimum_required(VERSION 3.4.1)

set(var hello)
message(${var})
message(${CMAKE_SOURCE_DIR})
message(${ANDROID_ABI})
#E:/AndroidWorkSpace/NDKDemo/app/src/main/cpp

include_directories(src/main/cpp/)
file(GLOB CPP_FILES "src/main/cpp/*.cpp")

set(distribution_DIR  ${CMAKE_SOURCE_DIR}/../../../../jniLibs/)
message(${distribution_DIR})
# 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.
             test-lib
             # Sets the library as a shared library.
             SHARED
             # Provides a relative path to your source file(s).
             test-lib.cpp)

add_library(
        people-lib
        SHARED
        people/People.cpp
)

#导入第三方so包,并声明为 IMPORTED 属性,指明只是想把 so 导入到项目中
add_library(
        jsio-lib
        SHARED
        IMPORTED
)
#指明 so 库的路径,CMAKE_SOURCE_DIR 表示 CMakeLists.txt 的路径
set_target_properties(
             jsio-lib
             PROPERTIES IMPORTED_LOCATION
             ${CMAKE_SOURCE_DIR}/../../../jniLibs/${ANDROID_ABI}/libndktest.so )
#指明头文件路径,不然会提示找不到 so 的方法
include_directories(${CMAKE_SOURCE_DIR}/include)
#添加第三方头文件
#target_include_directories(test-lib PRIVATE ${CMAKE_SOURCE_DIR}/include)


# 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 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.

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

4. 放入需要的.h

如果你没有.h那么恭喜你,这个so你玩不了

路径?

可以统一放在cpp/include,也可以直接放cpp下,看习惯和规范

5. 在自己的cpp中调用

include刚才那个.h文件,如果有配置过可以用<> include,如果没有,就老老实实用""。

然后就是直接调用方法了,以JNI的形式

如果不写成JNI的形式,那就呵呵了

 

最后上层JAVA调用JNI,开心!

你可能感兴趣的:(Android)