facebook / react-native

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

React-Native build ERROR: requires JDK11 or higher. Incompatible major version detected: '8' #33731

Closed Gabotron-ES closed 2 years ago

Gabotron-ES commented 2 years ago

Description

I downloaded java version 11 from here: https://www.oracle.com/java/technologies/javase/jdk11-archive-downloads.html

I check my java version with java -version command wich prints:

java version "11.0.14" 2022-01-18 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.14+8-LTS-263)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.14+8-LTS-263, mixed mode)

But I'm still getting same error, why is it not recognizing new version?

My android build gradle file

apply plugin: "com.android.application"
//Added this line
apply plugin: 'com.google.gms.google-services'
//Added this line
/*
project.ext.vectoricons = [
    iconFontNames: [ 'MaterialIcons.ttf', 'EvilIcons.ttf' ] // Name of the font files you want to copy
]
*/
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

import com.android.build.OutputFile

/**
 * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
 * and bundleReleaseJsAndAssets).
 * These basically call `react-native bundle` with the correct arguments during the Android build
 * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
 * bundle directly from the development server. Below you can see all the possible configurations
 * and their defaults. If you decide to add a configuration block, make sure to add it before the
 * `apply from: "../../node_modules/react-native/react.gradle"` line.
 *
 * project.ext.react = [
 *   // the name of the generated asset file containing your JS bundle
 *   bundleAssetName: "index.android.bundle",
 *
 *   // the entry file for bundle generation. If none specified and
 *   // "index.android.js" exists, it will be used. Otherwise "index.js" is
 *   // default. Can be overridden with ENTRY_FILE environment variable.
 *   entryFile: "index.android.js",
 *
 *   // https://reactnative.dev/docs/performance#enable-the-ram-format
 *   bundleCommand: "ram-bundle",
 *
 *   // whether to bundle JS and assets in debug mode
 *   bundleInDebug: false,
 *
 *   // whether to bundle JS and assets in release mode
 *   bundleInRelease: true,
 *
 *   // whether to bundle JS and assets in another build variant (if configured).
 *   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
 *   // The configuration property can be in the following formats
 *   //         'bundleIn${productFlavor}${buildType}'
 *   //         'bundleIn${buildType}'
 *   // bundleInFreeDebug: true,
 *   // bundleInPaidRelease: true,
 *   // bundleInBeta: true,
 *
 *   // whether to disable dev mode in custom build variants (by default only disabled in release)
 *   // for example: to disable dev mode in the staging build type (if configured)
 *   devDisabledInStaging: true,
 *   // The configuration property can be in the following formats
 *   //         'devDisabledIn${productFlavor}${buildType}'
 *   //         'devDisabledIn${buildType}'
 *
 *   // the root of your project, i.e. where "package.json" lives
 *   root: "../../",
 *
 *   // where to put the JS bundle asset in debug mode
 *   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
 *
 *   // where to put the JS bundle asset in release mode
 *   jsBundleDirRelease: "$buildDir/intermediates/assets/release",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in debug mode
 *   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in release mode
 *   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
 *
 *   // by default the gradle tasks are skipped if none of the JS files or assets change; this means
 *   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
 *   // date; if you have any other folders that you want to ignore for performance reasons (gradle
 *   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
 *   // for example, you might want to remove it from here.
 *   inputExcludes: ["android/**", "ios/**"],
 *
 *   // override which node gets called and with what additional arguments
 *   nodeExecutableAndArgs: ["node"],
 *
 *   // supply additional arguments to the packager
 *   extraPackagerArgs: []
 * ]
 */

project.ext.react = [
    enableHermes: false,  // clean and rebuild if changing
]

apply from: "../../node_modules/react-native/react.gradle"

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

/**
 * The preferred build flavor of JavaScriptCore.
 *
 * For example, to use the international variant, you can use:
 * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
 *
 * The international variant includes ICU i18n library and necessary data
 * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
 * give correct results when using with locales other than en-US.  Note that
 * this variant is about 6MiB larger per architecture than default.
 */
def jscFlavor = 'org.webkit:android-jsc:+'

/**
 * Whether to enable the Hermes VM.
 *
 * This should be set on project.ext.react and mirrored here.  If it is not set
 * on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
 * and the benefits of using Hermes will therefore be sharply reduced.
 */
def enableHermes = project.ext.react.get("enableHermes", false);

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    /*compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }*/
    compileOptions {
                   sourceCompatibility 1.8
                   targetCompatibility 1.8
               }
               dexOptions {
        javaMaxHeapSize "4g"
    }

    defaultConfig {
        applicationId "com.adoptatias"
        minSdkVersion 28
        targetSdkVersion 28
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        missingDimensionStrategy 'react-native-camera', 'general' // <--- insert this line
        missingDimensionStrategy 'store', 'play' // <--- insert this line
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }

        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules

    //implementation 'com.google.android.material:material:1.5.0' //was 1.4.0 before

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }

    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }

    //Added this line, Import the Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:26.2.0')
    // Added these lines,  the dependency for the Firebase SDK for Google Analytics
    // When using the BoM, don't specify versions in Firebase dependencies
    implementation 'com.google.firebase:firebase-analytics'
    //Added this lines
    implementation 'com.android.support:multidex:1.0.3'  //with support libraries
    //implementation 'com.google.android.material:material:1.4.0'
    //implementation 'androidx.multidex:multidex:2.0.1'  //with androidx libraries
    //Added this line
    //compile project(':tipsi-stripe')
    //implementation 'com.android.support:design:28.0.0'

}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

