Skip to content

Commit

Permalink
export mmkv symbols & headers for Native usage in NDK #1449 #1450
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Jan 3, 2025
1 parent 61227cf commit 7cad5d3
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 19 deletions.
12 changes: 10 additions & 2 deletions Android/MMKV/gradle/android-publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ android.libraryVariants.all { variant ->
failOnError false
}
def javadocJar = task("${variant.name}JavadocJar", type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}
def jniSymbolsJar = task("${variant.name}SymbolJar", type: Jar, dependsOn: 'build') {
classifier = "so-symbols"
archiveClassifier = "so-symbols"
boolean hasNativeBuildTask = false
tasks.each { task ->
if (task.getName().startsWith("externalNativeBuild")) {
Expand All @@ -110,6 +110,13 @@ android.libraryVariants.all { variant ->
from file("build/intermediates/cmake/release/obj/")
}
}
// Custom task to package headers into a ZIP file
def headerZip = task("${variant.name}Header", type: Zip, dependsOn: 'build') {
archiveClassifier = "header"
from '../../../Core/include'
archiveFileName = 'headers.zip'
destinationDirectory = file("$buildDir/outputs/headers")
}

def publicationName = "component${variant.name.capitalize()}"

Expand All @@ -131,6 +138,7 @@ android.libraryVariants.all { variant ->
}
artifact javadocJar
artifact jniSymbolsJar
artifact headerZip

pom {
name = 'MMKV'
Expand Down
5 changes: 5 additions & 0 deletions Android/MMKV/mmkv/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ android {
}
}
}
// sourceSets {
// main {
// assets.srcDirs += '../../../Core/include' // Include headers as assets
// }
// }
}

configurations {
Expand Down
9 changes: 6 additions & 3 deletions Android/MMKV/mmkv/src/main/cpp/flutter-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
* limitations under the License.
*/

#include "MMKVPredef.h"
#include <MMKV/MMKVPredef.h>

#ifndef MMKV_DISABLE_FLUTTER

# include "MMKV.h"
# include "MMKVLog.h"
# include <MMKV/MMKV.h>
# include <MMKV/MMKVLog.h>
# include <cstdint>
# include <string>

Expand All @@ -35,6 +35,9 @@ extern int g_android_api;
extern string g_android_tmpDir;
}

# ifdef MMKV_EXPORT
# undef MMKV_EXPORT
# endif
# define MMKV_EXPORT extern "C" __attribute__((visibility("default"))) __attribute__((used))

using LogCallback_t = void (*)(uint32_t level, const char *file, int32_t line, const char *funcname, const char *message);
Expand Down
10 changes: 5 additions & 5 deletions Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
* limitations under the License.
*/

#include "MMKVPredef.h"
#include <MMKV/MMKVPredef.h>

#ifdef MMKV_ANDROID

# include "MMBuffer.h"
# include "MMKV.h"
# include "MMKVLog.h"
# include "MemoryFile.h"
# include <MMKV/MMBuffer.h>
# include <MMKV/MMKV.h>
# include <MMKV/MMKVLog.h>
# include <MMKV/MemoryFile.h>
# include <cstdint>
# include <jni.h>
# include <string>
Expand Down
6 changes: 3 additions & 3 deletions Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ private static String doInitialize(String rootDir, String cacheDir, LibLoader lo
return MMKV.rootDir;
}

private static boolean g_isNativeLibLoaded = false;
private static boolean isNativeLibLoaded = false;

