Flutter - Not able to migrate to Gradle 8

Hi to everyone, It is my first time here.

Here’s the issue:
I first wanted to build My app in release mode, after I updated my app Flutter version.

Flutter doctor output:


Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.13.2, on macOS 13.0.1 22A400 darwin-arm64, locale en-IT)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.81.1)
[✓] Connected device (2 available)
[✓] Network resources

• No issues found!

but I got an error with the ext.kotlin_version that was too old (1.6.10) so I updated it

Now is : ext.kotlin_version = '1.8.22'

and here’s the dependencies:

    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.4.0'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.9'
    }

I’ve also updated the gradle-wrapper.properties from distributionUrl=https://services.gradle.org/distributions/gradle-7.4-all.zip
to:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip

After I run flutter build apk I got an error telling me to put my package name “com.example.namespace” inside android{ in android/app/build.gradle and I did so. And also removed it from AndroidManifest.xml

The error has disappeared, but now the situation is this, if i run “flutter build apk”

I get this:


FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':audio_session'.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
   > Namespace not specified. Please specify a namespace in the module's build.gradle file like so:

     android {
         namespace 'com.example.namespace'
     }

     If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

Please Help me, I don’t kow how to fix it, I don’t find a audio_session module, It is an external dependency I think… and If in Android studio I try to click on Tools/AGP Upgrade Assistant, nothing happens.

Thanks in advance.

1 Like

This has nothing to do with Gradle actually, more than that you use Gradle to run your build.
This is about an Android specific problem.
If you google for “Namespace not specified” you get many results.
And if you need more help than Google can provide, you probably better ask in some Android community.

Hi… I also encountered the same problem… Have you found any solution? Tq

Again, this is very common problem, but off-topic here.
Just Google for it or search in some Android community and you will find a solution.
(I don’t have it, I’m not into Android development, but have seen the same question multiple times)

How is it Off topic if it’s about Gradle?

Afair this is purely a problem in your Android stuff. Maybe it is a thing of the Android Gradle plugin, so it would be kind of on-topic, yeah. But you might still have more luck in an Android specific community, or if you Google, as multiple people had that problem already before. :slight_smile:

This is happening because from gradle 8 it is required for all projects to have namespace added in their build.gradle.
In your case the project :audio_session which I think would be a dependency added in pubspec.yaml of your flutter project doesn’t have its namespace field added. A solution for this would to be check the depedencies pub package page to see if they have a newer version which supports android gradle plugin 8. If not you can ask the package owners to fix this.

1 Like

Add the following to top level build.gradle. This will set the namespace value in all subprojects

subprojects {
   afterEvaluate { project ->
        if (project.hasProperty('android')) {
            project.android {
                if (namespace == null) {
                    namespace project.group
                }
            }
        }
    }
}
1 Like

… and is very bad practice.

- * - != + :smiley:

subprojects { ... } and afterEvaluate { ... } are both bad practices alone already, let alone combined.

Note: While this works, this is not a valid solution for production projects. While this allowed us to build / execute / test an app with 100+ flutter packages, Gradle 8 and associated AGP, we did experience a 4% spike in crashes when resuming the app from the background. The crashes were not in Flutter code but android library code. Gradle 8 / kotlin 1.9.x are building with Java 17 and most flutter packages haven’t been updated to use this. Gradle 8 marks mixing of java versions as errors and you have to add another hack to turn those back to warnings (as Gradle 7 defaulted to).

What do you think about this workaround some people are doing with gradle to fix this issue in flutter while we wait for packages to update?

Well, it is practically what was written above already, isn’t it?
So I already wrote what I think.

If you need it as workaround due to the strange flutter source-dependency-strategy, use it. But nevertheless it is bad practice and discouraged to do such cross project configuration, as it introduces project coupling and works against more sophisticated Gradle features and optimizations.

I appreciate you mentioning this being a bad practice, which I wasn’t aware of.
Unfortunately, this workaround is still required, since some Flutter packages haven’t moved to the declarative apply syntax. Can you recommend another approach achieving the same result?

I’m not into Flutter development, so no.
Maybe it would be at least marginally better to do it from the settings script instead of configuring one project from others. :man_shrugging:

The best way though probably is, to get your dependencies to be compatible with recent versions.