Yarn Monorepo升级RN至0.72.3新架构遇datetimepicker编译失败
升级React Native至v0.72.3并启用新架构后Android编译错误解决
问题背景
- Yarn Monorepo项目,从React Native v0.69.5升级到v0.72.3并启用新架构
- 执行
yarn react-native run-android时,react-native-community_datetimepicker模块编译失败,出现16个Java编译错误
编译错误日志
> Task :react-native-community_datetimepicker:compileDebugJavaWithJavac FAILED Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See https://docs.gradle.org/7.5/userguide/command_line_interface.html#sec:command_line_warnings Execution optimizations have been disabled for 10 invalid unit(s) of work during this build to ensure correctness. Please consult deprecation warnings for more details. 236 actionable tasks: 227 executed, 9 up-to-date info 💡 Tip: Make sure that you have set up your development environment correctly, by running react-native doctor. To read more about doctor command visit: https://github.com/react-native-community/cli/blob/main/packages/cli-doctor/README.md#doctor No modules to process in combine-js-to-schema-cli. If this is unexpected, please check if you set up your NativeComponent correctly. See combine-js-to-schema.js for how codegen finds modules. Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. No modules to process in combine-js-to-schema-cli. If this is unexpected, please check if you set up your NativeComponent correctly. See combine-js-to-schema.js for how codegen finds modules. No modules to process in combine-js-to-schema-cli. If this is unexpected, please check if you set up your NativeComponent correctly. See combine-js-to-schema.js for how codegen finds modules. No modules to process in combine-js-to-schema-cli. If this is unexpected, please check if you set up your NativeComponent correctly. See combine-js-to-schema.js for how codegen finds modules. Note: C:\Users\thiago_gomes\Documents\OmniJs\node_modules\lottie-react-native\src\android\src\main\java\com\airbnb\android\react\lottie\LottieAnimationViewManager.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: C:\Users\thiago_gomes\Documents\OmniJs\node_modules\lottie-react-native\src\android\src\main\java\com\airbnb\android\react\lottie\LottieAnimationViewManager.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Note: C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-clipboard\clipboard\android\src\main\java\com\reactnativecommunity\clipboard\ClipboardModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\checkbox\android\src\main\java\com\reactnativecommunity\checkbox\ReactCheckBoxEvent.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\DatePickerModule.java:35: error: cannot find symbol public class DatePickerModule extends NativeModuleDatePickerSpec { ^ symbol: class NativeModuleDatePickerSpec C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\TimePickerModule.java:33: error: cannot find symbol public class TimePickerModule extends NativeModuleTimePickerSpec { ^ symbol: class NativeModuleTimePickerSpec C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\DatePickerModule.java:44: error: method does not override or implement a method from a supertype @Override ^ C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\DatePickerModule.java:62: error: cannot find symbol if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) { ^ symbol: method getReactApplicationContext() location: class DatePickerModule.DatePickerDialogListener C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\DatePickerModule.java:96: error: cannot find symbol if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) { ^ symbol: method getReactApplicationContext() location: class DatePickerModule.DatePickerDialogListener C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\DatePickerModule.java:106: error: cannot find symbol if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) { ^ symbol: method getReactApplicationContext() location: class DatePickerModule.DatePickerDialogListener C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\DatePickerModule.java:117: error: cannot find symbol FragmentActivity activity = (FragmentActivity) getCurrentActivity(); ^ symbol: method getCurrentActivity() location: class DatePickerModule C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\DatePickerModule.java:150: error: cannot find symbol FragmentActivity activity = (FragmentActivity) getCurrentActivity(); ^ symbol: method getCurrentActivity() location: class DatePickerModule C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\RNDateTimePickerPackage.java:20: error: incompatible types: DatePickerModule cannot be converted to NativeModule return new DatePickerModule(reactContext); ^ C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\RNDateTimePickerPackage.java:22: error: incompatible types: TimePickerModule cannot be converted to NativeModule return new TimePickerModule(reactContext); ^ C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\TimePickerModule.java:43: error: method does not override or implement a method from a supertype @Override ^ C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\TimePickerModule.java:58: error: cannot find symbol if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) { ^ symbol: method getReactApplicationContext() location: class TimePickerModule.TimePickerDialogListener C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\TimePickerModule.java:70: error: cannot find symbol if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) { ^ symbol: method getReactApplicationContext() location: class TimePickerModule.TimePickerDialogListener C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\TimePickerModule.java:80: error: cannot find symbol if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) { ^ symbol: method getReactApplicationContext() location: class TimePickerModule.TimePickerDialogListener C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\TimePickerModule.java:91: error: cannot find symbol FragmentActivity activity = (FragmentActivity) getCurrentActivity(); ^ symbol: method getCurrentActivity() location: class TimePickerModule C:\Users\thiago_gomes\Documents\OmniJs\node_modules\@react-native-community\datetimepicker\android\src\main\java\com\reactcommunity\rndatetimepicker\TimePickerModule.java:97: error: cannot find symbol FragmentActivity activity = (FragmentActivity) getCurrentActivity(); ^ symbol: method getCurrentActivity() location: class TimePickerModule Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. 16 errors FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':react-native-community_datetimepicker: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
当前Android根build.gradle配置
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { buildToolsVersion = "33.0.0" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33 // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP. ndkVersion = "23.1.7779620" } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:7.3.1") classpath("com.facebook.react:react-native-gradle-plugin") classpath("com.google.gms:google-services:4.3.4") } }
解决步骤
1. 升级datetimepicker到兼容新架构的版本
错误核心原因是旧版本的@react-native-community/datetimepicker未适配React Native 0.72+的新架构Codegen机制,导致找不到自动生成的NativeModuleDatePickerSpec等类。需要升级到v7.2.0及以上版本:
yarn upgrade @react-native-community/datetimepicker@latest
或指定兼容版本:
yarn add @react-native-community/datetimepicker@7.2.0
2. 清理缓存并重新生成Codegen产物
执行以下命令清理项目缓存,确保Codegen正确生成所需类文件:
yarn clean yarn install cd android && ./gradlew clean
3. 确认新架构配置正确
检查android/gradle.properties中是否启用新架构:
newArchEnabled=true
同时确保react-native-codegen依赖版本与React Native v0.72.3一致(Monorepo环境下需确保依赖版本对齐)。
4. 升级其他兼容问题依赖
日志中其他第三方库(如lottie-react-native、clipboard、checkbox)存在API废弃警告,建议同步升级到适配RN 0.72的版本,避免后续潜在问题:
yarn upgrade lottie-react-native @react-native-clipboard/clipboard @react-native-community/checkbox
5. 对齐React Native Gradle插件版本
确保buildscript.dependencies中的react-native-gradle-plugin版本与RN版本完全一致:
classpath("com.facebook.react:react-native-gradle-plugin:0.72.3")
内容的提问来源于stack exchange,提问作者Thiago Lino Gomes
相关产品推荐
相关产品推荐

