Android Studio3.1.2采用CMake编译live555为Android平台的动态链接库

前言:笔者电脑环境:Window7 x64旗舰版,Android Studio版本为3.1.2,构建Android的targetSdkVersion27。

Android Studio从2.2版本开始就引入了CMake编译ndk程序,对于live555在安卓平台的编译,目前网上的大部分教程都是在mk文件的基础上采用ndk-build进行编译,容易出错。而对live555在android studio采用cmake编译较少,由于笔者对cmake和as也是小白,也花费了好几天的时间,所以记录一下编译的过程,希望能给大家提供个参考。废话一大堆,下面正式开始。

目的:live555库主要包含四个模块:BasicUsageEnvironment、UsageEnvironment、groupsock、liveMedia,将这四个模块在Android Studio上分别采用CMake编译为四个.so文件输出。

一、新建android ndk项目

下面几步默认,直至

Android Studio3.1.2采用CMake编译live555为Android平台的动态链接库_第1张图片

点击Finish,完成ndk项目创建。

二、下载live555压缩包(http://www.live555.com/liveMedia/public/),笔者采用的是2018-4-25更新的版本live555-latest.tar.gz。将解压出来的BasicUsageEnvironment、UsageEnvironment、groupsock、liveMedia拷贝至cpp目录下,如图:

Android Studio3.1.2采用CMake编译live555为Android平台的动态链接库_第2张图片

三、由于live555四个模块均要编译为相应的动态库文件,因此,我们要为每个模块编写对应的CMakeLists.txt文件。每个CMakeLists.txt文件要描述当前模块与其它模块的依赖关系。在编写之前,让我们先看看四个模块之间的关系,我用下面两张图描述:

Android Studio3.1.2采用CMake编译live555为Android平台的动态链接库_第3张图片

liveMedia模块与其它三个模块的关系为:

Android Studio3.1.2采用CMake编译live555为Android平台的动态链接库_第4张图片

图的说明:如:liveMedia---->BasicUsageEnvironment,表示liveMedia由于包含BasicHashTable.hh依赖于BasicUsageEnvironment模块。箭头上的.cpp或.c文件表示,当编译liveMedia模块为库时,需要添加BasicHashTable.cpp这个依赖文件。(CMake添加依赖库的方式有两种:一种是源码编译依赖库;另一种是直接使用预先编译好的依赖库,具体请看:http://aigo.iteye.com/blog/2229401,我采用的是第一种)。

弄清楚live555四个模块之间的依赖关系后,回到我们的初衷要是要实现live555四个模块分别编译为四个.so文件,为此我们分别在每个模块的文件夹下编写CMakeLists.txt文件,比如,对于BasicUsageEnvironment这个模块,我们在BasicUsageEnvironment文件夹下,依照上图BasicUsageEnvironment与其它模块的依赖关系,编写如下的CMakeLists.txt文件:

include_directories(include)
include_directories(../groupsock/include)
include_directories(../UsageEnvironment/include)

file(GLOB BasicUsageEnvironmentFiles
   *.cpp)
list(APPEND BUE_Dependence_SRC_LIST
            ../UsageEnvironment/HashTable.cpp
            ../UsageEnvironment/strDup.cpp
            ../UsageEnvironment/UsageEnvironment.cpp
        )
add_library(BasicUsageEnvironment SHARED
         ${BasicUsageEnvironmentFiles} ${BUE_Dependence_SRC_LIST})

include_directories()包含了BasicUsageEnvironment依赖的头文件;

file()将BasicUsageEnvironment目录下的.cpp文件信息赋值给了BasicUsageEnvironmentFiles;

list()将BasicUsageEnvironment用到的其它模块的函数对应的.cpp文件追加给了BUE_Dependence_SRC_LIST

add_library()将BasicUsageEnvironment模块编译为共享库(.so文件),即动态链接库。

与BasicUsageEnvironment模块类似,其余模块CMakeLists.txt文件如下:

groupsock:

include_directories(include)
include_directories(../UsageEnvironment/include)
include_directories(../BasicUsageEnvironment/include)

file(GLOB groupsockfiles
   *.cpp *.c)
list(APPEND GS_Dependence_SRC_LIST
            ../BasicUsageEnvironment/BasicHashTable.cpp
            ../UsageEnvironment/strDup.cpp
            ../UsageEnvironment/HashTable.cpp
        )
add_library(groupsock SHARED ${groupsockfiles} ${GS_Dependence_SRC_LIST})

liveMedia:

include_directories(include)
include_directories(../groupsock/include)
include_directories(../UsageEnvironment/include)
include_directories(../BasicUsageEnvironment/include)
ADD_DEFINITIONS(-D LOCALE_NOT_USED)
file(GLOB liveMediaFiles
      *.cpp *.c)
list(APPEND LM_Dependence_SRC_LIST
            ../UsageEnvironment/HashTable.cpp
            ../UsageEnvironment/strDup.cpp
            ../groupsock/GroupsockHelper.cpp
            ../groupsock/Groupsock.cpp
            ../groupsock/inet.c
            ../groupsock/NetAddress.cpp
            ../groupsock/NetInterface.cpp
            ../groupsock/GroupEId.cpp
            ../BasicUsageEnvironment/BasicHashTable.cpp
        )
add_library(liveMedia SHARED ${liveMediaFiles} ${LM_Dependence_SRC_LIST})

UsageEnvironment:

include_directories(include)
include_directories(../BasicUsageEnvironment/include)
include_directories(../groupsock/include)

file(GLOB UsageEnvironmentFiles
    *.cpp)
list(APPEND UE_Dependence_SRC_LIST
            ../BasicUsageEnvironment/BasicHashTable.cpp
        )
add_library(UsageEnvironment SHARED ${UsageEnvironmentFiles} ${UE_Dependence_SRC_LIST})

四、在上一步,我们已经为live555的四个模块编译了对应的CMakeLists.txt文件负责每个模块的编译工作。还需要一个外层的CMakeLists.txt调用每个模块的CMakeLists.txt。

插曲:我们会发现在src/main/cpp目录下,IDE为我们自动创建了一个native-lib.cpp文件,里面定义了一个JNI接口函数,是连接ndk程序和Android程序的重要部分,为保证它能正确编译,还需要为它编写相应的CMakeLists.txt文件,因此,在src/main/cpp文件夹下,新建CMakeLists.txt,它的内容可以直接从IDE为我们创建的最外层CMakeLists.txt(app/CMakeLists.txt)里直接拷贝:

# 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 a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )

# 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 )
重新编写最外层的CMakeLists.txt(app/CMakeLists.txt)文件,内容如下:
cmake_minimum_required(VERSION 3.4.1)

#添加子目录,将会调用子目录中的CMakeLists.txt
ADD_SUBDIRECTORY(src/main/cpp/BasicUsageEnvironment)
ADD_SUBDIRECTORY(src/main/cpp/groupsock)
ADD_SUBDIRECTORY(src/main/cpp/liveMedia)
ADD_SUBDIRECTORY(src/main/cpp/UsageEnvironment)
ADD_SUBDIRECTORY(src/main/cpp)

五、至此,与live555编译所需相关的配置基本就完成了,接下来就配置与android项目构建相关的东西了。编辑app目录下的build.gradle如下:

Android Studio3.1.2采用CMake编译live555为Android平台的动态链接库_第5张图片

六、编译项目,最终在app/build/intermediates/cmake/debug/obj生成文件如下:

Android Studio3.1.2采用CMake编译live555为Android平台的动态链接库_第6张图片

原创博文,转载引用请注明出处https://blog.csdn.net/jcl1774356658/article/details/80172898。谢谢

你可能感兴趣的:(Android开发,live555)