Android CMake调用第三方库

一.生成so文件

    1)创建StringUtil.h头文件,内容如下:

#pragma once

#define DLL_EXPORTED __attribute__((__visibility__("default")))

#ifdef __cplusplus
extern "C" {
#endif

	DLL_EXPORTED char* toLower(char* szSource);		
	DLL_EXPORTED char* toUpper(char* szSource);

#ifdef __cplusplus
}
#endif

2)创建StringUtil.cpp文件,内容如下:

#include "StringUtil.h"
#include <string>
#include <algorithm>

/*
	To test the library, include "StringUtil.h" from an application project
	and call StringUtilTest().
	
	Do not forget to add the library to Project Dependencies in Visual Studio.
*/

char* toLower(char* szSource)
{
	std::string sSource = szSource;
	std::string sDestiny = sSource;
	transform(sSource.begin(), sSource.end(), sDestiny.begin(), tolower);
	return (char *)sDestiny.c_str();
}

char* toUpper(char* szSource)
{
	std::string sSource = szSource;
	std::string sDestiny = sSource;
	transform(sSource.begin(), sSource.end(), sDestiny.begin(), toupper);
	return (char *)sDestiny.c_str();
}

3)创建Android.mk和Application.mk;

   Android.mk如下:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := StringUtil_lib
LOCAL_SRC_FILES := StringUtil.cpp
LOCAL_LDLIBS += -lm -llog
include $(BUILD_SHARED_LIBRARY)

Application.mk如下:

APP_MODULES := StringUtil_lib
APP_OPTIM := release
APP_STL := gnustl_static
APP_ABI := all

所有文件放到jni下,使用ndk-build生成libStringUtil_lib.so文件;

注意:DLL_EXPORTED宏定义必须要,否则so文件中没有导出符号;可以使用nm -D libStringUtil_lib.so命令查看导出符;

二.CMake调用so文件

Android studio创建工程,支持C++,

1.修改app下gradle文件,在android{}中加入如下代码,设置jniLibs路径:

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

2.defaultConfig中设置如下:

externalNativeBuild {
    cmake {
        cppFlags "-frtti -fexceptions"
        arguments "-DANDROID_STL=gnustl_static"
    }
}

3.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).
             src/main/cpp/native-lib.cpp )

#添加第三方库
add_library(StringUtil_lib SHARED IMPORTED)
set_target_properties(StringUtil_lib PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/StringUtil/libs/${ANDROID_ABI}/libStringUtil_lib.so)

#链接第三方库的头文件
include_directories(StringUtil_lib PUBLIC ${CMAKE_SOURCE_DIR}/StringUtil/include)

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}
                       StringUtil_lib)

1)add_library(StringUtil_lib SHARED IMPORTED)添加第三方库StringUtil_lib(名字根据so来设置[lib***.so])

2)链接so文件,$(CMAKE_SOURCE_DIR)为CMakeList.txt所在目录

set_target_properties(StringUtil_lib PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/StringUtil/libs/${ANDROID_ABI}/libStringUtil_lib.so)

3)添加StringUtil头文件

include_directories(StringUtil_lib PUBLIC ${CMAKE_SOURCE_DIR}/StringUtil/include)

4)target_link_libraries中添加StringUtil_lib

5)调用Make Project导入,在native-lib.cpp中添加StringUtil.h的头文件;

#include <jni.h>
#include <string>
#include "StringUtil.h"

extern "C" JNIEXPORT jstring

JNICALL
Java_diveo_testc3_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    char* szDestiny = toLower((char *)hello.c_str());
    return env->NewStringUTF(szDestiny);
}

5)运行


注意:除了设置Target_link_libraries外,jniLibs一定要设置,否则在运行时报错,找不到so。

测试代码链接:

https://download.csdn.net/download/gaojun1146/10448103

CMake中安装第三方库有多种方法。一种常见的方法是使用find_package命令。这个命令可以用来查找和引入系统已经安装的库。您可以在CMakeLists.txt文件中使用find_package命令,并指定您要引入的库的名称。CMake会根据您的系统配置自动查找这个库,并将其引入到您的项目中。这种方法的优点是简单方便,不需要手动下载和设置库的构建过程。 另一种方法是使用include(FetchContent)命令。这个命令允许您从指定的URL或Git存储库中提取第三方库,并将其作为CMake项目的子目录构建。使用FetchContent命令时,您无需手动下载、解压缩或设置外部库的构建过程,CMake会自动执行这些任务。这种方法的优点是它使得引入第三方库更加方便,并且使得与外部库的集成更加简单。同时,使用FetchContent命令时,您可以很容易地在您的CMake项目中指定外部库的版本和源代码位置。 另外,对于Android开发,您可以使用NDK-Build编译出各平台的so文件,然后将这些文件引入到您的CMake项目中。这种方法适用于调用第三方库的场景。您可以在CMakeLists.txt文件中使用add_library命令,将这些so文件添加到您的项目中。 综上所述,安装第三方库的方法包括使用find_package命令、include(FetchContent)命令和使用NDK-Build编译出so文件。您可以根据您的具体需求和项目情况选择适合的方法来安装第三方库。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值