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

引入social_share包后Flutter Android编译报错:无匹配Java工具链

问题:引入social_share包后Flutter Android端编译失败(Java工具链匹配错误)

我有一个Flutter应用,引入social_share包前一切正常,之后应用开始崩溃,原本正常的包也出现异常。现在运行Android端时出现以下错误:

Execution failed for task ':audioplayers_android:compileDebugJavaWithJavac'.

Error while evaluating property 'javaCompiler' of task ':audioplayers_android:compileDebugJavaWithJavac'.

Failed to calculate the value of task ':audioplayers_android:compileDebugJavaWithJavac' property 'javaCompiler'.

No matching toolchains found for requested specification: {languageVersion=8, vendor=any, implementation=vendor-specific}.

No locally installed toolchains match and toolchain download repositories have not been configured.

项目级build.gradle

buildscript {
    ext.kotlin_version = '1.9.21'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        // START: FlutterFire Configuration
        classpath 'com.google.gms:google-services:4.4.0'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
        // END: FlutterFire Configuration
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

plugins {
    // Apply the java plugin to add support for Java
    id 'java'
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(8))
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
    tasks.withType(JavaCompile) {
        javaCompiler = javaToolchains.compilerFor {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':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 keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

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'
// START: FlutterFire Configuration
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
// END: FlutterFire Configuration
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 34 //localProperties.getProperty("flutter.compileSdkVersion")
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

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

    defaultConfig {
        applicationId "com.rlabs.wordera"
        minSdkVersion 21
        targetSdkVersion flutter.targetSdkVersion
        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 "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:32.7.0"))

    // Add 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")

    // Also add the dependency for the Google Play services library and specify its version
    implementation("com.google.android.gms:play-services-auth:20.7.0")
}

问题根源

social_share包间接引入了更高版本的Gradle依赖(如Gradle 8.x),而你的项目全局强制所有子项目使用Java 8工具链。Gradle 8.x默认要求Java 11+,两者冲突导致工具链匹配失败;同时项目级build.gradle中的全局工具链配置覆盖了子项目的默认设置,进一步加剧了冲突。

修复步骤

  1. 移除全局Java工具链强制配置
    删除项目级build.gradle中以下代码块:

    plugins {
        // Apply the java plugin to add support for Java
        id 'java'
    }
    
    java {
        toolchain {
            languageVersion.set(JavaLanguageVersion.of(8))
        }
    }
    
    allprojects {
        tasks.withType(JavaCompile) {
            javaCompiler = javaToolchains.compilerFor {
                languageVersion.set(JavaLanguageVersion.of(8))
            }
        }
    }
    

    仅保留allprojects中的仓库配置:

    allprojects {
        repositories {
            google()
            mavenCentral()
        }
    }
    
  2. 升级Gradle插件版本
    将项目级build.gradle中的Android Gradle插件版本从7.3.0升级到7.4.2,该版本兼容Java 8且能更好处理子项目依赖冲突:

    classpath 'com.android.tools.build:gradle:7.4.2'
    
  3. 统一子项目Java版本配置
    在项目级build.gradle的subprojects块中添加以下配置,强制子项目与主项目使用一致的Java版本:

    subprojects {
        project.buildDir = "${rootProject.buildDir}/${project.name}"
        afterEvaluate {
            if (project.plugins.hasPlugin('com.android.library')) {
                android {
                    compileOptions {
                        sourceCompatibility JavaVersion.VERSION_1_8
                        targetCompatibility JavaVersion.VERSION_1_8
                    }
                }
            }
        }
        project.evaluationDependsOn(':app')
    }
    
  4. 清理缓存并重建
    执行以下命令清除缓存并重新拉取依赖:

    flutter clean
    flutter pub get
    cd android && ./gradlew clean build --refresh-dependencies
    

完成以上步骤后重新运行应用,Android端编译即可恢复正常。

内容的提问来源于stack exchange,提问作者Efim Rubin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 21:47:01