OpenCV Android module imported from SDK does not respect project's JVM settings

Hi,

I’m currently undertaking a project that involves using OpenCV and Android to make some sort of navigation app for indoors. I have managed to import the OpenCV module into my project, followed the instructions from opencv/build.gradle to make sure my application compiles, and I changed the targetSdkVersion and minSdkVersions to 17 in both app/build.gradle and opencv/build.gradle for consistency. The app did compile before.

As my project is stored on a Git repository and I’m using Git Actions to test my app, I found out that I had to create a debug variant of OpenCV to be included with the debug version of my application’s APK otherwise the workflow would fail. So I added the following line to the ‘debug’ block of ‘buildTypes’ in opencv/build.gradle:

consumerProguardFiles("proguard-rules.pro") // To help Gradle recognise the build variant
            matchingFallbacks.add("release") // Fallback if needed

After doing this, I encountered a nasty error that I did not encounter before which prevented the app from compiling on my computer (it compiled okay on Git Actions but instrumented tests still failed):

Execution failed for task ':opencv: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

So I decided to use the toolchain option by looking up the content on this website: Setting JDK level in Android Gradle Builds (which has sadly not worked at all), by adding the following to my app/build.gradle and opencv/build.gradle :

kotlin {
    jvmToolchain(17)
}

android {
    .....
    
    kotlinOptions {
        // This is needed for the task "compileDebugJavaWithJavac" to use JVM 17 and not 1.8
        jvmTarget = "17"
    }
}

When I clicked on Sync Now --> Clean Project --> Rebuild Project' this issue frustratingly persists. I didn’t use the Kotlin documentation about JVM toolchains as it is unclear with regards to the scope of where the toolchain setting should be included in a project’s build.gradle, so I’m stuck on how to proceed! I checked my other settings such as JAVA_HOME, the JDK used by my Android Studio project (which is pointing to my JDK 17 installation) and they’re all fine. But when I check my project’s structure I see this:

The ‘SourceCompatibility’ and ‘TargetCompability’ settings only show Java versions upto Java 11, when it should be recognizing 17 as the latest version.

Any ideas or suggestions I can use to fix this issue?

System Information:
Windows 11
Java SDK: 17.0.12
Javac version: 17.0.12
OpenCV version 4.10.0
Android Gradle Plugin: 8.5.2
Gradle version: 8.7