You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter构建AppBundle失败:无法找到lifecycle-runtime-2.2.0.jar依赖

Flutter构建AppBundle失败:无法解析lifecycle-runtime-2.2.0.jar依赖

问题描述

执行flutter build appbundle时构建失败,核心错误为两个任务(:connectivity:compileReleaseJavaWithJavac和:firebase_core:compileReleaseJavaWithJavac)无法找到androidx.lifecycle:lifecycle-runtime:2.2.0的jar包,搜索路径为https://dl.google.com/dl/android/maven2/androidx/lifecycle/lifecycle-runtime/2.2.0/lifecycle-runtime-2.2.0.jar。

当前Gradle配置

gradle-wrapper.properties

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

根目录build.gradle

buildscript {
    ext.kotlin_version = '1.8.21'
    repositories {
        google()
        jcenter()
        maven {
            url 'https://download.flutter.io'
        }

    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.8'  // Google Services plugin

    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://download.flutter.io'
        }

    }
}

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

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

android/app/build.gradle

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

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

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

android {
    compileSdkVersion 33

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

    lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false //<- add this line
    }

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

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null // original code
            //storeFile file("key.jks") ? file("key.jks") : null //new solution for some customers
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    def lifecycle_version = "2.6.1"
    def arch_version = "2.2.0"

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.firebase:firebase-messaging:20.1.0'
    implementation 'com.android.support:multidex:1.0.3'
    // Import the Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:28.0.1')
        implementation('com.google.firebase:firebase-auth') {
            exclude module: "play-services-safetynet"
        }

    implementation("com.google.firebase:firebase-iid")

    // When using the BoM, you don't specify versions in Firebase library dependencies

    // Declare the dependency for the Firebase SDK for Google Analytics
    implementation 'com.google.firebase:firebase-analytics-ktx'

    // Declare the dependencies for any other desired Firebase products
    // For example, declare the dependencies for Firebase Authentication and Cloud Firestore
    implementation 'com.google.firebase:firebase-auth-ktx'
    implementation 'com.google.firebase:firebase-firestore-ktx'


}

解决方案

1. 强制统一Lifecycle版本

在android/app/build.gradle的android块下添加版本强制配置,让所有依赖使用你定义的2.6.1版本:

android {
    // 保留原有的android配置...

    configurations.all {
        resolutionStrategy {
            force "androidx.lifecycle:lifecycle-runtime:2.6.1"
            force "androidx.lifecycle:lifecycle-common-java8:2.6.1"
            force "androidx.lifecycle:lifecycle-livedata:2.6.1"
            force "androidx.lifecycle:lifecycle-viewmodel:2.6.1"
        }
    }
}

2. 更新仓库与依赖

  • 移除已停止维护的jcenter()仓库,替换为mavenCentral(),修改根目录build.gradle的仓库配置:
    buildscript {
        repositories {
            google()
            mavenCentral()
            maven { url 'https://download.flutter.io' }
        }
        // 保留其他配置...
    }
    
    allprojects {
        repositories {
            google()
            mavenCentral()
            maven { url 'https://download.flutter.io' }
        }
    }
    
  • 替换旧版multidex依赖为AndroidX版本:
    // 替换原有的com.android.support:multidex:1.0.3
    implementation 'androidx.multidex:multidex:2.0.1'
    

3. 清理缓存并重构

执行以下命令清理缓存并刷新依赖:

flutter clean
./gradlew clean build --refresh-dependencies

然后重新执行构建:

flutter build appbundle

4. 修复Firebase依赖冲突

  • 移除手动指定的firebase-messaging版本,由Firebase BoM统一管理:
    // 删除这一行
    // implementation 'com.google.firebase:firebase-messaging:20.1.0'
    
  • 移除已弃用的firebase-iid依赖,BoM会自动管理必要的实例ID相关依赖。

内容的提问来源于stack exchange,提问作者reza kia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 19:15:05