How to fix "Gradle build failed to produce an Android bundle package." in Flutter

I have similar issue with the same error messages. (Build Release APK successfully but failed to build app bundle) The solution that works for me is to upgrade the Android Gradle Version. Below are the config that works for me (You may upgrade to the latest version also):

  1. /android/gradle/wrapper/gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

  1. /android/build.gradle:

    buildscript {
        repositories {
           google()
           jcenter()
          }
    
        dependencies {
           classpath 'com.android.tools.build:gradle:3.2.1'
        }
    }
    

There is a multitude of reasons for which you might encounter a frustrating problem like Gradle build failed to produce an Android bundle package.

As of @Tam Tam Tam's answer, first try to update your gradle and android gradle plugin to a latest version.

If that does not fix your problem, then try to disable all the different architectures and set universalApk to false in your android/app/build.grade in splits section. Put something like this:

splits {
        abi {
            enable true
            reset()
            // include 'x86', 'armeabi-v7a', 'x86_64'
            universalApk false
        }
    }

This suggestion was provided here: https://github.com/flutter/flutter/issues/27462#issuecomment-507229131

It happens because gradle generates the apk following a convention, eg, app-<architecture-name>.apk, which is not understandable by flutter, it expects apk names in another format, eg, simply app.apk. Disabling all different architectures name bring them to a common understanding :)

If that is also not work, then try to remember if you change your package name com.example.packagename to something your own personal like com.mywebsite.packagename. If you have done it, then you probably changed it everywhere needed, for example, in build.gradles and AndroidManifest.xml but you might forgot to change it in your java (or kotlin class).

To make your new package name works, you also have to change the package name in src/main/java/com/mywebsite/packagename/MainActivity.java (in case of kotlin, it's src/main/kotlin/com/mywebsite/packagename/MainActivity.kt)

I hope this helps.