Skip to content

Commit

Permalink
Merge pull request #35 from mrousavy/try-use-custom-getjsimodules-hook
Browse files Browse the repository at this point in the history
Try use custom getjsimodules hook
  • Loading branch information
mrousavy committed Mar 22, 2021
2 parents 870d03e + d2d8e93 commit cadc1b8
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 59 deletions.
116 changes: 116 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Install MMKV

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:

<details>
<summary>Without react-native-reanimated</summary>

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:


```java
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();
}
};

// ...
```

</details>

<details>
<summary>With react-native-reanimated</summary>

To install MMKV with Reanimated, open your Android project (the `android` folder) in Android Studio.

1. Find the folder where `MainActivity.java` and `MainApplication.java` live.
2. Right click, "New" > "Java class"
3. Call it whatever you prefer, in my case it's `ExampleJSIPackage` because my app is called "Example".
4. Add the following code:

```java
import com.facebook.react.bridge.JSIModulePackage;
import com.facebook.react.bridge.JSIModuleSpec;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.ReactApplicationContext;
import com.reactnativemmkv.MmkvModule;

import com.swmansion.reanimated.NodesManager;
import com.swmansion.reanimated.ReanimatedModule;

import java.util.Collections;
import java.util.List;

public class ExampleJSIPackage implements JSIModulePackage {
@Override
public List<JSIModuleSpec> getJSIModules(ReactApplicationContext reactApplicationContext, JavaScriptContextHolder jsContext) {
NodesManager nodesManager = reactApplicationContext.getNativeModule(ReanimatedModule.class).getNodesManager();
nodesManager.initWithContext(reactApplicationContext);
MmkvModule.install(jsContext, reactApplicationContext.getFilesDir().getAbsolutePath() + "/mmkv");
return Collections.emptyList();
}
}
```

5. Open `MainApplication.java` and find the location where the `ReactNativeHost` is initialized. You have to override it's `getJSIModulePackage` method:
```java
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
}
};

// ...
```

</details>


## Notes

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.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,20 @@

```sh
npm install react-native-mmkv
```

### iOS

iOS installation is automatic, just run:

```sh
cd ios && pod install
```

### Android

To correctly initialize MMKV on Android, please follow the [Installation guide](./INSTALL.md).

## Usage

### Set
Expand Down
31 changes: 5 additions & 26 deletions android/src/main/java/com/reactnativemmkv/MmkvModule.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
package com.reactnativemmkv;

import androidx.annotation.NonNull;
import com.facebook.react.bridge.JavaScriptContextHolder;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;

public class MmkvModule extends ReactContextBaseJavaModule {
public class MmkvModule {
static {
System.loadLibrary("mmkvnative");
}

private native void nativeInstall(long jsiPtr, String path);

public MmkvModule(ReactApplicationContext context) {
super(context);
}

@NonNull
@Override
public String getName() {
return "MMKV";
}

@Override
public void initialize() {
super.initialize();
private static native void nativeInstall(long jsiPtr, String path);

this.getReactApplicationContext().runOnJSQueueThread(() -> {
nativeInstall(
this.getReactApplicationContext().getJavaScriptContextHolder().get(),
this.getReactApplicationContext().getFilesDir().getAbsolutePath() + "/mmkv"
);
});
public static void install(JavaScriptContextHolder jsContext, String storageDirectory) {
nativeInstall(jsContext.get(), storageDirectory);
}
}
16 changes: 16 additions & 0 deletions android/src/main/java/com/reactnativemmkv/MmkvModulePackage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.reactnativemmkv;

import com.facebook.react.bridge.JSIModulePackage;
import com.facebook.react.bridge.JSIModuleSpec;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.ReactApplicationContext;
import java.util.Collections;
import java.util.List;

public class MmkvModulePackage implements JSIModulePackage {
@Override
public List<JSIModuleSpec> getJSIModules(ReactApplicationContext reactApplicationContext, JavaScriptContextHolder jsContext) {
MmkvModule.install(jsContext, reactApplicationContext.getFilesDir().getAbsolutePath() + "/mmkv");
return Collections.emptyList();
}
}
26 changes: 0 additions & 26 deletions android/src/main/java/com/reactnativemmkv/MmkvPackage.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import android.app.Application;
import android.content.Context;

import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.JSIModulePackage;
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import com.reactnativemmkv.MmkvPackage;
import com.reactnativemmkv.MmkvModulePackage;

public class MainApplication extends Application implements ReactApplication {

Expand All @@ -23,18 +25,18 @@ public boolean getUseDeveloperSupport() {

@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for MmkvExample:
// packages.add(new MyReactNativePackage());
packages.add(new MmkvPackage());
return packages;
return new PackageList(this).getPackages();
}

@Override
protected String getJSMainModuleName() {
return "index";
}

@Override
protected JSIModulePackage getJSIModulePackage() {
return new MmkvModulePackage();
}
};

@Override
Expand Down

0 comments on commit cadc1b8

Please sign in to comment.