Plugin [id: ‘flutter‘] was not found in any of the following sources解决方法
文章目录
- 错误描述
- 解决方法
- 修正方案:继续使用 `apply from`
- 修正后的 `build.gradle`
- 说明
- 警告的处理
- 进一步验证
错误描述
Plugin [id: ‘flutter’] was not found in any of the following sources:
- Gradle Core Plugins (not a core plugin, please see https://docs.gradle.org/7.5/userguide/standard_plugins.html for available core plugins)
- Plugin Repositories (plugin dependency must include a version number for this source)
解决方法
报错提示 Gradle 无法找到 flutter
插件,因为它不是 Gradle 的核心插件,且没有指定插件的来源和版本。Flutter 插件是通过 apply from
方式引入的,而不是通过 Gradle Plugin Portal 或类似的插件仓库。
要修正这个问题,需要调整配置,正确引入 Flutter 的 Gradle 插件。
修正方案:继续使用 apply from
由于 Flutter 的 Gradle 插件是 Flutter SDK 自带的,目前还不能直接通过 plugins
块引入。因此,仍需保留以下语句:
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
完整的 build.gradle
文件应该保持如下结构:
修正后的 build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.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 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'
}
android {
compileSdkVersion flutter.compileSdkVersion
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 {
applicationId "com.example.learning"
minSdkVersion 21
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
buildTypes {
release {
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:multidex:1.0.3'
}
说明
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
是 Flutter 官方推荐的加载方式。- 当前 Flutter SDK 尚未将插件迁移到 Gradle Plugin Portal,因此无法通过
plugins
块直接声明。
警告的处理
虽然警告仍然存在,但这是 Flutter 官方插件机制的限制。在未来的 Flutter 版本中,官方可能会发布新的机制来消除这种警告。
进一步验证
- 清理项目:
flutter clean
- 重新生成 APK:
flutter build apk
结束语
Flutter是一个由Google开发的开源UI工具包,它可以让您在不同平台上创建高质量、美观的应用程序,而无需编写大量平台特定的代码。我将学习和深入研究Flutter的方方面面。从基础知识到高级技巧,从UI设计到性能优化,欢饮关注一起讨论学习,共同进入Flutter的精彩世界!