Search code examples
javaandroidflutterdart

Error while running 'flutter build apk' command


I'm having trouble building release version for an flutter app. When I run the command "flutter build app --no-shrink" I get this error below. I tried with flutter clean, invalidate cache/restart, deleting .gradle folder but nothing seems to solve it. I don't know what is causing this.

Any advice on how to solve this?

error output

/Users/admin/.gradle/caches/transforms-2/files-2.1/efcc4312065c65c7c7e3232f7c990f31/jetified-firebase-messaging-22.0.0/jars/classes.jar: R8: Type com.google.firebase.iid.FirebaseInstanceIdReceiver is defined multiple times: /Users/admin/.gradle/caches/transforms-2/files-2.1/efcc4312065c65c7c7e3232f7c990f31/jetified-firebase-messaging-22.0.0/jars/classes.jar:com/google/firebase/iid/FirebaseInstanceIdReceiver.class, /Users/admin/.gradle/caches/transforms-2/files-2.1/06de51a56b776cd4fc647c005c2b7e7b/firebase-iid-21.0.1/jars/classes.jar:com/google/firebase/iid/FirebaseInstanceIdReceiver.class
                                                                        
FAILURE: Build failed with an exception.                                
                                                                        
* What went wrong:                                                      
Execution failed for task ':app:transformClassesAndResourcesWithR8ForRelease'.
> com.android.tools.r8.CompilationFailedException: Compilation failed to complete
                                                                        
* 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                              
                                                                        
BUILD FAILED in 2m 3s                                                   
Running Gradle task 'assembleRelease'...                                
Running Gradle task 'assembleRelease'... Done                     124.3s
[!] The shrinker may have failed to optimize the Java bytecode.
    To disable the shrinker, pass the `--no-shrink` flag to this command.
    To learn more, see: https://developer.android.com/studio/build/shrink-code

app/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 30

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.******"
        minSdkVersion 20
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.google.android.material:material:1.3.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

//    testImplementation 'junit:junit:4.12'
//    androidTestImplementation 'androidx.test:runner:1.2.0' // 1.1.1
//    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' // 3.1.1

    // Import the Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:26.3.0')

    // Add the dependency for the Firebase SDK for Google Analytics
    // When using the BoM, don't specify versions in Firebase dependencies

    implementation 'com.google.firebase:firebase-analytics'
    // implementation "com.google.firebase:firebase-messaging"

    // Add the dependencies for any other desired Firebase products
    // https://firebase.google.com/docs/android/setup#available-libraries

    // Declare the dependency for the Firebase Authentication library
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-auth'

    implementation 'com.google.firebase:firebase-messaging:22.0.0'

    implementation 'androidx.browser:browser:1.3.0'

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
apply plugin: 'com.google.gms.google-services'

android/build.gradle

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        //classpath 'com.android.tools.build:gradle:4.1.0'
        classpath 'com.android.tools.build:gradle:3.5.0' // 3.5.0
        classpath 'com.google.gms:google-services:4.3.4' // 4.3.2
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

./gradlew assembleRelease --info

* What went wrong:
Execution failed for task ':app:transformClassesAndResourcesWithR8ForRelease'.
> com.android.tools.r8.CompilationFailedException: Compilation failed to complete

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output. Run with --scan to get full insights.
AAPT2 aapt2-3.5.0-5435860-osx Daemon #9: shutdown

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 5m 9s
AAPT2 aapt2-3.5.0-5435860-osx Daemon #10: shutdown
509 actionable tasks: 508 executed, 1 up-to-date
AAPT2 aapt2-3.5.0-5435860-osx Daemon #11: shutdown

Solution

  • Solution

    I'm not really sure what was causing this problem but it seem as if something was wrong with the key.properties file. I deleted the generated upload-keystore.jks file and regenerated it. After that I changed the gradle version as follows:

    android -> gradle -> wrapper -> gradle-wrapper.properties

    This file should look something like this:

    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
    

    I edited this file and the gradle version to this (alongside with replacing the upload-keystore.jks):

    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip