一.生成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