Inconsistent JVM Target Compatibility in Android Builds
Problem Statement
When building Android projects that combine Java and Kotlin code, you may encounter an error like this:
Execution failed for task ':module:compileDebugKotlin'
> Inconsistent JVM-target compatibility detected for tasks
'compileDebugJavaWithJavac' (1.8) and 'compileDebugKotlin' (17)
This error occurs when your Java and Kotlin compilation tasks target different JVM versions. In Android projects—especially those using Flutter with Kotlin-based plugins—this often happens after updating dependencies, AGP, or Kotlin versions. The root cause is configuration inconsistency between:
- Java compilation (
sourceCompatibility
/targetCompatibility
) - Kotlin compilation (
jvmTarget
) - JDK toolchain versions
Best Solutions
1. Ensure Consistent Configuration in All Modules
First verify that every build.gradle uses matching Java/Kotlin targets:
// android/app/build.gradle
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
}
WARNING
Perform global searches for sourceCompatibility
, targetCompatibility
, and jvmTarget
to check multiple module configuration files. The error message specifies which module needs correction.
2. Use Kotlin JVM Toolchain Support (Recommended)
For projects using AGP 8.1.0+, replace manual version settings with Gradle's toolchain API:
// android/app/build.gradle
kotlin {
jvmToolchain(17)
}
// Remove existing compileOptions/kotlinOptions blocks
// Let toolchain handle consistent settings
TIP
Toolchain synchronization ensures Kotlin and Java compilation use the same JDK, eliminating version mismatches. This is the modern approach endorsed by the Kotlin team.
3. Override Third-Party Module Settings
For broken dependencies (e.g., flutter_google_places_sdk_android
in the original error), force settings in your app's build.gradle:
// android/app/build.gradle
dependencyResolutionManagement {
// ...
}
// Add this after dependencyResolutionManagement
project(':flutter_google_places_sdk_android').afterEvaluate { project ->
project.android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
}
}
4. Disable Validation (Temporary Workaround)
If immediate fixes aren't possible, suppress validation in gradle.properties:
# android/gradle.properties
kotlin.jvm.target.validation.mode=IGNORE
Use With Caution
This bypasses safety checks and may cause runtime issues. Only use as a temporary measure while implementing real solutions. Always match JVM targets in production.
Advanced Configuration
Flutter Project Setup
For Flutter-based projects, configure android/app/build.gradle:
android {
namespace "com.example.app"
compileSdkVersion 34
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
// Preferred toolchain approach
kotlin {
jvmToolchain(17)
}
}
Java/Kotlin Version Compatibility Matrix
Gradle Version | AGP Version | Max Java Version | Kotlin Version |
---|---|---|---|
8.8+ | 8.5+ | Java 23 | 2.0.21+ |
8.4+ | 8.0+ | Java 17 | 1.9.20+ |
7.6+ | 7.4+ | Java 11 | 1.8.20+ |
Check projects with:
./gradlew -v
Preventing Future Issues
Declare toolchain in settings.gradle:
kotlin// settings.gradle.kts plugins { kotlin("jvm") version "2.0.21" apply false }
Synchronize environment:
gradle// build.gradle dependencies { classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21' }
Consistency Checks:
Add to your project'sclean
task:gradletask checkJvmCompatibility { doLast { def javaVersions = [] def kotlinVersions = [] allprojects { project -> project.plugins.withType(JavaBasePlugin) { javaVersions << project.targetCompatibility } project.plugins.withType(org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper) { kotlinVersions << project.kotlin.jvmToolchain } } if (javaVersions.unique().size > 1 || kotlinVersions.unique().size > 1) { throw new GradleException("Inconsistent JVM configurations detected") } } }
Pro Tip
Set JDK versions directly in AGP 8.5+ with:
// gradle.properties
android.jdkVersion=17
This ensures build consistency across developer environments if team members use different JDK installations.