ammarahm-ed / react-native-mmkv-storage

An ultra fast (0.0002s read/write), small & encrypted mobile key-value storage framework for React Native written in C++ using JSI
https://rnmmkv.now.sh
MIT License
1.58k stars 109 forks source link

[Bug] v0.8.0 doesn't work with RN 0.71 #300

Closed pnthach95 closed 1 year ago

pnthach95 commented 1 year ago

Describe the bug RN 0.71 has a lot of change in android folder, so I try to run it with mmkv-storage and it is failed.

To Reproduce Steps to reproduce the behavior:

  1. Generate a new project npx react-native init RN0710RC4 --version 0.71.0-rc.4
  2. Install mmkv-storage
  3. yarn run android
  4. See error

Expected behavior Build success and install app to phone and run.

Screenshots I have log

❯ yarn run android
yarn run v1.22.19
$ react-native run-android
info Starting JS server...
info Installing the app...
5 actionable tasks: 5 up-to-date

FAILURE: Build failed with an exception.

* Where:
Build file '/Volumes/code/Projects/RN0710RC4/node_modules/react-native-mmkv-storage/android/build.gradle' line: 138

* What went wrong:
A problem occurred evaluating project ':react-native-mmkv-storage'.
> Expected directory '/Volumes/code/Projects/RN0710RC4/node_modules/react-native-mmkv-storage/android/../../../node_modules/react-native/android' to contain exactly one file, however, it contains no files.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* Where:
Build file '/Volumes/code/Projects/RN0710RC4/node_modules/react-native-mmkv-storage/android/build.gradle' line: 138

* What went wrong:
A problem occurred evaluating project ':react-native-mmkv-storage'.
> Expected directory '/Volumes/code/Projects/RN0710RC4/node_modules/react-native-mmkv-storage/android/../../../node_modules/react-native/android' to contain exactly one file, however, it contains no files.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

    at makeError (/Volumes/code/Projects/RN0710RC4/node_modules/execa/index.js:174:9)
    at /Volumes/code/Projects/RN0710RC4/node_modules/execa/index.js:278:16
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async runOnAllDevices (/Volumes/code/Projects/RN0710RC4/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:85:5)
    at async Command.handleAction (/Volumes/code/Projects/RN0710RC4/node_modules/@react-native-community/cli/build/index.js:108:9)
info Run CLI with --verbose flag for more details.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Platform Information:

Additional context The log is same as #165 Although it is RC release, things may be changed in official release, you should have future-proof solution.

pnthach95 commented 1 year ago

Note for library author about android folder

focux commented 1 year ago

There's an open PR that fixes this: #305

robrechtme commented 1 year ago

If you use patch-package you can apply this patch in the mean time:

