From 1a9972979b05139ea50872d3cd254106c750f2f8 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Tue, 7 Feb 2023 10:46:58 +0100 Subject: [PATCH] Compatibility with the old arch --- android/build.gradle | 47 ++++++++++++++++- .../NativeAsyncStorageModuleSpec.java | 52 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 android/src/paper/java/com/reactnativecommunity/asyncstorage/NativeAsyncStorageModuleSpec.java diff --git a/android/build.gradle b/android/build.gradle index 73a77b64..0079f44f 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -29,6 +29,43 @@ def getVersionOrDefault(String flagName, String defaultVersion) { rootProject.hasProperty(flagName) ? rootProject.properties[flagName] : defaultVersion } +def resolveReactNativeDirectory() { + def reactNativeLocation = safeExtGet("REACT_NATIVE_NODE_MODULES_DIR", null) + if (reactNativeLocation != null) { + return file(reactNativeLocation) + } + + // monorepo workaround + // react-native can be hoisted or in project's own node_modules + def reactNativeFromProjectNodeModules = file("${rootProject.projectDir}/../node_modules/react-native") + if (reactNativeFromProjectNodeModules.exists()) { + return reactNativeFromProjectNodeModules + } + + def reactNativeFromNodeModulesWithReanimated = file("${projectDir}/../../react-native") + if (reactNativeFromNodeModulesWithReanimated.exists()) { + return reactNativeFromNodeModulesWithReanimated + } + + throw new Exception( + "[react-native-async-storage] Unable to resolve react-native location in " + + "node_modules. You should add project extension property (in app/build.gradle) " + + "`REACT_NATIVE_NODE_MODULES_DIR` with path to react-native." + ) +} + +def getReactNativeMinorVersion() { + def REACT_NATIVE_DIR = resolveReactNativeDirectory() + + def reactProperties = new Properties() + file("$REACT_NATIVE_DIR/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) } + + def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME") + def REACT_NATIVE_MINOR_VERSION = REACT_NATIVE_VERSION.startsWith("0.0.0-") ? 1000 : REACT_NATIVE_VERSION.split("\\.")[1].toInteger() + + return REACT_NATIVE_MINOR_VERSION +} + def isNewArchitectureEnabled() { // To opt-in for the New Architecture, you can either: // - Set `newArchEnabled` to true inside the `gradle.properties` file @@ -128,6 +165,10 @@ android { } else { srcDirs += 'src/javaPackage/java' } + + if (!isNewArchitectureEnabled()) { + srcDirs += 'src/paper/java' + } } } } @@ -174,5 +215,9 @@ dependencies { } //noinspection GradleDynamicVersion - implementation 'com.facebook.react:react-native:+' // From node_modules + if (isNewArchitectureEnabled() && getReactNativeMinorVersion() < 71) { + implementation project(":ReactAndroid") + } else { + implementation 'com.facebook.react:react-native:+' // from node_modules + } } diff --git a/android/src/paper/java/com/reactnativecommunity/asyncstorage/NativeAsyncStorageModuleSpec.java b/android/src/paper/java/com/reactnativecommunity/asyncstorage/NativeAsyncStorageModuleSpec.java new file mode 100644 index 00000000..b5fdc3e7 --- /dev/null +++ b/android/src/paper/java/com/reactnativecommunity/asyncstorage/NativeAsyncStorageModuleSpec.java @@ -0,0 +1,52 @@ + +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GenerateModuleJavaSpec.js + * + * @nolint + */ + +package com.reactnativecommunity.asyncstorage; + +import com.facebook.proguard.annotations.DoNotStrip; +import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.ReactModuleWithSpec; +import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.turbomodule.core.interfaces.TurboModule; + +public abstract class NativeAsyncStorageModuleSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule { + public NativeAsyncStorageModuleSpec(ReactApplicationContext reactContext) { + super(reactContext); + } + + @ReactMethod + @DoNotStrip + public abstract void multiGet(ReadableArray keys, Callback callback); + + @ReactMethod + @DoNotStrip + public abstract void multiSet(ReadableArray kvPairs, Callback callback); + + @ReactMethod + @DoNotStrip + public abstract void multiRemove(ReadableArray keys, Callback callback); + + @ReactMethod + @DoNotStrip + public abstract void multiMerge(ReadableArray kvPairs, Callback callback); + + @ReactMethod + @DoNotStrip + public abstract void getAllKeys(Callback callback); + + @ReactMethod + @DoNotStrip + public abstract void clear(Callback callback); +}