Skip to content

Instantly share code, notes, and snippets.

@ellet0
Last active November 6, 2023 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ellet0/93fefb39e48c40592bda3931e05fd35c to your computer and use it in GitHub Desktop.
Save ellet0/93fefb39e48c40592bda3931e05fd35c to your computer and use it in GitHub Desktop.
Fix flutter Gradle fails with native android plugins when using the latest version of gradle

Fix flutter Gradle fails with native android plugins when using the latest version of gradle

The issue is, when you use Flutter plugins that didn't update thier android gradle to 8.0 or later and if you use gradle 8.0 or above, the build will faield since gradle 8.0 require the package name as property in android block in build.gradle called namespace instead of AndroidManifest.xml so you should remove package attribute from AndroidManifest.xml and define it in build.gradle, in order to upgrade to Android gradle 8.0 plugin with gradle 8.0 or above but if you use plugins that didn't do those changes, the build will failed, also the latest version of android studio have Java jdk 17, so all new projects should target that version, but android old apis don't support that

so this code in build.gradle in the project module (top one, not the one in app module) will override all the sub projects to use Java 17 version, and define the namespace if doesn't (it will read it from AndroidManifest.xml) and also chaange the compileSdkVersion, targetSdkVersion to 33 (as I said you need to change this in the future) so with this changes, you don't have to go to the authors plugins and create new issue or pull request and wait for their permission just so you can upgrade your build system

Solution

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
    ext.kotlin_version = '1.9.20'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"

    // Solution that I created myself, you can look into more details in my github gist
    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 in build.gradle from the AndroidManifest.xml")
                android.namespace = packageName
            }

            def javaVersion = JavaVersion.VERSION_17
            println("Changes will be applied for the following packages:")
            android {
                def androidApiVersion = 34
//                compileSdkVersion androidApiVersion
                compileSdk androidApiVersion
                defaultConfig {
                    targetSdkVersion androidApiVersion
                }
                compileOptions {
                    sourceCompatibility javaVersion
                    targetCompatibility javaVersion
                }
                tasks.withType(KotlinCompile).configureEach {
                    buildscript {
                        ext.kotlin_version = kotlin_version
                    }
                    kotlinOptions {
                        jvmTarget = javaVersion.toString()
                    }
                }
                String message = "For package ${android.namespace} by update compileSdkVersion, targetSdkVersion \n to $androidApiVersion and java version to ${javaVersion.toString()}"
                println(message)
            }
        }

    }
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment