|
| 1 | +# Install MMKV |
| 2 | + |
| 3 | +MMKV uses JSI which has not been officially released. For now, you have to manually edit a Java file to correctly set up MMKV. |
| 4 | + |
| 5 | +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: |
| 6 | + |
| 7 | +<details> |
| 8 | +<summary>Without react-native-reanimated</summary> |
| 9 | + |
| 10 | +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: |
| 11 | + |
| 12 | + |
| 13 | +```java |
| 14 | +public class MainApplication extends Application implements ReactApplication { |
| 15 | + |
| 16 | + private final ReactNativeHost mReactNativeHost = |
| 17 | + new ReactNativeHost(this) { |
| 18 | + @Override |
| 19 | + public boolean getUseDeveloperSupport() { |
| 20 | + return BuildConfig.DEBUG; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + protected List<ReactPackage> getPackages() { |
| 25 | + return new PackageList(this).getPackages(); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + protected String getJSMainModuleName() { |
| 30 | + return "index"; |
| 31 | + } |
| 32 | + |
| 33 | + // Add this method here! |
| 34 | + @Override |
| 35 | + protected JSIModulePackage getJSIModulePackage() { |
| 36 | + return new MmkvModulePackage(); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + // ... |
| 41 | +``` |
| 42 | + |
| 43 | +</details> |
| 44 | + |
| 45 | +<details> |
| 46 | +<summary>With react-native-reanimated</summary> |
| 47 | + |
| 48 | +To install MMKV with Reanimated, open your Android project (the `android` folder) in Android Studio. |
| 49 | + |
| 50 | +1. Find the folder where `MainActivity.java` and `MainApplication.java` live. |
| 51 | +2. Right click, "New" > "Java class" |
| 52 | +3. Call it whatever you prefer, in my case it's `ExampleJSIPackage` because my app is called "Example". |
| 53 | +4. Add the following code: |
| 54 | + |
| 55 | +```java |
| 56 | +import com.facebook.react.bridge.JSIModulePackage; |
| 57 | +import com.facebook.react.bridge.JSIModuleSpec; |
| 58 | +import com.facebook.react.bridge.JavaScriptContextHolder; |
| 59 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 60 | +import com.reactnativemmkv.MmkvModule; |
| 61 | + |
| 62 | +import com.swmansion.reanimated.NodesManager; |
| 63 | +import com.swmansion.reanimated.ReanimatedModule; |
| 64 | + |
| 65 | +import java.util.Collections; |
| 66 | +import java.util.List; |
| 67 | + |
| 68 | +public class ExampleJSIPackage implements JSIModulePackage { |
| 69 | + @Override |
| 70 | + public List<JSIModuleSpec> getJSIModules(ReactApplicationContext reactApplicationContext, JavaScriptContextHolder jsContext) { |
| 71 | + NodesManager nodesManager = reactApplicationContext.getNativeModule(ReanimatedModule.class).getNodesManager(); |
| 72 | + nodesManager.initWithContext(reactApplicationContext); |
| 73 | + MmkvModule.install(jsContext, reactApplicationContext.getFilesDir().getAbsolutePath() + "/mmkv"); |
| 74 | + return Collections.emptyList(); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +5. Open `MainApplication.java` and find the location where the `ReactNativeHost` is initialized. You have to override it's `getJSIModulePackage` method: |
| 80 | +
|
| 81 | +```java |
| 82 | +public class MainApplication extends Application implements ReactApplication { |
| 83 | +
|
| 84 | + private final ReactNativeHost mReactNativeHost = |
| 85 | + new ReactNativeHost(this) { |
| 86 | + @Override |
| 87 | + public boolean getUseDeveloperSupport() { |
| 88 | + return BuildConfig.DEBUG; |
| 89 | + } |
| 90 | +
|
| 91 | + @Override |
| 92 | + protected List<ReactPackage> getPackages() { |
| 93 | + return new PackageList(this).getPackages(); |
| 94 | + } |
| 95 | +
|
| 96 | + @Override |
| 97 | + protected String getJSMainModuleName() { |
| 98 | + return "index"; |
| 99 | + } |
| 100 | +
|
| 101 | + // Add this method here! |
| 102 | + @Override |
| 103 | + protected JSIModulePackage getJSIModulePackage() { |
| 104 | + return new ExampleJSIPackage(); // <-- your package's name |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + // ... |
| 109 | +``` |
| 110 | + |
| 111 | +</details> |
| 112 | + |
| 113 | + |
| 114 | +## Notes |
| 115 | + |
| 116 | +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. |
0 commit comments