为什么我需要这个?
我正在创建一个 Flutter 包(适用于 Android),并且我想在一个单独的 AAR 文件中创建 MethodCallHandler
的代码,该文件可以添加到我的插件代码中
所以我会:
pluginNativeCode/android/MyPlugin.kt
import ir.malv.android.flutter.MySeparateMethodCallHandler
class MyFlutterPlugin: FlutterPlugin {
override fun onAttachedToEngine(...) {
// Here's the source of the problem
channel.setMethodCallHandler(MySeparateMethodCallHandler())
}
}
MySeparateMethodCallHandler
不在 plugin/android
部分中,它作为 gradle 依赖项导入
但是在该库中我无法访问 Flutter 的代码,例如 MethodCallHandler
类。
Unity 和 React Native 中的类似行为
In react-native there is
com.facebook.react:react-native:+
that can be added ascompileOnly
to get the needed react-native codes
In unity we have
unity.jar
file which contains unity native codes and can be added ascompileOnly
as well to provide engine's native APIs
Flutter 中有什么功能可以做到这一点?
是否有一个依赖项可以作为compileOnly 包含并使用它来获取所需的类并在单独的项目中创建我的aar
文件?
最佳答案
解决方案如下:
- 将此 Remote 添加到
build.gradle
的存储库
allprojects {
repositories {
// .. rest of remote urls
maven { url("http://download.flutter.io/")}
}
}
- 将此依赖项添加到模块的
build.gradle
dependencies {
compileOnly("io.flutter:flutter_embedding_debug:1.0.0-0fdb562ac8068ce3dda6b69aca3f355f4d1d2718")
// rest
}
注意compileOnly
。这将避免类重复。插件只需要它。 flutter 应用程序将自行提供。
What is
0fdb562ac8068ce3dda6b69aca3f355f4d1d2718
?
It's the engine version that is good to match the version you are developing the plugin with.
在您的代码中,您将可以访问所需的类,例如 MethodChannel
、MethodChannel.Result
和 ...
奖励:我如何知道我正在使用什么引擎制作插件?
我认为,如果您只需要像 MethodChannel
这样的类来调用方法,那应该没有关系,但这里有一种使用 Android Studio 获取它的方法
- 使用 Android studio(已激活 Flutter 插件)打开插件的
project/example/android
- 运行
gradle plugin_name:dependencies
任务并查找flutter_embedding_
短语。
关于android - 在 android 库中添加 Flutter 功能作为依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67822752/