无法为Flutter构建发布APK

14

我目前正在使用Flutter在Windows上构建已签名的发布APK。我已执行Flutter官网文档中概述的任务。当我运行构建命令时,这是我得到的输出。它似乎与Firebase插件有关。有任何想法吗?

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  firebase_auth: 0.4.0
  firebase_database: ^0.3.0

Flutter Doctor 输出

    C:\Users\myuser\projects\myflutterapp>flutter doctor
    [√] Flutter (on Microsoft Windows [Version 10.0.16299.125], locale en-US, channel alpha)
        • Flutter at C:\Users\myuser\flutter
        • Framework revision 8f65fec5f5 (3 weeks ago), 2017-12-12 09:50:14 -0800
        • Engine revision edaecdc8b8
        • Tools Dart version 1.25.0-dev.11.0
        • Engine Dart version 2.0.0-edge.d8ae797298c3a6cf8dc9f4558707bd2672224d3e

    [√] Android toolchain - develop for Android devices (Android SDK 27.0.1)
        • Android SDK at C:\Users\myuser\AppData\Local\Android\Sdk
        • Android NDK at C:\Users\myuser\AppData\Local\Android\Sdk\ndk-bundle
        • Platform android-27, build-tools 27.0.1
        • ANDROID_HOME = C:\Users\myuser\AppData\Local\Android\Sdk
        • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b01)

    [√] Android Studio (version 3.0)
        • Android Studio at C:\Program Files\Android\Android Studio
        • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b01)

    [-] Connected devices
        • None

错误日志信息

    C:\Users\myuser\projects\myflutterapp>flutter build apk --release
    Initializing gradle...                                0.9s
    Resolving dependencies...                             1.1s
    Running 'gradlew assembleRelease'...
    Configuration 'debugCompile' in project ':app' is deprecated. Use 'debugImplementation' instead.
    Configuration 'profileCompile' in project ':app' is deprecated. Use 'profileImplementation' instead.
    Configuration 'releaseCompile' in project ':app' is deprecated. Use 'releaseImplementation' instead.
    Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
    registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
    registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
    registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
    Configuration 'debugProvided' in project ':firebase_auth' is deprecated. Use 'debugCompileOnly' instead.
    Configuration 'releaseProvided' in project ':firebase_auth' is deprecated. Use 'releaseCompileOnly' instead.
    Configuration 'compile' in project ':firebase_core' is deprecated. Use 'implementation' instead.
    Configuration 'debugProvided' in project ':firebase_core' is deprecated. Use 'debugCompileOnly' instead.
    Configuration 'releaseProvided' in project ':firebase_core' is deprecated. Use 'releaseCompileOnly' instead.
    Configuration 'debugProvided' in project ':firebase_database' is deprecated. Use 'debugCompileOnly' instead.
    Configuration 'releaseProvided' in project ':firebase_database' is deprecated. Use 'releaseCompileOnly' instead.
    Skipping AOT snapshot build. Fingerprint match.

    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ':app:lintVitalRelease'.
    > Could not resolve all artifacts for configuration ':app:profileRuntimeClasspath'.
    > Could not resolve project :firebase_auth.
        Required by:
            project :app
        > Unable to find a matching configuration of project :firebase_auth:
            - Configuration 'debugApiElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'debug'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found incompatible value 'java-api'.
            - Configuration 'debugRuntimeElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'debug'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found compatible value 'java-runtime'.
            - Configuration 'releaseApiElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'release'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found incompatible value 'java-api'.
            - Configuration 'releaseRuntimeElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'release'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found compatible value 'java-runtime'.
    > Could not resolve project :firebase_database.
        Required by:
            project :app
        > Unable to find a matching configuration of project :firebase_database:
            - Configuration 'debugApiElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'debug'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found incompatible value 'java-api'.
            - Configuration 'debugRuntimeElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'debug'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found compatible value 'java-runtime'.
            - Configuration 'releaseApiElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'release'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found incompatible value 'java-api'.
            - Configuration 'releaseRuntimeElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'release'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found compatible value 'java-runtime'.
    > Could not resolve project :firebase_core.
        Required by:
            project :app
        > Unable to find a matching configuration of project :firebase_core:
            - Configuration 'debugApiElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'debug'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found incompatible value 'java-api'.
            - Configuration 'debugRuntimeElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'debug'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found compatible value 'java-runtime'.
            - Configuration 'releaseApiElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'release'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found incompatible value 'java-api'.
            - Configuration 'releaseRuntimeElements':
                - Required com.android.build.api.attributes.BuildTypeAttr 'profile' and found incompatible value 'release'.
                - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
                - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
                - Required org.gradle.api.attributes.Usage 'java-runtime' and found compatible value 'java-runtime'.

    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

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

    BUILD FAILED in 38s
    Gradle build failed: 1

你可以通过命令提示符生成已签名的apk/bundle:https://dev59.com/questions/7FMI5IYBdhLWcg3wsdyA#60699042 - Marcel Hofgesang
6个回答

15

事实证明,处理这个问题的最佳方法是在android/app/build.gradle中进行以下更改:

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.3'
    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }
    // This is the main section that I've updated
    // to get the release APK to build
    buildTypes {
        release {
            profile {
                matchingFallbacks = ['debug', 'release']
            }
            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

apply plugin: 'com.google.gms.google-services'

经过这个更改,我能够构建发布APK。


1
你能展示/告诉一下你的 build.gradle 文件顶部有什么吗?我也按照文档操作了,但是当我尝试从属性文件中添加密钥库信息(gradle 中签名配置的第一步)时,无法解析 new Properties() 和 new FileInputStream()。 - JC23
我已经添加了更多的构建脚本,希望这可以帮到你。 - marktechson
我认为对于旧版本的Gradle,配置文件有自己的部分在buildTypes下。按照上面的设置matchingFallbacks也有帮助。 - Diederik

8
尝试这个:
lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false
}

android/app/build.gradle


checkReleaseBuilds false 的意思是“不检查发布版本构建”。这个设置解决了我的问题。 - GokulDAS027
这将禁用代码检查。 - Mehrdad
应用程序现在在启动时崩溃了,这是否有关系? - GokulDAS027
1
这很不可能。因为对我来说成功了。 - Mehrdad

6

我刚刚停用了代码检查,这解决了我的版本构建问题。

lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false
}

4

我曾经遇到过相同的错误,后来发现自己不小心重复使用了buildTypes。

buildTypes {
  buildTypes {
    .....
  }
}

3

如果您在从Android构建APK时出现问题,请尝试使用终端命令:

Flutter build apk --build-name =1.0.1
Flutter build apk --build-Number=10
Flutter build apk --build-name =1.0.1 --build-number=10

在构建apk时,请确保您始终更改构建名称和构建编号。例如,如果您的上一个版本名称是1.0.1,则使用1.0.2或更高版本,并且构建编号为11。每次更改这个信息后,您的问题就会得到解决。


2
我尝试将调试模式改为发布模式,但这种方法对我无效。然而,当我查看了此网站:https://www.dartlang.org/tools/pub/dependencies,它说您可以设置依赖项而不指定版本号。因此,在pubspec.yaml文件中,我这样做了,并且它对我有效...
dependencies:
  flutter:
    sdk: flutter
    image_picker: 
    google_sign_in: 
    firebase_analytics: 
    firebase_auth: 
    firebase_database:
    firebase_storage:

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接