Android 已有项目增加JNI

简单三步,解决一切烦恼。

1. 工程右键-Folder-JNI Folder

2. 在build.gradle文件的android节点下添加cmake

    // 在android节点下
    // 指定CMakeLists.txt路径
    externalNativeBuild {
        cmake {
            // 在该文件种设置所要编写的c源码位置,以及编译后so文件的名字
            path "src/main/jni/CMakeLists.txt"
            // cmake版本 可以省略
            //version "3.10.2"
        }
    }

3. 在jni目录里添加 CMakeLists.txt 文件,内容如下(根据情况修改,我这个是编译live555使用的):

# 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构建本地库时所需的最小版本
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.

# 变量 CMAKE_CURRENT_SOURCE_DIR 指的是当前处理的 CMakeLists.txt 所在的路径。

add_definitions(-DSOCKLEN_T=socklen_t)
add_definitions(-DNO_SSTREAM=1)
add_definitions(-DNEWLOCALE_NOT_USED)
#add_definitions(-D_LARGEFILE_SOURCE=1)
#add_definitions(-D_FILE_OFFSET_BITS=64)
add_definitions(-DBSD=1)


# 遍历所有 .c 和 .cpp
file(GLOB_RECURSE SRCS_NATIVE_C "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
file(GLOB_RECURSE SRCS_NATIVE_C++ "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")


# 打印信息
message(STATUS "================================")
message(STATUS "CMAKE_CURRENT_SOURCE_DIR:[${CMAKE_CURRENT_SOURCE_DIR}]")
message(STATUS "SRC:[${SRCS_NATIVE_C}]")
message(STATUS "SRC:[${SRCS_NATIVE_C++}]")
message(STATUS "================================")


# 设置so文件的目录
set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs)

# 引入头文件
include_directories(

        // live555 头文件
        live555/BasicUsageEnvironment/include
        live555/groupsock/include
        live555/liveMedia/include
        live555/UsageEnvironment/include
)


# add_library 导入库
# 添加一个库文件
# 参数一:库名称,主要用于System.loadLibrary()中填写的名字
# 参数二:库类型,SHARED 表示动态链接库
# 参数三:已导入

# set_target_properties 指库添加动态链接库的库文件
# 参数一:对应添加库的库名
# 参数二:PROPERTIES表示导入
# 参数三:IMPORTED_LOCATION 表示导入本地库
# 参数四:库文件地址

# 添加
add_library( # Sets the name of the library.

        #将资源文件生成动态链接库(so文件)的库名称(文件名称:“lib" +设置的名称)
        my-native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        ${SRCS_NATIVE_C}
        ${SRCS_NATIVE_C++}
        )

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

# 搜索系统库log的路径,并保存在变量 log-lib 中
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.

# link_libraries 用来链接静态库,target_link_libraries用来链接导入库
target_link_libraries( # Specifies the target library.

        #将所有的add_library中的库链接起来,有多少个add_library成的库就将其添加到这里
        #这个和add_library中的指定的so库名称一致
        my-native-lib

        # Links the target library to the log library
        # included in the NDK.
        # 这个是上面 find_library 中找到的库
        ${log-lib}
        )

鸣谢:

Android studio 4.x在已有项目中集成jni
https://www.jianshu.com/p/a352e81db455

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值