React Native - Gradle - Andriod -JAVA

So i have been working on a project that is an app to from react native to collect the data from an NPK sensor from bluetooth and store that data in the sqlite in the app i just eject the react native expo out since it cannot use bluetooth and sqlite but it seem likes there is a problem related to the version of java my gradle knows and the version of java i am currently using (not sure about the cause of the problem) but here is the error massages

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\User\Desktop\react_native\FirstWork\android\app\build.gradle' line: 175

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not open cp_dsl generic class cache for script 'C:\Users\User\Desktop\react_native\FirstWork\node_modules\@react-native-community\cli-platform-android\native_modules.gradle' (C:\Users\User\.gradle\caches\8.3\scripts\eoxe7qyipog4lrxqwlgckzp03).
   > BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 66

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

BUILD FAILED in 12s
info Run CLI with --verbose flag for more details.

i need help , if you know any info related to this problem or how to fix the problem so i can run the app on an android emulator please let me know.

You try to run a Gradle 8.3 build using Java 22, this is not supported.
See here for the compatibility information: Compatibility Matrix

1 Like

i checked in terminal and it said i have been using java 22 and gradle 8.8 so


is there a way to change the version of my gradle or installing a new java is faster?

  • is changing the java version to java 20 is better faster and easier?
  • how to update my gradle caches is it better faster and easier?


I am not sure if this is still the same problem but how do i solve it?

What gradle -v returns is absolutely irrelevant.
Usually you should not even have any Gradle version installed locally at all anyway.
Each project should have the 4 Gradle wrapper files included which defines and manages the Gradle version that is used.
The Gradle version is defined in gradle/wrapper/gradle-wrapper.properties by default

The error from your last message is something different. You try to use targetCompatibility which seems to not exist, probably because you do it on a project that does not apply any javaesque plugin. But it would anyway better to use JVM toolchains instead.

hey bro i have been encountering some new error again the build was succeed once which i dont know why but i tried changing the internet and turn off firewall


and if i do gradle --refresh-dependencies

PS C:\Users\User\Desktop\react_native\FirstWork> gradle --refresh-dependencies
Starting a Gradle Daemon, 2 incompatible and 1 stopped Daemons could not be reused, use --status for details

FAILURE: Build failed with an exception.

* What went wrong:
java.io.UncheckedIOException: Could not move temporary workspace (C:\Users\User\Desktop\react_native\FirstWork\.gradle\8.8\dependencies-accessors\d7f838bb16dc243bdd05bfa8f75ef956f6209f3c-1b1b456e-42ec-47db-861b-526c78d8685d) to immutable location (C:\Users\User\Desktop\react_native\FirstWork\.gradle\8.8\dependencies-accessors\d7f838bb16dc243bdd05bfa8f75ef956f6209f3c)
> Could not move temporary workspace (C:\Users\User\Desktop\react_native\FirstWork\.gradle\8.8\dependencies-accessors\d7f838bb16dc243bdd05bfa8f75ef956f6209f3c-1b1b456e-42ec-47db-861b-526c78d8685d) to immutable location (C:\Users\User\Desktop\react_native\FirstWork\.gradle\8.8\dependencies-accessors\d7f838bb16dc243bdd05bfa8f75ef956f6209f3c)

this appear so what do i do?

The “was not found in any …” means you try to apply the com.android.application plugin without specifying a version and without otherwise bringing the plugin to the classpath.

The “Could not move temporary workspace” is

i dont clearly understand both of the error eventhough u had explain it to me, i mean i tried chaning the

plugins {
    id 'com.android.application' 
    id 'org.jetbrains.kotlin.android' version '1.9.0'
}

to

plugins {
    id 'com.android.application' version '8.5.0' apply false (try 8.0.0 and apply false alread)
    id 'org.jetbrains.kotlin.android' version '1.9.0'
}

to try indicating the version of the com.android.application but it still say that
“was not found in any …”
can you please check my build gradle in android level

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.0' // Use the appropriate version for your setup
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10' // Use the appropriate version for your setup
        classpath 'com.facebook.react:react-native-gradle-plugin'
    }
}

plugins {
    id 'com.android.application' version '8.5.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.9.0'
}

