facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.24k stars 24.34k forks source link

Build from source error: Cannot access 'serviceOf': it is internal in 'org.gradle.configurationcache.extensions' #32858

Closed Avishayy closed 2 months ago

Avishayy commented 2 years ago

Description

I've been following the Building from source guide and I can't get packages/react-native-gradle-plugin's build to work

The only errors I have when building through Android Studio are:

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

I couldn't find any documentation on the configurationcache plugin and I'm pretty inept with Gradle in general.

Version

0.66.4

Output of 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

Steps to reproduce

  1. Follow instructions from this guide https://github.com/facebook/react-native/wiki/Building-from-source
  2. Build the project

Snack, code example, screenshot, or link to a repository

No response

Avishayy commented 2 years ago

I saw that serviceOf was used for tests (which I don't need), so I removed the import and the reference and now I have other kotlin issues:

e: /Users/avishay/project/node_modules/react-native/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt: (30, 33): Unresolved reference: replaceFirstChar
e: /Users/avishay/projectnode_modules/react-native/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt: (30, 52): Unresolved reference: it
e: /Users/avishay/project/node_modules/react-native/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt: (258, 16): Unresolved reference: lowercase

I tried adding kotlin-gradle-plugin, kotlin-android-extensions and kotlin-stdlib-jdk7 dependencies to my project but to no avail.

cortinico commented 2 years ago

Thanks for giving it a try @Avishayy

The only errors I have when building through Android Studio are:

Can you share your project? If not, can you share what's the content of your android/gradle/wrapper/gradle-wrapper.properties file? It might be that you're on an older version of Gradle and you can't build from source as the version from source is newer.

I tried adding kotlin-gradle-plugin, kotlin-android-extensions and kotlin-stdlib-jdk7 dependencies to my project but to no avail.

Ideally you should not have to deal with this at all.

Avishayy commented 2 years ago

Thanks for giving it a try @Avishayy

The only errors I have when building through Android Studio are:

Can you share your project? If not, can you share what's the content of your android/gradle/wrapper/gradle-wrapper.properties file? It might be that you're on an older version of Gradle and you can't build from source as the version from source is newer. I can't, sorry. My version is 6.1.1 but Android Studio prompted me to update to update to 6.7.1 to make it work

I tried adding kotlin-gradle-plugin, kotlin-android-extensions and kotlin-stdlib-jdk7 dependencies to my project but to no avail.

Ideally you should not have to deal with this at all.

Actually, I don't want to proceed with this issue anymore as my goal was to get debugging to work. After failing to get building to work, I tried to make a minimal reproducer with rn-tester, which failed too.

Eventually, I found out I can just remove the class I want to debug from the .aar and the java implementation to my project with the same package name, which did work. (BTW, I'm really deep in the debugging hell, I understand the symptoms of my original issue and not why it differs from the previous version, I'll update soon)

pjc0247 commented 2 years ago

I was able to solve this by modifying android/gradle/wrapper/gradle-wrapper.properties

-distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
ycdaskin commented 2 years ago

I was able to solve this by modifying android/gradle/wrapper/gradle-wrapper.properties

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

But this time i have to compile with jdk 11 and it causes an error while compiling firebase crashlytics :(

taylorkline commented 2 years ago

I was able to solve this by modifying android/gradle/wrapper/gradle-wrapper.properties

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

Thanks, turns out I also forgot to apply the gradle-wrapper.properties diff from https://react-native-community.github.io/upgrade-helper/?from=0.64.1&to=0.68.2 when upgrading.

lonewolf73 commented 2 months ago

Regarding last post , I disagree the issue was resolved just changing gradle version. Because I have last gradle release (8.10) and it happens to me. So my current configuration is 👍

current Java version

openjdk 17.0.12 2024-07-16
OpenJDK Runtime Environment Temurin-17.0.12+7 (build 17.0.12+7)
OpenJDK 64-Bit Server VM Temurin-17.0.12+7 (build 17.0.12+7, mixed mode, sharing)

current Gradle version

------------------------------------------------------------
Gradle 8.10
------------------------------------------------------------

Build time:    2024-08-14 11:07:45 UTC
Revision:      fef2edbed8af1022cefaf44d4c0514c5f89d7b78

Kotlin:        1.9.24
Groovy:        3.0.22
Ant:           Apache Ant(TM) version 1.10.14 compiled on August 16 2023
Launcher JVM:  17.0.12 (Eclipse Adoptium 17.0.12+7)
Daemon JVM:    C:\Program Files\Eclipse Adoptium\jre-17.0.12.7-hotspot (no JDK specified, using current Java home)
OS:            Windows 10 10.0 amd64

current npm version and dependencies

+-- @emotion/react@11.13.3
+-- @emotion/styled@11.13.0
+-- @react-native/metro-config@0.75.2
+-- @react-navigation/material-top-tabs@6.6.14
+-- @react-navigation/native@6.1.18
+-- @tsconfig/react-native@3.0.5
+-- @types/jest@28.1.8
+-- @types/react-test-renderer@18.3.0
+-- @types/react@18.3.4
+-- i18next@23.14.0
+-- metro-config@0.80.10
+-- micromatch@4.0.8
+-- npm@10.8.3
+-- react-i18next@14.1.3
+-- react-native-gesture-handler@2.18.1
+-- react-native-gradle-plugin@0.71.19
+-- react-native-pager-view@6.4.1
+-- react-native-reanimated@3.15.1
+-- react-native-safe-area-context@4.10.9
+-- react-native-screens@3.34.0
+-- react-native-svg-transformer@1.5.0
+-- react-native-svg@15.6.0
+-- react-native-tab-view@3.5.2
+-- react-native@0.75.2
+-- react@18.3.1
`-- typescript@5.5.4

current android/build.gradle

buildscript {
    ext {
        buildToolsVersion = "34.0.0"
        minSdkVersion = 29
        compileSdkVersion = 34
        targetSdkVersion = 34
        ndkVersion = "26.1.10909125"
        kotlinVersion = "1.9.24"        
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:8.10")
        classpath("com.facebook.react:react-native-gradle-plugin")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
    }
}

apply plugin: "com.facebook.react.rootproject"

current android/settings.gradle

pluginManagement { includeBuild('../node_modules/@react-native/gradle-plugin') }
plugins { id("com.facebook.react.settings") }
extensions.configure(com.facebook.react.ReactSettingsExtension){ ex -> ex.autolinkLibrariesFromCommand() }
rootProject.name = 'AreaHotspot'
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) 
include ':app'
includeBuild('../node_modules/@react-native/gradle-plugin')

command run :

gradle --refresh-dependencies --full-stacktrace --debug --scan

error got :

2024-08-30T16:46:36.661+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-08-30T16:46:36.661+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build completed with 2 failures.
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 1: Task failed with an exception.
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] -----------
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Where:
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Build file 'C:\Users\.....\node_modules\@react-native\gradle-plugin\react-native-gradle-plugin\build.gradle.kts' line: 10
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong:
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Script compilation errors:
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   Line 10: import org.gradle.configurationcache.extensions.serviceOf
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]                                                            ^ Unresolved reference: serviceOf
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   Line 58:           serviceOf<ModuleRegistry>()
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]                      ^ Unresolved reference: serviceOf
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 2 errors
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Try:
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > Run with --scan to get full insights.
2024-08-30T16:46:36.663+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > Get more help at https://help.gradle.org.
2024-08-30T16:46:36.664+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-08-30T16:46:36.664+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Exception is:
2024-08-30T16:46:36.664+0200 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] ScriptCompilationException(scriptCompilationErrors=[ScriptCompilationError(message=Unresolved reference: serviceOf, location=C:\Gradle\.tmp\gradle-kotlin-dsl-1957617396773519824.tmp\build.gradle.kts (10:49)), ScriptCompilationError(message=Unresolved reference: serviceOf, location=C:\Gradle\.tmp\gradle-kotlin-dsl-1957617396773519824.tmp\build.gradle.kts (58:11))])

following this issue https://github.com/facebook/react-native/issues/37953 , I even tried to install npm -i react-native-gradle-plugin because of that classpath("com.facebook.react:react-native-gradle-plugin") in android/build.gradle , but it didn't worked as well and even that one is deprecated.

By the way , Gradle 8.10 doesn't have anymore --clean option.

lonewolf73 commented 2 months ago

UPDATE: after some more deep investigation at Gradle sources I figured probably that

import org.gradle.configurationcache.extensions.serviceOf

in ..\node_modules\@react-native\gradle-plugin\react-native-gradle-plugin\build.gradle.kts and also in ..\node_modules\@react-native\gradle-plugin\settings-plugin are referenced to old versions of Gradle....can't say which version.

So I changed that import with :

import org.gradle.internal.extensions.core.serviceOf

as I found in my Gradle 8.10 version. So I re-executed the command :

gradle --refresh-dependencies --full-stacktrace --debug --scan

and seems the command is still working, no more errors about serviceOf are shown.... So probably that import inside build.gradle.kts inside module @react-native\gradle-plugin has to be constructed depending from the version of Gradle present on system....

cipolleschi commented 2 months ago

cc. @cortinico

david-cahill commented 2 months ago

Also seeing this issue with the latest React Native 0.75.2 and Gradle 8.8

cortinico commented 2 months ago

Fix is here: https://github.com/facebook/react-native/pull/46367 The whole serviceOf is not necessary anymore, I've already removed it from main.

shubhamguptadream11 commented 2 months ago

@cortinico I am also getting the same error while upgrading sample app from 0.75.2 to 0.76.0-rc.0?

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* Where:
Build file '/Users/shubhamgupta/D11Repo/rn-benchmarking/Benchamrking/node_modules/@react-native/gradle-plugin/react-native-gradle-plugin/build.gradle.kts' line: 10

* What went wrong:
Script compilation errors:

  Line 10: import org.gradle.configurationcache.extensions.serviceOf
                                                           ^ Unresolved reference: serviceOf

  Line 58:           serviceOf<ModuleRegistry>()
                     ^ Unresolved reference: serviceOf

2 errors

* 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.
==============================================================================

2: Task failed with an exception.
-----------
* Where:
Build file '/Users/shubhamgupta/D11Repo/rn-benchmarking/Benchamrking/node_modules/@react-native/gradle-plugin/react-native-gradle-plugin/build.gradle.kts' line: 10

* What went wrong:
Script compilation errors:

  Line 10: import org.gradle.configurationcache.extensions.serviceOf
                                                           ^ Unresolved reference: serviceOf

  Line 58:           serviceOf<ModuleRegistry>()
                     ^ Unresolved reference: serviceOf

2 errors

I followed changes suggested by upgrade helper

Gradle version

------------------------------------------------------------
Gradle 8.10.1
------------------------------------------------------------

Build time:    2024-09-09 07:42:56 UTC
Revision:      8716158d3ec8c59e38f87a67f1f311f297b79576

Kotlin:        1.9.24
Groovy:        3.0.22
Ant:           Apache Ant(TM) version 1.10.14 compiled on August 16 2023
Launcher JVM:  21.0.1 (Oracle Corporation 21.0.1+12-29)
Daemon JVM:    /Users/shubhamgupta/Library/Java/JavaVirtualMachines/openjdk-21.0.1/Contents/Home (no JDK specified, using current Java home)
OS:            Mac OS X 14.6.1 aarch64
shubhamguptadream11 commented 2 months ago

I am able to resolve this by removing this package: @types/react-native": "^0.73.0. It's deprecated as well.

cortinico commented 2 months ago

Closing as this will be fixed in 0.75.3 and 0.76.x

shyvum commented 1 month ago

@cortinico is it possible to port it back to 0.73.x, we are currently on 0.73.2 and it's quite hard to upgrade to 0.75.x right now.

this specifically is blocking us to upgrade both Gradle & AGP to latest on our production project.

cortinico commented 1 month ago

@cortinico is it possible to port it back to 0.73.x, we are currently on 0.73.2 and it's quite hard to upgrade to 0.75.x right now.

You can request pick requests at: https://github.com/reactwg/react-native-releases/

kenobi-io commented 4 weeks ago

I have same issue . Help for me downgrade java from 23 to 17 sudo ls -sf /usr/lib/jvm/java-17-openjdk-amd64/bin/java /usr/bin/java I have 8 gradle and

rm -rf android node_module package-lock.json
npm i
npm run android

for more info looks in your https://expo.dev/accounts/... in page -> builds. Like Screenshot 2024-10-15 132354

ko-devHong commented 1 week ago

@cortinico is it possible to port it back to 0.73.x, we are currently on 0.73.2 and it's quite hard to upgrade to 0.75.x right now.

this specifically is blocking us to upgrade both Gradle & AGP to latest on our production project.

you can use patch-package