Fix react-native-tts Android Build Error: Could Not Find Method jcenter()
Fixing react-native-tts Android Build Error: Could Not Find Method jcenter()
While running a React Native project on Android, I encountered the following build error:
A problem occurred evaluating project ':react-native-tts'. > Could not find method jcenter() for arguments [] on repository container of type
The build failed when running:
./gradlew app:installDebug
After checking the stack trace, the error clearly pointed to the following file inside
node_modules:
node_modules/react-native-tts/android/build.gradle
Why This Error Happens
jcenter() has been deprecated and removed in newer versions of Gradle. However, some older React Native libraries still reference it.
In this case, the library react-native-tts was still using
jcenter() in its Android Gradle configuration, which caused the build
to fail immediately.
The Tempting but Wrong Fix
You can temporarily fix the issue by directly editing the file inside
node_modules — but this solution does not last.
Any of the following actions will undo your change:
- Running
npm install - Running
yarn install - Deleting
node_modules
So we need a way to patch the dependency properly and permanently.
The Correct Solution: patch-package
patch-package
allows you to safely patch dependencies in node_modules and automatically
re-apply those patches after every install.
This makes it ideal for fixing issues in third-party libraries like react-native-tts until they are officially updated.
Step-by-Step: Patching react-native-tts
1. Install patch-package
npm install patch-package --save-dev
or with Yarn:
yarn add patch-package --dev
2. Edit react-native-tts Gradle File
Open the following file:
node_modules/react-native-tts/android/build.gradle
Locate the buildscript repositories block and change it from:
buildscript {
repositories {
jcenter()
}
}
to:
buildscript {
repositories {
google()
mavenCentral()
}
}
This removes the deprecated repository and replaces it with supported ones.
3. Generate the Patch
From the root of your project, run:
npx patch-package react-native-tts
This will generate a patch file similar to:
patches/react-native-tts+X.Y.Z.patch
4. Apply the Patch Automatically
Update your package.json to include:
{
"scripts": {
"postinstall": "patch-package"
}
}
This ensures the patch is re-applied every time dependencies are installed.
5. Commit the Patch
Commit the patches/ directory to version control.
Do not commit node_modules.
Verify the Fix
To confirm everything works as expected:
- Delete
node_modules - Run
npm installoryarn install - Run the Android build again
The build should now succeed without the jcenter() error.
Conclusion
As Gradle continues to remove deprecated features, issues like this will appear more frequently in older React Native libraries.
Until react-native-tts updates its Android configuration, using patch-package is a clean, reliable, and team-friendly workaround.
Hopefully this post saves someone else a good chunk of debugging time. 🚀
❤️ Support This Blog
If this post helped you, you can support my writing with a small donation. Thank you for reading.
Comments
Post a Comment