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

Android Studio打开完整Flutter项目构建APK失败求助

问题

我的Flutter项目在Visual Studio Code中可正常构建为Android应用;单独打开android文件夹并作为Android项目在Android Studio中构建也能成功。但在Android Studio中打开整个Flutter项目并尝试构建Android版本时(iOS版本构建正常),出现错误:

Exception: Gradle build failed to produce an .apk file. It's likely that this file was generated under .../build, but the tool couldn't find it.

怀疑是IDE Bug,恳请提供解决方案。

环境信息:

  • Android Studio版本:Electric Eel | 2022.1.1(Build #AI-221.6008.13.2211.9477386,构建于2023年1月11日)

附加代码(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 FileNotFoundException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
def flutterVersionName = localProperties.getProperty('flutter.versionName')


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

android {
    compileSdkVersion 33

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

    flavorDimensions "distribute"
    productFlavors {
        appCenter {
            dimension "distribute"
        }

        googlePlay {
            dimension "distribute"
        }
    }

    lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false
    }

    defaultConfig {
        // TODO: 指定你自己的唯一应用ID
        applicationId "myId"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 3
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: 为发布版本添加签名配置
            // 暂时使用调试密钥签名,这样`flutter run --release`可以正常运行
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
解决方案
  1. 指定正确的构建变体
    你配置了两个产品风味(appCenter、googlePlay),Android Studio打开完整Flutter项目时可能未自动选中有效变体。打开右侧Build Variants面板,选择appCenterDebug或googlePlayDebug后重新构建。

  2. 清理缓存后重新构建

    • 执行命令:flutter clean清除Flutter构建缓存
    • 删除android目录下的.gradle文件夹
    • 重启Android Studio,重新执行构建
  3. 修复IDE与插件兼容性
    Android Studio Electric Eel版本较旧,可能和当前Flutter SDK版本不兼容。可以:

    • 更新Flutter SDK到最新稳定版
    • 或降级Android Studio到与Flutter版本匹配的稳定版本(比如Chipmunk)
    • 卸载重装Android Studio中的Flutter插件
  4. 明确APK输出路径
    在app/build.gradle的android块中添加输出配置,强制指定APK命名和路径,确保Flutter工具能找到文件:

    android {
        // 保留原有配置...
        
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith('.apk')) {
                    def fileName = "${variant.flavorName}-${variant.buildType.name}-v${variant.versionName}.apk"
                    output.outputFileName = fileName
                }
            }
        }
    }
    
  5. 用命令行验证构建
    在项目根目录执行命令构建:

    • flutter build apk --flavor appCenter
    • flutter build apk --flavor googlePlay
      如果命令行能成功生成APK,说明是Android Studio插件问题,可尝试重置IDE设置(File > Manage IDE Settings > Restore Default Settings)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 03:25:19