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

Android Studio中Flutter Module文件索引失败问题求助

背景

我用VS Code开发Flutter有段时间了,现在想切换到Android Studio。我的项目是嵌入Android应用的Flutter Module,这个方案已经稳定运行好几年了。

问题

把项目导入Android Studio后,所有文件因为没被索引都显示黄色。
文件未被索引显示黄色

尝试运行项目时,配置界面底部出现如下错误:
运行配置界面底部错误

线索

如果创建新Flutter项目然后粘贴所有文件,项目能正常运行,但重启IDE或者执行Invalidate Cache后问题又会出现。我不清楚原因,怀疑是有内容覆盖设置导致索引丢失。

Flutter Doctor检查结果

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.13.6, on macOS 14.1.1 23B81 darwin-arm64, locale fr-FR)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.84.2)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

已尝试的解决方法

我试过网上能找到的所有办法:

  • Invalidate Cache
  • 保留并粘贴.iml文件到项目中
  • 删除.idea文件夹和.iml文件
  • 使用Android Studio内置工具修复项目
  • 重新创建项目并粘贴所有文件(仅在重启IDE前有效)

更新!

我尝试创建空白新项目,逐个添加文件并重启IDE,定位导致索引失败的文件。最终发现问题出在build.gradle.kts及相关Gradle配置上,推测Android Studio因为Gradle配置出现异常。请问现在该怎么解决?

以下是去除隐私信息后的build.gradle.kts文件:

// when changing the Flutter version, don"t forget to also upload the dependencies generated by the aar to the GAR with dependency-build.gradle

import java.io.FileInputStream
import java.util.Properties
import phraseapp.phraseapp

   
fun garToken() : String {
    val fis = FileInputStream("${project.rootDir}/.secure/properties/gradle.properties")
    val prop = Properties()
    prop.load(fis)
    return prop.getProperty("GAR_TOKEN")
}

fun phraseToken() : String {
    val fis = FileInputStream("${project.rootDir}/.secure/properties/gradle.properties")
    val prop = Properties()
    prop.load(fis)
    return prop.getProperty("PHRASE_TOKEN")
}


fun versionName() : String {
    val fis = FileInputStream("${project.rootDir}/gradle.properties")
    val prop = Properties()
    prop.load(fis)
    return prop.getProperty("VERSION_NAME")
}

val flutterVersion = "1.0.0-a794cf2681c6c9fe7b260e0e84de96298dc9c18b"
val dependenciesVersion = "23.12.01-02"


repositories {
        google()
        mavenCentral()
}

buildscript {
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.41")
        classpath("com.decathlon.phrase:phrase-gradle-plugin:1.0.0-beta01") 
    }  
}

plugins {
    kotlin("jvm") version "1.3.70"
    `maven-publish`
}

apply {
    plugin("com.company.phraseapp")
}


phraseapp {
    val token = phraseToken()
    platform.set(phraseapp.internal.Platform.FLUTTER)
    authToken.set("token $token")
    projectId.set("xx")
    mainLocaleId.set("xx")
    resFolders.set(arrayListOf("res"))
}

tasks.register<Task>(name = "resolveDependencies") {
    group = "Build Setup"
    description = "Resolve and prefetch dependencies"
    doLast {
        rootProject.allprojects.forEach {
            it.buildscript.configurations.filter(Configuration::isCanBeResolved).forEach { it.resolve() }
            it.configurations.filter(Configuration::isCanBeResolved).forEach { it.resolve() }
        }
    }
}


publishing {
    publications {
        create<MavenPublication>("debug") {
            groupId = "com.company.flutter.project"
            artifactId = "debug"
            version = versionName()
        
            artifact("$buildDir/host/outputs/repo/com/company/project/flutter_debug/1.0/flutter_debug-1.0.aar")
            
            pom.withXml {
                // hack to manual add dependencies
                val root = asNode()
                val dependenciesNode = root.appendNode("dependencies")

                fun addDependency(groupId: String, artifactId: String, version: String, scope:String) {
                    val dep = dependenciesNode.appendNode("dependency") 
                    dep.appendNode("groupId", groupId)
                    dep.appendNode("artifactId", artifactId)
                    dep.appendNode("version", version)
                    dep.appendNode("scope", scope)
                }

                addDependency("io.flutter", "flutter_embedding_debug", flutterVersion, "compile")
                addDependency("io.flutter", "armeabi_v7a_debug", flutterVersion, "compile")
                addDependency("io.flutter", "arm64_v8a_debug", flutterVersion, "compile")
                addDependency("io.flutter", "x86_64_debug", flutterVersion, "compile")
                addDependency("io.flutter", "x86_debug", flutterVersion, "compile")
                addDependency("dev.fluttercommunity.plus.device_info", "device_info_plus_debug", 
                dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.pathprovider", "path_provider_debug", dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.firebase.core", "firebase_core_debug",dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.firebase.analytics", "firebase_analytics_debug",dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.firebase.remoteconfig", "firebase_remoteconfig_debug",dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.firebase.crashlytics", "firebase_crashlytics_debug",dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.firebase.performance", "firebase_performance_debug",dependenciesVersion, "compile")
                addDependency("net.nfet.flutter.printing", "printing_debug", dependenciesVersion, "compile")
                addDependency("com.jrai.flutter_keyboard_visibility", "flutter_keyboard_visibility_debug", dependenciesVersion, "compile")

                root.appendNode("description", "Flutter module for xx")
                root.appendNode("inceptionYear", "x")
            }
        }
        create<MavenPublication>("release") {
            groupId = "com.company.flutter.project"
            artifactId = "release"
            version = versionName()

            artifact("$buildDir/host/outputs/repo/com/company/project/flutter_release/1.0/flutter_release-1.0.aar")

            pom.withXml {
                // manually add dependencies
                val root = asNode()
                val dependenciesNode = root.appendNode("dependencies")

                fun addDependency(groupId: String, artifactId: String, version: String, scope:String) {
                    val dep = dependenciesNode.appendNode("dependency") 
                    dep.appendNode("groupId", groupId)
                    dep.appendNode("artifactId", artifactId)
                    dep.appendNode("version", version)
                    dep.appendNode("scope", scope)
                }

                addDependency("io.flutter", "flutter_embedding_release", flutterVersion, "compile")
                addDependency("io.flutter", "armeabi_v7a_release", flutterVersion, "compile")
                addDependency("io.flutter", "arm64_v8a_release", flutterVersion, "compile")
                addDependency("io.flutter", "x86_64_release", flutterVersion, "compile")
                addDependency("dev.fluttercommunity.plus.device_info", "device_info_plus_release", dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.pathprovider", "path_provider_release", dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.firebase.core", "firebase_core_release",dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.firebase.analytics", "firebase_analytics_release",dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.firebase.remoteconfig", "firebase_remoteconfig_release",dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.firebase.crashlytics", "firebase_crashlytics_release",dependenciesVersion, "compile")
                addDependency("io.flutter.plugins.firebase.performance", "firebase_performance_release",dependenciesVersion, "compile")
                addDependency("net.nfet.flutter.printing", "printing_release", dependenciesVersion, "compile") 
                addDependency("com.jrai.flutter_keyboard_visibility", "flutter_keyboard_visibility_release", dependenciesVersion, "compile")

                root.appendNode("description", "Flutter module for xx")
                root.appendNode("inceptionYear", "x")
            }
        }
    }
    repositories {
        maven {
            url = uri("https://xx")
            credentials {
                username = "_json_key_base64"
                password = garToken()
            }
            authentication {
                create<BasicAuthentication>("basic")
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Aymeric Le Feyer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 08:59:53