Android NDK系列三(Android Studio cmke 编译多个个.so文件)

如果我们要在我们的项目中加载多个我们生成的.so库

System.loadLibrary("play_sound");

System.loadLibrary("native-lib");

 

这时候需要我们生成多个.so库;

 

我们对上一篇做一个升级,比如我在生成 play_sound 库的时候,也想生成 我自己的native-lib库;

 

这时候我们需要多个CmakeList.txt管理:

先看一下我的项目目录:

 

在cpp下面分别创建了 native_lib 和 play_sound 文件夹,用来区分两个生成的so, 每个子CmakeList.txt对应一个.so

 

看一下native_lib目录下的CmakeList.txt,基本和默认生成的一样:

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

 

# 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

#./src/main/cpp/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 )

 

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

native-lib

# Links the target library to the log library

# included in the NDK.

${log-lib} )

 

 

再来看play_sound目录下的CmakeList.txt:

 

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

 

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

 

#*********************************************

include_directories(

../fmod_inc

)

 

set(distribution_DIR ${CMAKE_SOURCE_DIR}/libs)

#set(distribution_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs)

 

#添加预编译库fmod

add_library(

fmod

SHARED

IMPORTED

)

set_target_properties(

fmod

PROPERTIES IMPORTED_LOCATION

${distribution_DIR}/${ANDROID_ABI}/libfmod.so

)

 

 

#添加预编译库fmodL

add_library(

fmodL

SHARED

IMPORTED

)

set_target_properties(

fmodL

PROPERTIES IMPORTED_LOCATION

${distribution_DIR}/${ANDROID_ABI}/libfmodL.so

)

 

#添加目标库

add_library(

play_sound

SHARED

# src/main/cpp/play_sound.cpp

# src/main/cpp/common_platform.cpp

# src/main/cpp/common.cpp

play_sound.cpp

common.cpp

common_platform.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 )

 

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

# 生成的目标库

play_sound

# Links the target library to the log library

# included in the NDK.

#依赖于库

fmod

fmodL

${log-lib} )

 

 

最后来看app目录下的CmakeList.txt, 这里将上面两个 文件引入进来,

 

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

 

#设置生成的so动态库最后输出的路径

#设置后,在编译后,还是先在build/intermediates/cmake下生成.so,然后会拷贝一份放到下面的目录中

#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/./libs/${ANDROID_ABI})

 

#设置头文件搜索路径(和此txt同个路径的头文件无需设置),可选

#INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/common)

 

#指定用到的系统库或者NDK库或者第三方库的搜索路径,可选。

#LINK_DIRECTORIES(/usr/local/lib)

 

ADD_SUBDIRECTORY(./src/main/cpp/native_lib)

ADD_SUBDIRECTORY(./src/main/cpp/play_sound)

 

当然,在app模块 build.gradle中指定的 CmakeList.txt 文件是app下面的CmakeList.txt;

 

编译项目,这时生成两个.so:

ok啦!

大家也可以参考

参考:https://blog.csdn.net/b2259909/article/details/58591898

 

 

你可能感兴趣的:(android,ndk)