android {
    compileSdkVersion 31
    defaultConfig {
        applicationId 'com.anonymous.FirstWork'
        minSdkVersion 21
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
    }
    // Java Toolchain Configuration
    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(22)
        }
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}
dependencies {
    implementation("com.facebook.react:react-android")
    // Other dependencies

    def isGifEnabled = (findProperty('expo.gif.enabled') ?: "") == "true";
    def isWebpEnabled = (findProperty('expo.webp.enabled') ?: "") == "true";
    def isWebpAnimatedEnabled = (findProperty('expo.webp.animated') ?: "") == "true";

    if (isGifEnabled) {
        implementation("com.facebook.fresco:animated-gif:${reactAndroidLibs.versions.fresco.get()}")
    }

    if (isWebpEnabled) {
        implementation("com.facebook.fresco:webpsupport:${reactAndroidLibs.versions.fresco.get()}")
        if (isWebpAnimatedEnabled) {
            implementation("com.facebook.fresco:animated-webp:${reactAndroidLibs.versions.fresco.get()}")
        }
    }

    implementation("com.facebook.react:flipper-integration")

    if (hermesEnabled.toBoolean()) {
        implementation("com.facebook.react:hermes-android")
    } else {
        implementation jscFlavor
    }
}

apply from: new File(["node", "--print", "require.resolve('@react-native-community/cli-platform-android/package.json', { paths: [require.resolve('react-native/package.json')] })"].execute(null, rootDir).text.trim(), "../native_modules.gradle");
applyNativeModulesAppBuildGradle(project)

what are the things wrong and why are they wrong?

If you change id 'com.android.application' to id 'com.android.application' version '8.5.0' apply false that is not particularly helpful as you then do no longer apply the plugin but just add it to the classpath.

what are the things wrong and why are they wrong?

Actually many things if you ask that way.
Here some of the points:

  • More a personal recommendation, but switch to Kotlin DSL, by now it is the default DSL, you immediately get type-safe build scripts, actually helpful error messages if you mess up the syntax, and amazingly better IDE support if you use a good IDE like IntelliJ IDEA or Android Studio.
  • Having in the root build script both, classpath 'com.android.tools.build:gradle:8.0.0' and id 'com.android.application' version '8.5.0' apply false is bad. Both add the AGP to the classpath so if it were the same version it would be redundant, as they are different versions it even is an error that would fail if the resoultion would have worked
  • I guess applying org.jetbrains.kotlin.android in the build script you showed will fail as you do not apply the Android plugin (apply false)
  • Don’t put the java { ... } block inside the android { ... } block, it is a sibling and having it inside is just visual clutter and confusing
  • allprojects { ... } as well as any other way to do cross-project configuration like subprojects { ... }, project("...") { ... }, and so on are highly discouraged bad practice and work against more sophisticated Gradle features and optimzations, if you want to centralize build logic, better consider using Convention Plugins, for example in buildSrc or an included build, for example implemented as Precompiled script plugins.
  • Do not use legacy script plugins (the things you use using apply from) they have many quirks and problems and should be avoided

Regarding the error, I guess that you did not define google() as plugin repository in your settings script and thus it cannot resolve from the plugin id with version in the plugins { ... } block to the plugin. Either use the proper way, defining google() as plugin repository and using the plugins { ... } block, or if you prefer using the discouraged legacy way, remove the version from inside the plugins { ... } block and keep the classpath stuff.

1 Like

i am not sure that is there a way to change from using groovy to kotlin without a mess or not so i do prefer to stay with the groovy for now to fix the problem i can (if i would stuck to some problem in the future please tell me to change)

so i tried solving the error base on my understanding from your reply as

  • removing all the plugin stuff and remain the classpath stuff
  • using maven to properly define google
  • move the java() out
    *** but i dont understand what to do with allproject() things

buildscript {
repositories {
google() // For Gradle 4.0+
maven { url ‘https://maven.google.com’} // For Gradle < 4.0

but now i receive this error
(the error is too long i will put it in the google doc link)

the top of the error look like this
FAILURE: Build failed with an exception.

  • What went wrong:
    A problem occurred configuring root project ‘FirstWork’.

Could not resolve all dependencies for configuration ‘:classpath’.
Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.22.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.9.22/kotlin-gradle-plugin-1.9.22.pom
- https://maven.google.com/org/jetbrains/kotlin/kotlin-gradle-plugin/1.9.22/kotlin-gradle-plugin-1.9.22.pom
Required by:
project :
Could not find org.jetbrains.kotlin:kotlin-stdlib:1.9.22.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.9.22/kotlin-stdlib-1.9.22.pom
- https://maven.google.com/org/jetbrains/kotlin/kotlin-stdlib/1.9.22/kotlin-stdlib-1.9.22.pom
Required by:
project : > project :gradle-plugin
Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.22.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.9.22/kotlin-gradle-plugin-1.9.22.pom

You now only have google() as repository (and a mirror of it where even your comment says to not use it as you are not on Gradle <4 where simply google() did not exist).
But org.jetbrains.kotlin:kotlin-gradle-plugin is not available in google(), but in mavenCentral().

but i dont understand what to do with allproject() things

Replace them by using convention plugins as documented in the links I provided. :slight_smile: