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

执行flutter build appbundle时遇:path_provider_android编译任务失败

Flutter构建App Bundle失败问题解决方案

问题情况

执行flutter build appbundle时触发错误:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':path_provider_android:compileReleaseJavaWithJavac'.
> Could not resolve all files for configuration ':path_provider_android:androidJdkImage'.
   > Failed to transform core-for-system-modules.jar to match attributes {artifactType=_internal_android_jdk_image, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for JdkImageTransform: /Users/armagangok/Library/Android/sdk/platforms/android-34/core-for-system-modules.jar.
         > Error while executing process /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/jlink with arguments {--module-path /Users/armagangok/.gradle/caches/transforms-3/cdad0845cf274cf31a832ff77b7149b2/transformed/output/temp/jmod --add-modules java.base --output /Users/armagangok/.gradle/caches/transforms-3/cdad0845cf274cf31a832ff77b7149b2/transformed/output/jdkImage --disable-plugin system-modules}

同时伴随Java 8版本过时的警告,当前使用JDK 17,项目级和应用级build.gradle配置如下:

项目级build.gradle

allprojects {
    ext.kotlin_version = '1.9.20'    
    repositories {
        google()
        mavenCentral()
    }
}

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

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

应用级build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
}

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

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

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

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

android {
    namespace = "com.appsweat.pomodoro_app"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = "25.1.8937393"

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8' // Update this line
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.appsweat.pomodoro_app"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdk = 21
        targetSdk = 34
        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 {
           // TODO: Add your own signing config for the release build.
           // Signing with the debug keys for now,
           // so flutter run --release works.
           signingConfig signingConfigs.release
       }
    }
}

flutter {
    source = "../.."
}

解决方案

1. 统一子项目Java版本配置

修改项目级build.gradle,强制所有第三方依赖子项目使用Java 11(适配JDK 17环境):

subprojects {
    project.evaluationDependsOn(":app")
    afterEvaluate {
        if (project.hasProperty('android')) {
            android {
                compileOptions {
                    sourceCompatibility = JavaVersion.VERSION_11
                    targetCompatibility = JavaVersion.VERSION_11
                }
                kotlinOptions {
                    jvmTarget = '11'
                }
            }
        }
    }
}

2. 升级Gradle与Android Gradle插件

  • 在应用级build.gradle更新插件版本:
plugins {
    id "com.android.application" version "8.2.0"
    id "kotlin-android" version "1.9.20"
    id "dev.flutter.flutter-gradle-plugin"
}
  • 在gradle/wrapper/gradle-wrapper.properties更新Gradle版本:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-all.zip

3. 清理缓存

执行以下命令清除冗余缓存:

flutter clean
rm -rf ~/.gradle/caches
flutter pub get

4. 更新第三方依赖

在pubspec.yaml中升级path_provider及其他老旧依赖到最新稳定版:

dependencies:
  path_provider: ^2.1.2 # 替换为实际最新版本

更新后执行:

flutter pub upgrade

5. 验证JDK环境

确保终端和Android Studio使用同一JDK 17:

  • 终端执行echo $JAVA_HOME,确认输出指向JDK 17路径
  • Android Studio中通过File > Project Structure > SDK Location设置JDK为17

内容的提问来源于stack exchange,提问作者Armagan GOK

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 15:48:10