zeshuaro / google_api_headers

A Flutter plugin for getting the headers required to call Google APIs with an app restricted API key
https://pub.dev/packages/google_api_headers
MIT License
10 stars 24 forks source link

[BUG] ANDROID : The error occurred due to a mismatch in JVM target versions: compileDebugJavaWithJavac is set to 1.8, while compileDebugKotlin is set to 17 #476

Closed LazreqYa closed 2 months ago

LazreqYa commented 2 months ago

Steps to Reproduce

Switch to this version

`------------------------------------------------------------ Gradle 8.9

Build time: 2024-07-11 14:37:41 UTC Revision: d536ef36a19186ccc596d8817123e5445f30fef8

Kotlin: 1.9.23 Groovy: 3.0.21 Ant: Apache Ant(TM) version 1.10.13 compiled on January 4 2023 Launcher JVM: 22.0.1 (Oracle Corporation 22.0.1+8-16) Daemon JVM: /Library/Java/JavaVirtualMachines/jdk-22.jdk/Contents/Home (no JDK specified, using current Java home) OS: Mac OS X 14.6.1 aarch64`

using Android Studio Gradle JDK 17

Expected behaviour

Want to run my android app with flutter run after a long time. The package works perfectly on ios but on android i can't compile.

Actual behaviour

Actually every package are compiling but the google_api_header because of this :

* What went wrong:
Execution failed for task ':google_api_headers:compileDebugKotlin'.
> Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'compileDebugKotlin' (17).

Flutter doctor output

[✓] Flutter (Channel stable, 3.24.1, on macOS 14.6.1 23G93 darwin-arm64, locale en-FR)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.1)
[✓] VS Code (version 1.92.2)
[✓] Connected device (6 available)
[✓] Network resources

Additional Context

This package is used by the package flutter_google_place.

LazreqYa commented 2 months ago

More here: https://stackoverflow.com/staging-ground/78944634

stact commented 2 months ago

I got the same issue early this week on upgrading my app.

Found the solution by managing the upgrade of the Gradle and force the plugins to use the 17 version

Try:

Upgrade to gradle 8 distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-all.zip

Update the android application id "com.android.application" version '8.1.4' apply false

In your android build.gradle to adapt subprojects:

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    afterEvaluate {
        // check if android block is available
        if (it.hasProperty('android')) {
            if (it.android.namespace == null) {
                def manifest = new XmlSlurper().parse(file(it.android.sourceSets.main.manifest.srcFile))
                def packageName = manifest.@package.text()
                println("Setting ${packageName} as android namespace")
                android.namespace = packageName
            }
            def javaVersion = JavaVersion.VERSION_17
            android {
                def androidApiVersion = 34
                compileSdkVersion androidApiVersion
                defaultConfig {
                    targetSdkVersion androidApiVersion
                }
                compileOptions {
                    sourceCompatibility javaVersion
                    targetCompatibility javaVersion
                }
                tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
                    kotlinOptions {
                        jvmTarget = javaVersion.toString()
                    }
                }
                println("Setting java version to ${javaVersion.toString()} which is $javaVersion")
                println("Setting compileSdkVersion and targetSdkVersion to $androidApiVersion")
            }
        }

    }

}
LazreqYa commented 2 months ago

@stact thanks for the answer i just try your solution but the new XmlSlurper() is not reconize, even if i add the import groovy.util.XmlSlurper.

LazreqYa commented 2 months ago

i finally just downgrade the google_api_headers and stop using the flutter_google_place.