Keep Flutter MMKV Core linked with SwiftPM #1676

This commit is contained in:
guoling
2026-08-10 14:57:28 +08:00
parent 10cfef6332
commit bd7c8e6d9f
3 changed files with 14 additions and 0 deletions
@@ -25,6 +25,8 @@
@implementation MMKVPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
mmkvEnsureCoreLinked();
FlutterMethodChannel *channel = [FlutterMethodChannel
methodChannelWithName:@"mmkv"
binaryMessenger:[registrar messenger]];
@@ -19,6 +19,12 @@ using ErrorCallback_t = uint32_t (*)(const char *mmapID, uint32_t errorType);
using ContenctChangeCallback_t = void (*)(const char *mmapID);
using LogCallback_t = void (*)(uint32_t level, const char *file, int32_t line, const char *funcname, const char *message);
// SwiftPM packages native targets as static archives. Dart resolves the FFI
// entry points with dlsym(), so those lookups don't create linker references
// that would extract this translation unit from the archive. MMKVPlugin calls
// this anchor to keep the bridge, and therefore MMKVCore, in the final binary.
extern "C" void mmkvEnsureCoreLinked(void);
@interface MyMMKVHandler : NSObject<MMKVHandler>
@property (atomic, assign) ErrorCallback_t errorCallback;
@@ -27,6 +27,12 @@
using namespace std;
extern "C" __attribute__((noinline, visibility("hidden"))) void mmkvEnsureCoreLinked(void) {
// The compiler must preserve this call even under whole-module optimization.
// Its relocation is what extracts this object from the SwiftPM archive.
__asm__ __volatile__("" ::: "memory");
}
MMKV_EXPORT void *mmkvInitialize(const char *rootDir, const char *groupDir, int32_t logLevel, LogCallback_t logCallback) {
auto handler = [MyMMKVHandler getHandler];
handler.logCallback = logCallback;