I encountered the following compilation error in Android Studio of one of my Android project about current target JVM compatibility (or incompatibility), as shown in the screenshot message listing below:
Execution failed for task ':app:compileDebugKotlin'. > 'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version. Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain * 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.
I managed to fix the problem by ensuring the source and target are compatible, i.e. having the same Java version.
To fix the issue:
- In Android Studio, select File | Project Structure.
The Project Structure dialog box appears. - Click Gradle Settings.
The Gradle dialog box appears. - In the Use Gradle from combo box, choose 'gradle-wrapper.properties' file.
- In the Gradle JDK combo box, choose jbr-17 JetBrains Runtime version 17.
- Click OK to close all dialog boxes.
- In Android Studio, open the app's build.gradle file in the editor.
- Within the android construct, add in the compileOptions as shown in the listing below.
android { compileSdk 33 // ...etc... compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } }
From this point on, compiling the app should not throw upany JVM target compatibility errors.