我正在开发一个 Flutter 项目,并尝试使用 Kotlin 原生实现 AlarmManager,但我遇到了与 Android Activity 相关的问题
以下是两个错误:
MainActivity.kt: (5, 33): 未解决的参考:FlutterPlugin
MainActivity.kt: (12, 35): 类型不匹配:推断类型为 FlutterEngine,但预期为 FlutterPlugin.FlutterPluginBinding
package com.example.alarmapp
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.FlutterPlugin
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(binding: FlutterEngine) {
super.configureFlutterEngine(binding)
val plugin = AlarmScheduler()
plugin.onAttachedToEngine(binding)
}
}
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10'
implementation 'io.flutter:embedding-android:3.16.5
implementation 'io.flutter:flutte:3.16.5'
implementation 'io.flutter.plugins:flutter_plugin_android_lifecycle:2.0.0'
}
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:checkReleaseAarMetadata'.
> Could not resolve all files for configuration ':app:releaseRuntimeClasspath'.
> Could not find io.flutter:embedding-android:3.0.5.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/io/flutter/embedding-android/3.0.5/embedding-android-3.0.5.pom
Required by:
project :app
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
> Could not find io.flutter:flutte:3.16.5.
Searched in the following locations:
- https://repo.maven.apache.org/maven2/io/flutter/flutte/3.16.5/flutte-3.16.5.pom
Required by:
project :app
> Could not find io.flutter.plugins:flutter_plugin_android_lifecycle:2.0.0.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/io/flutter/plugins/flutter_plugin_android_lifecycle/2.0.0/flutter_plugin_android_lifecycle-2.0.0.pom
Required by:
project :app
要解决 Android 项目中对 FlutterPlugin 的未解析引用,您可以包含 Flutter SDK 中的 flutter.jar 作为依赖项。具体方法如下:
在 Flutter SDK 路径中找到 flutter.jar 文件:
flutter-sdk-path/bin/cache/artifacts/engine/android-arm64/flutter.jar
将flutter.jar复制到项目的libs目录中:
android/src/main/libs/
(libs目录不存在则创建)。
更新您的构建。 gradle 文件以将 flutter.jar 包含在依赖项中:
依赖项{ 实现文件('src/main/libs/flutter.jar') }
这应该修复对 FlutterPlugin 的未解析引用。