直接进入主题
-
安装插件
-
创建项目,直接加上include C++ support
-
创建一个调用SO库函数的类
- 使用命令行进入java这个文件夹,使用命令javah -jni com.example.apple.ndktest.NDKManager,生成.h头文件
-
在自己创建的cpp文件中导入上面的头文件并实现
-
配置build.grade
-
配置CMakeLists.txt(附链接第三方库方法-openssl)
# 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)
#设置JNI变量名的值为${PROJECT_SOURCE_DIR}/src/main/jni
set(JNI /${PROJECT_SOURCE_DIR}/src/main/jni)
# 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.
#设置Lib库的名字
nativeLib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# 源文件(实现的文件)
src/main/cpp/nativeLib.cpp )
#加载第三方库 SHARED->.so 或者 STATIC->.a
add_library(openssl STATIC IMPORTED )
#配置加载头文件
include_directories( ${JNI}/include)
#引入第三方.a库
set_target_properties(openssl PROPERTIES IMPORTED_LOCATION ${JNI}/libs/${ANDROID_ABI}/libcrypto.a)
# 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.
#设置Lib库的名字(System.Loadlibrary("libraryName")的libraryName)
nativeLib
openssl
# Links the target library to the log library
# included in the NDK.
${log-lib} )
-
最后点击Make Project生成SO库
-
选择编译Debug还是Release版本后,点击Make Project,生成对应版本SO库