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

Flutter项目运行报错:com.android.application插件未找到求助

解决Flutter编译报错:Plugin [id: 'com.android.application']未找到

报错信息

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\musta\AndroidStudioProjects\untitled\android\app\build.gradle' line: 2

* What went wrong:
Plugin [id: 'com.android.application'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)

* 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 1s
Exception: Gradle task assembleDebug failed with exit code 1

问题根源

  1. 插件未指定版本:com.android.application、kotlin-android等插件没有声明版本号,Gradle无法从仓库拉取对应插件
  2. 本地build.gradle存在语法错误:注释格式混乱、代码断行残缺、applicationId/namespace格式不合法
  3. 根项目Gradle配置缺失插件仓库地址,导致无法下载插件

修复步骤

1. 给插件添加版本号

在plugins块中为每个插件指定对应版本,版本号需与开发环境匹配(比如com.android.application版本对应compileSdkVersion 34,推荐用8.2.0):

plugins {
    id "com.android.application" version "8.2.0"
    id "kotlin-android" version "1.9.0"
    id "dev.flutter.flutter-gradle-plugin" version "1.0.0" // 版本与你的Flutter版本匹配即可
}

2. 修复build.gradle中的语法错误

  • 修正无效注释:把(https://developer.android.com/studio/build/application-id.html).改成// https://developer.android.com/studio/build/application-id.html
  • 删除残缺代码:移除buildTypes里的build.残句
  • 修正applicationId和namespace:改成合法包名格式,比如com.example.untitled
  • 修复断行错误:把exclude 'META-和INF/services/javax.annotation.processing.Processor'合并成一行

3. 检查根项目settings.gradle配置

确保根项目的settings.gradle中配置了插件仓库,示例:

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "untitled"
include ':app'

修正后的完整build.gradle示例

plugins {
    id "com.android.application" version "8.2.0"
    id "kotlin-android" version "1.9.0"
    id "dev.flutter.flutter-gradle-plugin" version "1.0.0"
}

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'
    }

android {
    namespace "com.example.untitled"
    compileSdkVersion 34
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

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

    defaultConfig {
        // TODO: Specify your own unique Application ID
        // https://developer.android.com/studio/build/application-id.html
        configurations.all {
            resolutionStrategy { force 'androidx.core:core-ktx:1.7.0-alpha01' }
        }

        applicationId "com.example.untitled"
        // You can update the following values to match your application needs.
        minSdkVersion 23
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
    
    packagingOptions {
        // Exclude file to avoid Error: Duplicate files during packaging of APK
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude 'META-INF/*.kotlin_module'
    }
}

flutter {
    source '../..'
}

dependencies {}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 10:52:34