React Native安卓构建报错:无法调用null对象的mavenDeployer()方法(第131行)
React Native安卓构建错误:Cannot invoke method mavenDeployer() on null object (line 131)
问题描述
构建React Native安卓项目时触发错误:
Cannot invoke method mavenDeployer() on null object (line 131)
相关代码片段:
仓库配置
repositories { 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() }
任务配置(错误行131)
task installArchives(type: Upload) { configuration = configurations.archives repositories.mavenDeployer { // line 131 // Deploy to react-native-event-bridge/maven, ready to publish to npm repository url: "file://${projectDir}/../android/maven" configureReactNativePom pom } }
尝试修改repositories.mavenDeployer相关代码后,错误依旧。
修复方案
方案1:适配新版Gradle(推荐)
Upload任务和mavenDeployer在Android Gradle Plugin 4.0+中已被废弃,改用maven-publish插件:
- 在模块
build.gradle顶部引入插件:
plugins { id 'com.android.library' // 若为应用模块则用id 'com.android.application' id 'maven-publish' }
- 替换原
installArchives任务为发布配置:
afterEvaluate { publishing { publications { maven(MavenPublication) { from components.release // 按需切换为debug groupId = "你的组ID" // 示例:com.yourteam artifactId = "react-native-event-bridge" version = "1.0.0" pom { name = "React Native Event Bridge" description = "自定义事件桥接库" // 若需复用原configureReactNativePom逻辑,可在此处迁移相关配置 } } } repositories { maven { url = uri("file://${projectDir}/../android/maven") } } } }
- 执行发布命令:
./gradlew publishToMavenLocal
方案2:修复旧版Gradle任务写法
若仍使用旧版Gradle,需确保任务内repositories正确初始化,原写法中repositories对象未实例化导致null:
修改任务代码:
task installArchives(type: Upload) { configuration = configurations.archives repositories { mavenDeployer { repository url: "file://${projectDir}/../android/maven" configureReactNativePom pom } } }
方案3:检查自定义方法有效性
确认configureReactNativePom方法在build.gradle中已正确定义,若该方法缺失或异常,也会间接导致mavenDeployer初始化失败。
内容的提问来源于stack exchange,提问作者honk19
相关产品推荐
相关产品推荐

