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

Flutter Release包编译失败:Kotlin编译错误求助

Flutter Android编译失败:Unresolved Reference 错误解决

报错信息

e: file:///C:/Users/○○/○○/android/app/src/main/kotlin/com/example/ai_art/MainActivity.kt:5:8 Unresolved reference: io
e: file:///C:/Users/○○/○○/android/app/src/main/kotlin/com/example/ai_art/MainActivity.kt:7:21 Unresolved reference: FlutterActivity    
e: file:///C:/Users/○○/○○/android/app/src/main/kotlin/com/example/ai_art/MainActivity.kt:8:5 'onCreate' overrides nothing
e: file:///C:/Users/○○/○○/android/app/src/main/kotlin/com/example/ai_art/MainActivity.kt:9:15 Unresolved reference: onCreate
e: file:///C:/Users/○○/○○/android/app/src/main/kotlin/com/example/ai_art/MainActivity.kt:10:9 Unresolved reference: requestedOrientation

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileReleaseKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 54s
Running Gradle task 'assembleRelease'...                           55.8s
Gradle task assembleRelease failed with exit code 1

已尝试操作

  • 更换Gradle版本(8.3、8.1等),问题未解决

项目核心配置文件

MainActivity.kt

package com.○○.○○

import android.content.pm.ActivityInfo
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }
}

android/build.gradle

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

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

buildscript {
    ext.kotlin_version = '1.7.21'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.1.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 
    }
}

android/app/build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    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 keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

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

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

android {
    namespace = "com.○○.○○"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion
    buildFeatures {
        viewBinding true
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

    defaultConfig {
        applicationId = "com.○○.○○"
        minSdk = flutter.minSdkVersion
        minSdkVersion 21
        targetSdk = flutter.targetSdkVersion
        versionCode = flutterVersionCode.toInteger()
        versionName = flutterVersionName
    }

    signingConfigs {
       release {
           keyAlias keystoreProperties['keyAlias']
           keyPassword keystoreProperties['keyPassword']
           storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
           storePassword keystoreProperties['storePassword']
       }
   }

   buildTypes {
        release {
            signingConfig = signingConfigs.release
            debug {
                resValue "string", "app_name", "○○"
            applicationIdSuffix ".debug"
            versionNameSuffix "-d"
                }
            release {
                resValue "string", "app_name", "○○"
            }
        }
    }
}

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

android/settings.gradle

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }

    plugins {
        id "com.android.application" version "1.7.21" apply false
        id "org.jetbrains.kotlin.android" version "1.8.0" apply false
        id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    }
}

include ":app"

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip

问题排查与解决步骤

1. 修正settings.gradle插件版本不匹配问题

插件版本需与项目依赖保持一致,修改pluginManagement下的plugins块:

plugins {
    id "com.android.application" version "8.1.4" apply false // 与build.gradle中AGP版本一致
    id "org.jetbrains.kotlin.android" version "1.7.21" apply false // 与buildscript中的kotlin_version一致
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
}

2. 修复app/build.gradle中buildTypes语法错误

原配置中debug和release块错误嵌套在release内部,调整为同级结构:

buildTypes {
    debug {
        resValue "string", "app_name", "○○"
        applicationIdSuffix ".debug"
        versionNameSuffix "-d"
    }
    release {
        signingConfig = signingConfigs.release
        resValue "string", "app_name", "○○"
    }
}

3. 清理defaultConfig重复配置

删除重复的minSdkVersion 21,保留Flutter统一管理的版本:

defaultConfig {
    applicationId = "com.○○.○○"
    minSdk = flutter.minSdkVersion
    targetSdk = flutter.targetSdkVersion
    versionCode = flutterVersionCode.toInteger()
    versionName = flutterVersionName
}

4. 清理缓存并重新构建

执行以下命令彻底清理后重新编译:

flutter clean
flutter pub get
cd android && ./gradlew clean assembleRelease

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 02:23:11