How to Automatically Add the Namespace Required by Flutter (Android Gradle 8)

The latest Flutter versions now utilize Gradle 8 or higher, which means Flutter plugins need to be updated to support these newer requirements. Since not all plugins immediately reflect the latest update demands, developers need a solution to address plugins that haven’t yet adopted these new requirements.

It’s said that you can implement a feature to automatically add the namespace required by Gradle 8 by simply adding the following code to your android/build.gradle file.

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty("android")) {
            project.android { // <- 여기가 31번째 줄 근처일 가능성이 높습니다.
                if (namespace == null) {
                    // 기본값으로 group을 사용하거나, 특정 문자열을 지정할 수 있습니다.
                    // 하지만 각 모듈의 특성에 맞는 고유한 namespace를 지정하는 것이 원칙입니다.
                    namespace project . group // 예시: 'com.example' // <- 여기가 38번째 글자 근처일 수 있습니다.
                    // 또는 namespace "com.example.defaultnamespace"
                }
            }
        }
    }
}

However, an error occurred. So, I wondered if it was because it was a Kotlin file (build.gradle.kts), and I tried converting it to Kotlin code as shown below.

subprojects {
    afterEvaluate { project: Project ->
        if (project.hasProperty("android")) {
            println("DEBUG: Project ${project.name} has android property.") // 디버깅 출력
            val androidExt =
                project.extensions.findByType(com.android.build.gradle.BaseExtension::class.java)
            if (androidExt != null) {
                println("DEBUG: Android extension found for ${project.name}.")
                if (androidExt.namespace == null) {
                    val defaultNamespace =
                        project.group?.toString() ?: "com.example.default.${project.name}"
                    println("INFO: Module ${project.name} has no namespace. Setting to '$defaultNamespace'")
                    androidExt.namespace = defaultNamespace
                }
            } else {
                println("DEBUG: Android extension NOT found for ${project.name}.")
            }
        }
    }
}

But… it still resulted in an error.

“Type mismatch: inferred type is (Project) -> Unit but Closure<(raw) Any!>! was expected”

After quite a bit of searching here and there, I finally found the solution code below. Instead of using afterEvaluate, this method directly modifies the Android extension as each project is configured.

subprojects {
    // 이 블록은 각 하위 프로젝트가 구성될 때 실행됨
    // Android 플러그인이 적용되었는지 확인 후 작업 수행
    project.plugins.withId("com.android.base") {
        // "com.android.base"는 "com.android.application" 및 "com.android.library"의 기본 플러그인 ID
        try {
            val androidExtension = project.extensions.getByType(com.android.build.gradle.BaseExtension::class.java)
            if (androidExtension.namespace == null) {
                val defaultNamespace = project.group?.toString() ?: "com.example.default.${project.name}"
                println("INFO (withId): Module ${project.name} has no namespace. Setting to '$defaultNamespace'")
                androidExtension.namespace = defaultNamespace
            }
        } catch (e: Exception) {
            println("WARN (withId): Could not configure namespace for ${project.name}: ${e.message}")
        }
    }
}

Summary

I had a real headache trying to use the Isar NoSQL Database plugin. It’s been a while since I had such a throbbing headache, but now that it’s finally resolved, I feel so relieved. The headache is gone too. 🙂


Reference

2025.05.28 – [분류 전체보기] – Flutter 기본 기능 | as, show, hide | 변수나 함수, 클래스명이 같은 경우 해결 방법 | example

댓글 남기기