【问题标题】:Error while running 'flutter build apk' command运行“flutter build apk”命令时出错
【发布时间】:2021-05-14 15:41:27
【问题描述】:

我在为 Flutter 应用构建发布版本时遇到问题。当我运行命令“flutter build app --no-shrink”时,出现以下错误。我尝试使用flutter clean,使缓存/重启无效,删除.gradle文件夹,但似乎没有任何解决方法。不知道是什么原因造成的。

关于如何解决这个问题的任何建议?

错误输出

/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

【问题讨论】:

  • 尝试在 android 文件夹中运行 ./gradlew assembleRelease --info 以查看您遇到的确切异常
  • 我添加了输出,这似乎是 gradle-wrapper.properties 的问题?我应该将它更新到 7.0 吗?
  • 我尝试使用不同版本的 cradle-wrapper.properties,但似乎没有任何效果......

标签: java android flutter dart


【解决方案1】:

解决方案

我不确定是什么导致了这个问题,但似乎 key.properties 文件有问题。我删除了生成的 upload-keystore.jks 文件并重新生成了它。之后我将gradle版本更改如下:

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

此文件应如下所示:

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

我编辑了这个文件和 gradle 版本(同时替换了 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

【讨论】:

    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 2020-08-24
    • 2021-05-04
    • 1970-01-01
    • 2020-01-25
    • 2021-06-11
    • 2020-09-17
    • 2021-08-29
    相关资源
    最近更新 更多