-
-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from mrousavy/try-use-custom-getjsimodules-hook
Try use custom getjsimodules hook
- Loading branch information
Showing
6 changed files
with
157 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
android/src/main/java/com/reactnativemmkv/MmkvModulePackage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
android/src/main/java/com/reactnativemmkv/MmkvPackage.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters