问题原因
很明显就是依赖没有,但是不是flutter的依赖,是Android native缺少这个方法。
如果你不是直接在flutter工程生成的Android工程,或者是由其他方式使用flutter依赖时,当flutter工程引入部分带有native插件的第三方库时,就不会主动打到对应的Android工程中,引起改错误。
解决方案
方案一、将navtive插件当依赖引入到Android 工程,例如:
//magpie Android SDK debug
debugApi 'com.wuba.magpie:magpie_debug:2.0.0'
方案二、(推荐)使用fat-aar将所有的插件包打入同一个aar中
1、在工程build.gradle中添加classpath
classpath 'com.kezong:fat-aar:1.2.15'
2、在Flutter module中的build.gradle中添加plugin
apply plugin: 'com.android.library'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.kezong.fat-aar'
3、在Fluuter module的build.gradle中添加依赖,可以使用条件语句剔除部分不需要的插件
dependencies {
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}
plugins.each { name, _ ->
println name
def content = name.toString();
if (content.indexOf("macos") == -1
&& content.indexOf("linux") ==-1)
embed project(path: ":$name", configuration: 'default')
}
}
4、打包aar
flutter build aar