private static void tryLoadNativeLib(@Nullable LibLoader loader) {
if (g_isNativeLibLoaded) {
if (isNativeLibLoaded) {
return;
}
if (loader != null) {
Expand All @@ -240,7 +240,7 @@ private static void tryLoadNativeLib(@Nullable LibLoader loader) {
}
System.loadLibrary("mmkv");
}
g_isNativeLibLoaded = true;
isNativeLibLoaded = true;
}

/**
Expand Down
1 change: 1 addition & 0 deletions Core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include/
42 changes: 41 additions & 1 deletion Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ ELSE()
aes/openssl/openssl_aes-armv4.S)
ENDIF()

target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

IF (WIN32)
# MMKV can be used only with Unicode on Windows.
Expand All @@ -170,3 +170,43 @@ ELSE()
target_link_libraries(core ${zlib})
ENDIF()

function(copy_files)
list(LENGTH ARGN num_args)

if(num_args LESS 2)
message(FATAL_ERROR "copy_files function requires at least one source file and one destination directory.")
return()
endif()

# Get the last argument as the destination directory
list(GET ARGN -1 destination_dir)
# Get all arguments except the last one as the source files
list(REMOVE_AT ARGN -1)
set(source_files ${ARGN})

file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${destination_dir})

foreach(file ${source_files})
get_filename_component(file_dir ${file} DIRECTORY)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${destination_dir}/${file_dir})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${file} DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/${destination_dir}/${file_dir})
endforeach()
endfunction()

file(REMOVE_RECURSE ${CMAKE_CURRENT_SOURCE_DIR}/include)

copy_files(
MMKV.h
MMKVPredef.h
MMBuffer.h
MiniPBCoder.h
include/MMKV)

IF(OHOS OR ANDROID)
copy_files(
MMKVLog.h
MemoryFile.h
include/MMKV)
ENDIF()

target_include_directories(core INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
2 changes: 1 addition & 1 deletion Core/MMBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum MMBufferCopyFlag : bool {
struct KeyValueHolderCrypt;
#endif

class MMBuffer {
class MMKV_EXPORT MMBuffer {
enum MMBufferType : uint8_t {
MMBufferType_Small, // store small buffer in stack memory
MMBufferType_Normal, // store in heap memory
Expand Down
5 changes: 2 additions & 3 deletions Core/MMKV.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ concept MMKV_SUPPORTED_VALUE_TYPE = MMKV_SUPPORTED_PRIMITIVE_VALUE_TYPE<T> || MM
MMKV_SUPPORTED_VECTOR_VALUE_TYPE<T>;
#endif // MMKV_HAS_CPP20

class MMKV_EXPORT MMKV {
#ifndef MMKV_ANDROID
class MMKV {
MMKV(const std::string &mmapID, MMKVMode mode, const std::string *cryptKey, const MMKVPath_t *rootPath, size_t expectedCapacity = 0);
#else // defined(MMKV_ANDROID)
class __attribute__((visibility("default"))) MMKV {
mmkv::FileLock *m_fileModeLock;
mmkv::InterProcessLock *m_sharedProcessModeLock;
mmkv::InterProcessLock *m_exclusiveProcessModeLock;
Expand Down Expand Up @@ -604,7 +603,7 @@ MMKV_NAMESPACE_END
namespace mmkv {

// a POD-like facade what wraps custom root directory
class NameSpace {
class MMKV_EXPORT NameSpace {
const MMKVPath_t &m_rootDir;
NameSpace(const MMKVPath_t &rootDir) : m_rootDir(rootDir) {}
public:
Expand Down
6 changes: 6 additions & 0 deletions Core/MMKVPredef.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,10 @@ constexpr size_t AES_KEY_BITSET_LEN = 128;
#define MMKV_ABI "unknown"
#endif

#ifdef MMKV_ANDROID
#define MMKV_EXPORT __attribute__((visibility("default")))
#else
#define MMKV_EXPORT
#endif

#endif //MMKV_SRC_MMKVPREDEF_H
2 changes: 1 addition & 1 deletion Core/MiniPBCoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AESCrypt;
class CodedInputDataCrypt;
struct PBEncodeItem;

class MiniPBCoder {
class MMKV_EXPORT MiniPBCoder {
const MMBuffer *m_inputBuffer = nullptr;
CodedInputData *m_inputData = nullptr;
CodedInputDataCrypt *m_inputDataDecrpt = nullptr;
Expand Down

0 comments on commit 7cad5d3

Please sign in to comment.