React Native - 运行 react-native run-android 时出现 Async Storage 错误

10

我试图运行react-native run-android,但遇到以下错误:

Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081
D:\Work\CS\Wms-Mobile\CS-WmsMobile\node_modules\@react-native-community\async-storage\android\src\main\java\com\reactnativecommunity\asyncstorage\AsyncStorageModule.java:26: error: package com.facebook.react.module.annotations does not exist
import com.facebook.react.module.annotations.ReactModule;
                                            ^
D:\Work\CS\Wms-Mobile\CS-WmsMobile\node_modules\@react-native-community\async-storage\android\src\main\java\com\reactnativecommunity\asyncstorage\AsyncStorageModule.java:34: error: cannot find symbol     
@ReactModule(name = AsyncStorageModule.NAME)
 ^
  symbol: class ReactModule
2 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':react-native-community_async-storage:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* 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 1m 16s

    at checkExecSyncError (child_process.js:621:11)
    at execFileSync (child_process.js:639:15)
    at runOnAllDevices (D:\Work\CS\Wms-Mobile\CS-WmsMobile\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:94:39)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

我尝试过的方法:

  • 重新安装node_modules
  • 进入./android目录,然后运行./gradlew clean命令

但似乎都没有起作用。非常感谢您能提供建议,谢谢!

react-native-cli: 2.0.1

react-native: 0.61.4

android gradle plugin: 3.5.3

gradle: 5.4.1

6个回答

7

请尝试使用以下命令

react-native link @react-native-community/async-storage

有疑问请随时提出


3
不幸的是,这并不起作用,而且对于React Native 0.60+版本,React Native会为我们进行链接,所以当我尝试手动链接它时,它会引发一个错误。 - hrtlkr29

1

打开AVD管理器,尝试使用冷启动启动您的手机模拟器,然后关闭所有应用程序。再次清理和编译您的应用程序,现在它必须正常工作。 有时即使您清理了应用程序,您的模拟器也会卡住。


1

请尝试以下解决方案。

  1. 卸载Node模块
  2. 清理Gradle (cd android && gradlew clean)
  3. 重新安装您的模块 (yarn install)

如果这些方法都不起作用,请尝试升级您的软件包。

yarn upgrade --pattern react-native


0

您可以尝试将此代码添加到您的 android/build.gradle 文件中

   allprojects {
    repositories {
        exclusiveContent {
            // We get React Native's Android binaries exclusively through npm,
            // from a local Maven repo inside node_modules/react-native/.
            // (The use of exclusiveContent prevents looking elsewhere like Maven Central
            // and potentially getting a wrong version.)
            filter {
                includeGroup "com.facebook.react"
            }
            forRepository {
                maven {
                    // NOTE: if you are in a monorepo, you may have "$rootDir/../../../node_modules/react-native/android"
                    url "$rootDir/../node_modules/react-native/android"
                }
            }
        }
        mavenCentral()
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../../../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../../../node_modules/jsc-android/dist")
        }

        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

0

注解通常与 Jetpack 迁移有关, 请尝试使用 Jetifier 工具

只需确保在项目根目录下运行 npx jetify


0

在我的情况下,使用sudo解决了这个问题。sudo react-native ...sudo ./gradlew ...等。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接