My package.json file:

{
  "name": "AdoptaTias",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@ptomasroos/react-native-multi-slider": "^2.2.2",
    "@react-native-async-storage/async-storage": "^1.15.5",
    "@react-native-community/geolocation": "^2.0.2",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-native-community/netinfo": "^7.1.2",
    "@react-native-firebase/app": "^10.4.0",
    "@react-native-firebase/messaging": "^10.4.1",
    "@react-native-google-signin/google-signin": "^6.0.1",
    "@react-navigation/bottom-tabs": "^5.11.3",
    "@react-navigation/native": "^5.9.0",
    "@react-navigation/stack": "^5.13.0",
    "@stream-io/flat-list-mvcp": "^0.10.1",
    "axios": "^0.21.4",
    "date-fns": "^2.28.0",
    "i18n-js": "^3.8.0",
    "laravel-echo": "^1.11.3",
    "lodash": "^4.17.21",
    "mobx": "^6.0.4",
    "mobx-persist-store": "^1.0.3",
    "mobx-react": "^7.0.5",
    "moment": "^2.29.2",
    "moment-timezone": "^0.5.34",
    "pusher-js": "^7.0.3",
    "react": "16.13.1",
    "react-native": "^0.68.1",
    "react-native-bidirectional-infinite-scroll": "^0.3.3",
    "react-native-camera": "^4.2.1",
    "react-native-custom-tabs": "^0.1.7",
    "react-native-fast-image": "^8.5.11",
    "react-native-fbsdk-next": "^4.3.0",
    "react-native-geolocation-service": "^5.1.1",
    "react-native-gesture-handler": "^1.9.0",
    "react-native-iap": "^8.0.5",
    "react-native-image-crop-picker": "^0.37.3",
    "react-native-image-picker": "^3.1.3",
    "react-native-in-app-review": "^3.3.1",
    "react-native-linear-gradient": "^2.5.6",
    "react-native-localize": "^2.0.2",
    "react-native-map-clustering": "^3.4.2",
    "react-native-maps": "^0.27.1",
    "react-native-modal": "^11.6.1",
    "react-native-reanimated": "^1.13.2",
    "react-native-safe-area-context": "^3.1.9",
    "react-native-screens": "^2.15.2",
    "react-native-shadow-2": "^6.0.3",
    "react-native-share": "^6.2.1",
    "react-native-snap-carousel": "^3.9.1",
    "react-native-vector-icons": "^7.1.0",
    "react-native-video": "^5.1.1",
    "react-navigation": "^4.4.3"
  },
  "devDependencies": {
    "@babel/core": "7.12.10",
    "@babel/runtime": "7.12.5",
    "@react-native-community/eslint-config": "1.1.0",
    "babel-jest": "25.5.1",
    "eslint": "6.8.0",
    "jest": "25.5.4",
    "metro-react-native-babel-preset": "0.59.0",
    "react-test-renderer": "16.13.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

Version

^0.68.1

Output of npx react-native info

FAILURE: Build failed with an exception.

* What went wrong:
Could not dispatch a message to the daemon.

* 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

> Configure project :app

**************************************************************************************************************

ERROR: requires JDK11 or higher.
Incompatible major version detected: '8'

**************************************************************************************************************

Steps to reproduce

cd C:\xampp\htdocs\AdoptaTias && cd android && gradlew clean && cd .. && npx react-native run-android -- --reset-cache

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

No response

marqroldan commented 2 years ago

Maybe you have a specified path in your gradle.properties?

cortinico commented 2 years ago

Are you sure you're actually using the Java 11 version you installed @Gabotron-ES? Do you have 2 versions of Java side-by-side? Are you using JEnv or any other version manager?

channeladam commented 2 years ago

I hit this problem too, but then did this to make it work:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
bozaigao commented 2 years ago
  1. install idk 11;
  2. AndroidStudio->file->ProjectStructure->click Gradle Settings->choose idk 11 ->sync; 截屏2022-05-28 20 28 12
cortinico commented 2 years ago

Closing as the solution was already provided. The original reporter @Gabotron-ES is unresponsive so I assume the problem is resolved.

DongjingZhong commented 2 years ago

I hit this problem too, but then did this to make it work:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

Hi my friend ,could you please let me know where can you find this in VScode ? I could not find it at any file ..Thank you so much !

channeladam commented 2 years ago

@DongjingZhong in android/app/build.gradle

android {
    ndkVersion rootProject.ext.ndkVersion

    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

    flavorDimensions "default"
    ...
DongjingZhong commented 2 years ago

Thank you so much Adam !

ZeeshanAhmadKhalil commented 2 years ago
  1. install idk 11;
  2. AndroidStudio->file->ProjectStructure->click Gradle Settings->choose idk 11 ->sync;
截屏2022-05-28 20 28 12

This worked for me but I had to restart VS code since I am using that to run my react-native project. Only restarting the VS Code's terminal won't work.

Hacimurad commented 1 year ago

thank you so much))I solved problem