最近,使用0.68.0版本的React Native构建项目后,在运行项目的时候,突然给报了一个Cannot access 'serviceOf': it is internal in 'org.gradle.configurationcache.extensions' 的错误,报错日志如下。

e: /Users/avishay/project/node_modules/react-native/packages/react-native-gradle-plugin/build.gradle.kts:9:49: Cannot access 'serviceOf': it is internal in 'org.gradle.configurationcache.extensions'
e: /Users/avishay/project/node_modules/react-native/packages/react-native-gradle-plugin/build.gradle.kts:41:7: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
internal inline fun <reified T : Any> TaskInternal.serviceOf(): TypeVariable(T) defined in org.gradle.configurationcache.extensions

下面是使用npx react-native info

info Fetching system and libraries information...
System:
    OS: macOS 11.6
    CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
    Memory: 199.65 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.0.1 - /usr/local/bin/node
    Yarn: 1.22.1 - /usr/local/bin/yarn
    npm: 8.3.0 - ~/Projects/knox/knock/node_modules/.bin/npm
    Watchman: 2021.11.01.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.2 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.0.1, iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0
    Android SDK: Not Found
  IDEs:
    Android Studio: 2020.3 AI-203.7717.56.2031.7583922
    Xcode: 13.1/13A1030d - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.10 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2
    react-native: 0.66.4 => 1000.0.0
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

对于这个问题,我们只需要升级gradle版本即可。打开android/gradle/wrapper/gradle-wrapper.properties,然后将gradle版本修改为7.3.3版本。

-distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip

由于7.0以上版本需要Java 11的支持,所以,升级版本后需要升级下项目的Java版本。


xiangzhihong
5.9k 声望15.3k 粉丝

著有《React Native移动开发实战》1,2,3、《Kotlin入门与实战》《Weex跨平台开发实战》、《Flutter跨平台开发与实战》1,2和《Android应用开发实战》