react-native-mmkv-storage+0.8.0.patch ```diff diff --git a/node_modules/react-native-mmkv-storage/android/build.gradle b/node_modules/react-native-mmkv-storage/android/build.gradle index a806e23..56035e7 100644 --- a/node_modules/react-native-mmkv-storage/android/build.gradle +++ b/node_modules/react-native-mmkv-storage/android/build.gradle @@ -1,79 +1,75 @@ -import groovy.json.JsonSlurper - buildscript { - repositories { - google() - mavenCentral() - maven { url "https://plugins.gradle.org/m2/" } + mavenCentral() + google() } dependencies { - classpath 'com.android.tools.build:gradle:4.2.1' + classpath 'com.android.tools.build:gradle:7.2.2' } } -apply plugin: 'com.android.library' +def resolveBuildType() { + Gradle gradle = getGradle() + String tskReqStr = gradle.getStartParameter().getTaskRequests()['args'].toString() -def getExtOrDefault(name, defaultValue) { - return rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue + return tskReqStr.contains('Release') ? 'release' : 'debug' } -def found = false -def reactNativeDir = null -def androidSourcesName = 'React Native sources' -def parentDir = rootProject.projectDir +def isNewArchitectureEnabled() { + // To opt-in for the New Architecture, you can either: + // - Set `newArchEnabled` to true inside the `gradle.properties` file + // - Invoke gradle with `-newArchEnabled=true` + // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true` + return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true" +} -if (rootProject.ext.has('reactNativeAndroidRoot')) { - reactNativeDir = rootProject.ext.get('reactNativeAndroidRoot') -} else { - reactNativeDir = new File( - projectDir, - '/../../../node_modules/react-native/android' - ) +if (isNewArchitectureEnabled()) { + apply plugin: 'com.facebook.react' } -if (reactNativeDir.exists()) { - found = true -} else { - 1.upto(5, { - if (found) return true - parentDir = reactNativeDir.parentFile - reactNativeDir = new File( - parentDir, - 'node_modules/react-native/android' - ) +apply plugin: 'com.android.library' + +def safeExtGet(prop, fallback) { + rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback +} - if (reactNativeDir.exists()) { - found = true - } - }) +def reactNativeArchitectures() { + def value = project.getProperties().get("reactNativeArchitectures") + return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"] } -def reactNative = reactNativeDir.absolutePath; -def reactNativeManifest = file("$reactNative/../package.json") -def reactNativeManifestAsJson = new JsonSlurper().parseText(reactNativeManifest.text) -def reactNativeVersion = reactNativeManifestAsJson.version as String -def (major, minor, patch) = reactNativeVersion.tokenize('.') -def rnMinorVersion = Integer.parseInt(minor) +repositories { + mavenCentral() +} android { - compileSdkVersion getExtOrDefault('compileSdkVersion', 28) + compileSdkVersion safeExtGet("compileSdkVersion", 28) - defaultConfig { - minSdkVersion getExtOrDefault('minSdkVersion', 21) - targetSdkVersion getExtOrDefault('targetSdkVersion', 28) - //ndkVersion "21.1.6352462" + buildFeatures { + prefab true + } + defaultConfig { + minSdkVersion safeExtGet('minSdkVersion', 16) + targetSdkVersion safeExtGet('targetSdkVersion', 28) externalNativeBuild { cmake { - cppFlags "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID" - abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' - arguments '-DANDROID_STL=c++_shared', "-DNODE_MODULES_DIR=${reactNativeDir.parentFile.parentFile.toString()}", "-DRN_VERSION=${rnMinorVersion}" + cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all" + arguments "-DANDROID_STL=c++_shared" + abiFilters (*reactNativeArchitectures()) } } + packagingOptions { + doNotStrip resolveBuildType() == 'debug' ? "**/**/*.so" : '' + excludes = [ + "META-INF", + "META-INF/**", + "**/libjsi.so", + ] + } } compileOptions { @@ -81,69 +77,16 @@ android { targetCompatibility JavaVersion.VERSION_1_8 } - lintOptions{ - abortOnError false - disable 'GradleCompatible' - } - externalNativeBuild { cmake { path "./src/main/rnmmkv/CMakeLists.txt" } } - - packagingOptions { - excludes = ["**/libc++_shared.so","**/libjsi.so","**/libreactnativejni.so", "META-INF/MANIFEST.MF"] - } -} - -repositories { - mavenCentral() - mavenLocal() - google() - - if (reactNativeDir.exists()) { - maven { - url reactNativeDir.toString() - name androidSourcesName - } - logger.info(":${project.name}:reactNativeAndroidRoot ${reactNativeDir.canonicalPath}") - } - - if (!found) { - throw new GradleException( - "${project.name}: unable to locate React Native android sources. " + - "Ensure you have you installed React Native as a dependency in your project and try again." - ) - } - } dependencies { //noinspection GradleDynamicVersion - implementation 'com.facebook.react:react-native:+' + implementation 'com.facebook.react:react-android:+' implementation "androidx.security:security-crypto:1.1.0-alpha03" implementation 'com.google.code.gson:gson:2.8.6' - def buildType = "debug" - tasks.all({ task -> - if (task.name == "buildCMakeRelease") { - buildType = "release" - } - }) - def rnAarMatcher = "**/react-native/**/*${buildType}.aar" - if (rnMinorVersion < 69) { - rnAarMatcher = "**/**/*.aar" - } - - def rnAAR = fileTree("$reactNative").matching({ it.include rnAarMatcher }).singleFile - files(rnAAR).each { - def file = it.absoluteFile - def aarFolder = new File("$buildDir/../$file.name"); - if (aarFolder.exists()) return; - copy { - from zipTree(file) - into "$buildDir/../$file.name" - include "jni/**/*" - } - } } diff --git a/node_modules/react-native-mmkv-storage/android/src/main/rnmmkv/CMakeLists.txt b/node_modules/react-native-mmkv-storage/android/src/main/rnmmkv/CMakeLists.txt index df79f03..c916c92 100644 --- a/node_modules/react-native-mmkv-storage/android/src/main/rnmmkv/CMakeLists.txt +++ b/node_modules/react-native-mmkv-storage/android/src/main/rnmmkv/CMakeLists.txt @@ -1,56 +1,23 @@ cmake_minimum_required(VERSION 3.9.0) + set (PACKAGE_NAME "rnmmkv") project(PACKAGE_NAME) set (BUILD_DIR ../../../) +set(CMAKE_VERBOSE_MAKEFILE ON) +set(CMAKE_CXX_STANDARD 17) add_subdirectory(../../../../MMKV/Core core) include_directories( ${PACKAGE_NAME} - "${NODE_MODULES_DIR}/react-native/React" - "${NODE_MODULES_DIR}/react-native/React/Base" - "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi" "./" ) -file (GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}") - -if (${RN_VERSION} LESS 65) - # React Native 0.65.x and below - add_library( - ${PACKAGE_NAME} - SHARED - ../../../../../react-native/ReactCommon/jsi/jsi/jsi.cpp - ./rnmmkv-adapter.cpp - ) - -else() - # React Native 0.66.x and above - add_library( - ${PACKAGE_NAME} - SHARED - ./rnmmkv-adapter.cpp - ) - - find_library( - JSI_LIB - jsi - PATHS ${LIBRN_DIR} - NO_CMAKE_FIND_ROOT_PATH - ) -endif() - -find_library( - LOG_LIB - log -) - -find_library( - REACT_NATIVE_JNI_LIB - reactnativejni - PATHS ${LIBRN_DIR} - NO_CMAKE_FIND_ROOT_PATH +add_library( + ${PACKAGE_NAME} + SHARED + ./rnmmkv-adapter.cpp ) set_target_properties( @@ -60,11 +27,13 @@ set_target_properties( POSITION_INDEPENDENT_CODE ON ) +find_package(ReactAndroid REQUIRED CONFIG) +find_library(log-lib log) + target_link_libraries( ${PACKAGE_NAME} - ${LOG_LIB} - ${REACT_NATIVE_JNI_LIB} - ${JSI_LIB} core + ${log-lib} + ReactAndroid::jsi android ) \ No newline at end of file ```
jkepps commented 1 year ago

@robrechtme thanks for the patch. i'm getting the following errors when running yarn

**ERROR** Failed to apply patch for package react-native-mmkv-storage

  This happened because the patch file patches/react-native-mmkv-storage+0.8.0.patch could not be parsed.

  If you just upgraded patch-package, you can try running:

    patch -p1 -i patches/react-native-mmkv-storage+0.8.0.patch
    npx patch-package react-native-mmkv-storage

  Otherwise, try manually creating the patch file again.

  If the problem persists, please submit a bug report:

    https://github.com/ds300/patch-package/issues/new?title=Patch+file+parse+error&body=%3CPlease+attach+the+patch+file+in+question%3E

and then when i try to run the patch command

> patch -p1 -i patches/react-native-mmkv-storage+0.8.0.patch
patching file 'node_modules/react-native-mmkv-storage/android/build.gradle'
patch: **** malformed patch at line 8: buildscript {
warrenfalk commented 1 year ago

Looks like github comments mangles the format... I went to the pull request and got the two files in robrechtme's patch and created it myself. Here's a gist:

https://gist.github.com/warrenfalk/97702b3908fdb23b6ae8d1bdcdf62316

japhet247 commented 1 year ago

After applying the patch I am still getting the error below. Please help. Using react-native v0.71.3

`error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.

2: Task failed with an exception.

BUILD FAILED in 12s

at makeError (/Users/jshewiyo/Desktop/PROJECTS/otr/otr-app/otr/node_modules/execa/index.js:174:9)
at /Users/jshewiyo/Desktop/PROJECTS/otr/otr-app/otr/node_modules/execa/index.js:278:16
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async runOnAllDevices (/Users/jshewiyo/Desktop/PROJECTS/otr/otr-app/otr/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:82:7)
at async Command.handleAction (/Users/jshewiyo/Desktop/PROJECTS/otr/otr-app/otr/node_modules/@react-native-community/cli/build/index.js:108:9)

info Run CLI with --verbose flag for more details.`

ammarahm-ed commented 1 year ago

Fixed in 0.9.0.

japhet247 commented 1 year ago

Unfortunately, the update did not work for me using react native v0.71.3. I am therefore forced to resort to the other plugin.

ammarahm-ed commented 1 year ago

Unfortunately, the update did not work for me using react native v0.71.3. I am therefore forced to resort to the other plugin.

Hmmm strange 🤔 i tested on a clean android project and everything worked and app is built. React Native version is latest.

pnthach95 commented 1 year ago

I can confirm v0.9.1 work on RN 0.71.4 https://github.com/pnthach95/mmkvtest/tree/rn71