You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加@react-native-community/netinfo后Android编译失败求助

React Native Android编译失败问题(引入@react-native-community/netinfo后)

问题描述

在React Native项目中引入@react-native-community/netinfo库后,Android端编译失败,但iOS端可正常运行。已尝试清理Gradle、配置ANDROID_HOME环境变量等操作,均未解决问题。

项目依赖版本

"react-native": "0.70.5",
"react-native-actions-sheet": "^0.7.2",
"react-native-chart-kit": "^6.12.0",
"react-native-cli": "^2.0.1",
"@react-native-community/netinfo": "^11.3.2",

Android编译错误信息

See https://docs.gradle.org/7.3.3/userguide/command_line_interface.html#sec:command_line_warnings
153 actionable tasks: 147 executed, 6 up-to-date
Warning: This version only understands SDK XML versions up to 3 but an SDK XML file of version 4 was encountered. This can happen if you use versions of Android Studio and the command-line tools that were released at different times.
Note: D:\Projects\ReactNative\Project\node_modules\@react-native-async-storage\async-storage\android\src\main\java\com\reactnativecommunity\asyncstorage\AsyncStorageModule.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: D:\Projects\ReactNative\Project\node_modules\@react-native-async-storage\async-storage\android\src\main\java\com\reactnativecommunity\asyncstorage\AsyncStoragePackage.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
D:\Projects\ReactNative\Project\node_modules\@react-native-community\netinfo\android\src\main\java\com\reactnativecommunity\netinfo\NetInfoUtils.java:45: error: cannot find symbol
                    receiver, filter, exported ? Context.RECEIVER_EXPORTED : Context.RECEIVER_NOT_EXPORTED);
                                                        ^
  symbol:   variable RECEIVER_EXPORTED
  location: class Context
D:\Projects\ReactNative\Project\node_modules\@react-native-community\netinfo\android\src\main\java\com\reactnativecommunity\netinfo\NetInfoUtils.java:45: error: cannot find symbol
                    receiver, filter, exported ? Context.RECEIVER_EXPORTED : Context.RECEIVER_NOT_EXPORTED);
                                                                                    ^
  symbol:   variable RECEIVER_NOT_EXPORTED
  location: class Context
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
2 errors

FAILURE: Build failed with an exception.

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

Build Gradle配置

buildscript {
    ext {
        buildToolsVersion = "31.0.0"
        minSdkVersion = 21
        compileSdkVersion = 32
        targetSdkVersion = 33
        kotlinVersion = "1.6.10"

        if (System.properties['os.arch'] == "aarch64") {
            // For M1 Users we need to use the NDK 24 which added support for aarch64
            ndkVersion = "24.0.8215888"
        } else {
            // Otherwise we default to the side-by-side NDK version from AGP.
            ndkVersion = "21.4.7075529"
        }
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath ('com.android.tools.build:gradle:2.3.1')
        classpath("com.facebook.react:react-native-gradle-plugin")
        classpath("de.undercouch:gradle-download-task:5.0.1")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" // <- add this line
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        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")
        }
        mavenCentral {
            // We don't want to fetch react-native from Maven Central as there are
            // older versions over there.
            content {
                excludeGroup "com.facebook.react"
            }
        }
        google()
        maven { url 'https://www.jitpack.io' }
    }
}

环境信息

  • JDK版本:17

解决方案

原因分析

错误中提到的Context.RECEIVER_EXPORTED和Context.RECEIVER_NOT_EXPORTED是Android API 33(Android 13)新增的常量。当前使用的@react-native-community/netinfo@11.3.2版本依赖这些API,但项目compileSdkVersion设为32,导致编译时无法找到常量。同时,Gradle插件版本2.3.1过于老旧,与React Native 0.70.5不兼容。

修复步骤

  1. 降级netinfo版本
    适配React Native 0.70.5,将@react-native-community/netinfo降级到^9.3.10,该版本不依赖API 33的新常量。执行命令:

    npm install @react-native-community/netinfo@9.3.10 --save
    
  2. 更新Gradle插件版本
    React Native 0.70.5要求Gradle插件版本至少为7.3.1,修改buildscript中的依赖:

    classpath ('com.android.tools.build:gradle:7.3.1')
    
  3. 清理并重新构建
    执行以下命令清理缓存并重新编译项目:

    cd android && ./gradlew clean && cd .. && npx react-native run-android
    

内容的提问来源于stack exchange,提问作者Ahsan Baloch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 18:54:51