MMKV uses JSI which has not been officially released. For now, you have to manually edit a Java file to correctly set up MMKV.
Since react-native-reanimated also uses JSI, there will be conflicts if you install both libraries at the same time. That's why the installation steps are different:
Without react-native-reanimated
To install MMKV without Reanimated, open your Android project (the android
folder) in Android Studio. In MainApplication.java
find the location where the ReactNativeHost
is initialized. You have to override it's getJSIModulePackage
method:
import com.reactnativemmkv.MmkvModulePackage;
import com.facebook.react.bridge.JSIModulePackage;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost =
new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return new PackageList(this).getPackages();
}
@Override
protected String getJSMainModuleName() {
return "index";
}
// Add this method here!
@Override
protected JSIModulePackage getJSIModulePackage() {
return new MmkvModulePackage();
}
};
// ...
With react-native-reanimated
To install MMKV with Reanimated, open your Android project (the android
folder) in Android Studio.
- Find the folder where
MainActivity.java
andMainApplication.java
live. - Right click, "New" > "Java class"
- Call it whatever you prefer, in my case it's
ExampleJSIPackage
because my app is called "Example". - Add the following code:
package com.example;
import com.facebook.react.bridge.JSIModuleSpec;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.ReactApplicationContext;
import com.swmansion.reanimated.ReanimatedJSIModulePackage;
import com.reactnativemmkv.MmkvModule;
import java.util.Collections;
import java.util.List;
// TODO: Remove all of this when MMKV and Reanimated can be autoinstalled (maybe RN 0.65)
public class ExampleJSIPackage extends ReanimatedJSIModulePackage {
@Override
public List<JSIModuleSpec> getJSIModules(ReactApplicationContext reactApplicationContext, JavaScriptContextHolder jsContext) {
MmkvModule.install(jsContext, reactApplicationContext.getFilesDir().getAbsolutePath() + "/mmkv");
return super.getJSIModules(reactApplicationContext, jsContext);
}
}
-
Replace
com.example
with your package namespace -
Open
MainApplication.java
and find the location where theReactNativeHost
is initialized. You have to override it'sgetJSIModulePackage
method:
import com.facebook.react.bridge.JSIModulePackage;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost =
new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return new PackageList(this).getPackages();
}
@Override
protected String getJSMainModuleName() {
return "index";
}
// Add this method here!
@Override
protected JSIModulePackage getJSIModulePackage() {
return new ExampleJSIPackage(); // <-- your package's name
}
};
// ...
All of this is a temporary workaround. JSI and TurboModules are still actively in development and cannot be autolinked yet. All of this will change very soon and no extra configuration will be needed to use MMKV.