Fix 'Unresolved reference: serviceOf' in React Native Gradle Plugin
When upgrading to Gradle 8.10 for React Native 0.73.9, you may encounter build failures with the following error:
e: file:///path/to/node_modules/@react-native/gradle-plugin/build.gradle.kts:10:49: Unresolved reference: serviceOf
FAILURE: Build failed with an exception.
* Where:
Build file ... line: 10
* What went wrong:
Unresolved reference: serviceOf
This error occurs because the serviceOf
method was removed in recent Gradle versions, causing compatibility issues with React Native's Gradle plugin. While the issue might work on some machines due to cached artifacts, others face immediate failures.
Solution: Patch the Gradle Plugin (Recommended)
The React Native team has already fixed this in a PR. Apply this fix locally using patch-package:
Step 1: Edit the Gradle Plugin File
Open /node_modules/@react-native/gradle-plugin/build.gradle.kts
and make these changes:
Remove deprecated import:
- import org.gradle.configurationcache.extensions.serviceOf
Replace service calls with modern equivalents:
- val projectName = stringProperty("project.name") ?: provider { "react-native" }
+ val projectName = providers.gradleProperty("project.name").forUseAtConfigurationTime().orElse("react-native")
- val moduleRegistry = serviceOf<ModuleRegistry>()
- .getModule(ModuleRegistry::class.java)
+ val moduleRegistry = project.objects.newInstance(
+ ModuleRegistry::class.java
+ )
Step 2: Install Patch-Package
npm install --save-dev patch-package
Step 3: Create Patch File
After modifying the plugin, generate a patch:
npx patch-package @react-native/gradle-plugin
Step 4: Update package.json
In your package.json
, add the postinstall script:
"scripts": {
"postinstall": "patch-package"
}
Verify Your Environment Configuration
Ensure your configuration matches these versions shown to work with React Native 0.73.9:
android/build.gradle:
buildscript {
ext {
buildToolsVersion = "34.0.0"
minSdkVersion = 23
compileSdkVersion = 34
targetSdkVersion = 34
ndkVersion = "25.1.8937393"
kotlinVersion = "1.8.0"
}
dependencies {
classpath("com.android.tools.build:gradle:8.4.0")
classpath("com.facebook.react:react-native-gradle-plugin:0.75.3")
}
}
gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-all.zip
Additional Troubleshooting Steps
Reset Your Environment
Clean your project artifacts before rebuilding:
# Clear Gradle caches
./gradlew clean
# Reset JavaScript artifacts
rm -rf node_modules
npm install --force
npm start --reset-cache
Avoid Outdated Fixes
Methods like npm audit fix --forced
or dependency deduplication may appear in community solutions, but do not resolve this specific serviceOf
error and can introduce new issues.
Why This Happens
The serviceOf
method was deprecated in Gradle 8.x and fully removed in newer versions. The React Native plugin version shipped with 0.73.9 (0.75.3) still contained references to this removed API, causing compatibility issues with Gradle 8.10.
The patch implements modern configuration APIs (forUseAtConfigurationTime()
and direct object instantiation) that are compatible with current Gradle versions. This solution is safe until the official Gradle plugin update ships with React Native.