Last active
January 19, 2026 10:35
-
-
Save kibotu/d85392ab5775a52df9d753f0952e62b4 to your computer and use it in GitHub Desktop.
Print Java Version for Kotlin and Java Compiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Gradle task that prints comprehensive Java and Kotlin version information for Android modules. | |
| * | |
| * This task displays: | |
| * - Java runtime version, vendor, and home directory | |
| * - Gradle version | |
| * - Java source and target compatibility settings | |
| * - Kotlin JVM target version (if configured) | |
| * - Kotlin plugin version | |
| * | |
| * Usage: | |
| * - Run for specific module: ./gradlew :moduleName:printJavaVersionInfo | |
| * - Run for all modules: ./gradlew printJavaVersionInfo | |
| * | |
| * HINT: https://gist.github.com/kibotu/d85392ab5775a52df9d753f0952e62b4 | |
| */ | |
| abstract class PrintJavaVersionInfoTask extends DefaultTask { | |
| @Input | |
| abstract Property<String> getModuleName() | |
| @Input | |
| abstract Property<String> getGradleVersion() | |
| @Input | |
| abstract Property<String> getSourceCompatibility() | |
| @Input | |
| abstract Property<String> getTargetCompatibility() | |
| @Input | |
| abstract Property<String> getKotlinJvmTarget() | |
| @Input | |
| abstract Property<String> getKotlinVersion() | |
| @TaskAction | |
| void printVersions() { | |
| println "================================" | |
| println "Module: ${moduleName.get()}" | |
| println "================================" | |
| println "Java Version (Runtime): ${System.getProperty('java.version')}" | |
| println "Java Vendor: ${System.getProperty('java.vendor')}" | |
| println "Java Home: ${System.getProperty('java.home')}" | |
| println "" | |
| println "Gradle Version: ${gradleVersion.get()}" | |
| println "" | |
| println "Java Source Compatibility: ${sourceCompatibility.get()}" | |
| println "Java Target Compatibility: ${targetCompatibility.get()}" | |
| def jvmTarget = kotlinJvmTarget.get() | |
| if (jvmTarget != "N/A") { | |
| println "Kotlin JVM Target: ${jvmTarget}" | |
| } | |
| println "" | |
| println "Kotlin Version: ${kotlinVersion.get()}" | |
| println "================================" | |
| } | |
| } | |
| /** | |
| * Automatically registers the printJavaVersionInfo task for all Android application | |
| * and library modules in the project. | |
| */ | |
| subprojects { | |
| pluginManager.withPlugin('com.android.application') { | |
| createPrintJavaVersionTask(project) | |
| } | |
| pluginManager.withPlugin('com.android.library') { | |
| createPrintJavaVersionTask(project) | |
| } | |
| } | |
| /** | |
| * Creates and configures the printJavaVersionInfo task for the specified Android project. | |
| * | |
| * All Android-specific properties are wrapped in providers to defer evaluation until | |
| * task execution time, avoiding Gradle configuration finalization issues. | |
| * | |
| * @param targetProject The Android application or library project to create the task for | |
| */ | |
| void createPrintJavaVersionTask(Project targetProject) { | |
| targetProject.tasks.register('printJavaVersionInfo', PrintJavaVersionInfoTask) { | |
| group = "check24" | |
| description = "Prints Java and Kotlin version information for ${targetProject.name}" | |
| moduleName.set(targetProject.name) | |
| gradleVersion.set(gradle.gradleVersion) | |
| def android = targetProject.extensions.findByName('android') | |
| sourceCompatibility.set(targetProject.provider { | |
| android?.compileOptions?.sourceCompatibility?.toString() ?: "N/A" | |
| }) | |
| targetCompatibility.set(targetProject.provider { | |
| android?.compileOptions?.targetCompatibility?.toString() ?: "N/A" | |
| }) | |
| // For AGP 9.0+, get JVM target from Kotlin compilation task | |
| kotlinJvmTarget.set(targetProject.provider { | |
| try { | |
| // Try to find any Kotlin compilation task | |
| def kotlinTask = targetProject.tasks.findByName('compileDebugKotlin') | |
| if (kotlinTask == null) { | |
| kotlinTask = targetProject.tasks.findByName('compileReleaseKotlin') | |
| } | |
| if (kotlinTask == null) { | |
| kotlinTask = targetProject.tasks.findByName('compileKotlin') | |
| } | |
| if (kotlinTask != null) { | |
| def jvmTarget = kotlinTask.compilerOptions?.jvmTarget?.orNull | |
| return jvmTarget?.target ?: "N/A" | |
| } | |
| // Fallback: try to get from kotlin extension | |
| def kotlinExt = targetProject.extensions.findByName('kotlin') | |
| if (kotlinExt != null) { | |
| def toolchain = kotlinExt.jvmToolchain?.languageVersion?.orNull | |
| if (toolchain != null) { | |
| return toolchain.asInt().toString() | |
| } | |
| } | |
| return "N/A" | |
| } catch (Exception e) { | |
| return "N/A" | |
| } | |
| }) | |
| // Get Kotlin version from AGP 9.0's built-in Kotlin or external plugin | |
| kotlinVersion.set(targetProject.provider { | |
| try { | |
| // First, try to find the external Kotlin plugin (pre-AGP 9.0) | |
| def kotlinPlugin = targetProject.plugins.findPlugin('org.jetbrains.kotlin.android') | |
| if (kotlinPlugin != null) { | |
| def version = kotlinPlugin.class.package.implementationVersion | |
| if (version != null) { | |
| return version | |
| } | |
| } | |
| // For AGP 9.0+, get version from classpath dependencies | |
| def kotlinDep = targetProject.buildscript.configurations.getByName('classpath') | |
| .resolvedConfiguration | |
| .resolvedArtifacts | |
| .find { | |
| it.moduleVersion.id.group == 'org.jetbrains.kotlin' && | |
| it.moduleVersion.id.name == 'kotlin-gradle-plugin' | |
| } | |
| if (kotlinDep != null) { | |
| return kotlinDep.moduleVersion.id.version | |
| } | |
| // Fallback: try to get from any Kotlin compilation task | |
| def kotlinTask = targetProject.tasks.findByName('compileDebugKotlin') | |
| if (kotlinTask == null) { | |
| kotlinTask = targetProject.tasks.findByName('compileReleaseKotlin') | |
| } | |
| if (kotlinTask != null) { | |
| return kotlinTask.class.package.implementationVersion ?: "Built-in (AGP 9.0+)" | |
| } | |
| return "Not applied" | |
| } catch (Exception e) { | |
| return "Unknown (${e.message})" | |
| } | |
| }) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
project
build.gradleoutputs