前言
今天在编译项目的时候,突然留意到控制台输出的一段日志,大概意思就说:尝试注册插件,但是该插件已经在此 FlutterEngine 中注册了...
详细日志:
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (dev.fluttercommunity.android_id.AndroidIdPlugin@d3d07b4) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.example.appsettings.AppSettingsPlugin@48d4fdd) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (de.bytepark.autoorientation.AutoOrientationPlugin@6e8752) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (dev.fluttercommunity.plus.connectivity.ConnectivityPlugin@7b6ca23) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.deviceinfo.DeviceInfoPlugin@47ceb20) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (dev.fluttercommunity.plus.device_info.DeviceInfoPlusPlugin@3940cd9) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.firebase.analytics.FlutterFirebaseAnalyticsPlugin@ac9c69e) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.firebase.core.FlutterFirebaseCorePlugin@ea1e57f) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.firebase.crashlytics.FlutterFirebaseCrashlyticsPlugin@e15994c) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.firebase.dynamiclinks.FlutterFirebaseDynamicLinksPlugin@af9d95) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin@307aeaa) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.fluttercandies.flutter_image_compress.ImageCompressPlugin@2f95a9b) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.pichillilorenzo.flutter_inappwebview.InAppWebViewFlutterPlugin@ca8fe38) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin@8b97e11) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin@d8a0b76) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.github.ponnamkarthik.toast.fluttertoast.FlutterToastPlugin@6890577) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.baseflow.geocoding.GeocodingPlugin@90bc5e4) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.baseflow.geolocator.GeolocatorPlugin@2a6ea4d) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.googlemaps.GoogleMapsPlugin@1036902) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.imagepicker.ImagePickerPlugin@1fe8213) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (dev.flutter.plugins.integration_test.IntegrationTestPlugin@2b15c50) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.ljk.leak_detector.LeakDetectorPlugin@584de49) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (dev.fluttercommunity.plus.packageinfo.PackageInfoPlugin@ac3134e) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.pathprovider.PathProviderPlugin@1452c6f) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.baseflow.permissionhandler.PermissionHandlerPlugin@ad7ed7c) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.fluttercandies.photo_manager.PhotoManagerPlugin@2341605) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.sentry.flutter.SentryFlutterPlugin@bc1165a) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.share.SharePlugin@9a2208b) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin@6746568) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.tekartik.sqflite.SqflitePlugin@ce50d81) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.umeng.umeng_common_sdk.UmengCommonSdkPlugin@a6a3e26) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.urllauncher.UrlLauncherPlugin@5903a67) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.example.video_compress.VideoCompressPlugin@2de7014) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.videoplayer.VideoPlayerPlugin@25400bd) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (creativemaybeno.wakelock.WakelockPlugin@52c16b2) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (io.flutter.plugins.webviewflutter.WebViewFlutterPlugin@25c1603) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
W/FlutterEngineCxnRegstry(27539): Attempted to register plugin (com.monitor.x_apm.XApmPlugin@93c7980) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@157bb66).
D/FlutterGeolocator(27539): Creating service.
D/FlutterGeolocator(27539): Binding to location service.
D/FlutterGeolocator(27539): Geolocator foreground service connected
D/FlutterGeolocator(27539): Initializing Geolocator services
D/FlutterGeolocator(27539): Flutter engine connected. Connected engine count 1
排查问题
首先找到我们的程序入口,看看这些代码是哪儿出了问题?
import android.content.Context
import io.flutter.app.FlutterApplication
class BaseApplication : FlutterApplication() {
companion object {
lateinit var context: Context
}
override fun onCreate() {
super.onCreate()
context = this
}
}
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this.flutterEngine!!)
}
}
以上就是具体的代码实现,看上去并没有什么问题,于是再进一步分析...
如果没有手动配置automaticallyRegisterPlugins属性的话,则默认值为true,也可以在AndroidManifest.xml文件中进行配置手动关闭自动注册插件属性,具体代码如下:
<meta-data
android:name="io.flutter.automatically-register-plugins"
android:value="false" />
也就是说在没有手动配置情况下,程序入口初始化的时候就默认设置自动注册插件的属性了,莫非是因为这个原因才重复注册了插件?带着疑惑继续探索...
再来看看FlutterActivity的实现
不难看出,Flutter引擎其实是在FlutterActivity中进行初始化的,如果缓存引擎id不为空的话会去获取缓存的引擎,否则会去获取自定义引擎,如果自定义引擎也为空,则会生成一个默认引擎。
然而默认引擎的automaticallyRegisterPlugins属性值为false...
在Flutter引擎中自动注册插件是否生效,取决于两个条件进行判断,是由构造函数中的传递的参数和在AndroidManifest中配置的参数同时为true时才生效。既然需要同时为true才生效,那么也就排除这个原因了...
再回过头来看FlutterActivity中有这么一段代码:
看完之后豁然开朗,原来是因为我们自己又在onCreate中手动调用了注册插件的方法,所以重复注册插件了...
解决问题
删除我们的注册插件方法,或者重载configureFlutterEngine函数自行注册插件即可
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(this.flutterEngine!!)
}
}
或者
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
}
}