Repository: flutter
Issue: MissingPluginException thrown when setting up the add-to-app platform channel
Labels: platform-android,engine,d: api docs,a: existing-apps,P2,team-android,triaged-android

pubspec.yaml

```
version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter_easyrefresh: ^2.1.1
  flutter_swiper : ^1.1.4
  flutter:
    sdk: flutter
```
flutter channel --> master

**1.When I initialize the MethodChannel in the FragmentActivity configureFlutterEngine**
```
class MyFlutterActivity : FlutterActivity() {
    private val CHANNEL_NETWORK = "native_network"
    private val CHANNEL_LOG = "native_log"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            CHANNEL_NETWORK
        ).setMethodCallHandler {
            // Note: this method is invoked on the main thread.
                call, result ->
            //call.method是获取调用的方法名字
            //call.arguments 是获取参数
            if (call.method == "test") {
                val batteryLevel: String = "hello kotlin"
                if (batteryLevel != null) {
                    result.success(batteryLevel) // 相当于是调用方法的return
                } else {
                    result.error("UNAVAILABLE", "Battery level not available.", null) // 告诉调用者失败了
                }
            } else {
                result.notImplemented() // 告诉调用者没有此方法，避免一直等到阻塞在这
            }
        }

        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            CHANNEL_LOG
        ).setMethodCallHandler(MethodCallHandler { call, result ->
            var tag: String = call.argument("tag") ?: "flutter"
            var message: String = call.argument("msg") ?: ""
            when (call.method) {
                "logV" -> Log.v(tag, message)
                "logD" -> Log.d(tag, message)
                "logI" -> Log.i(tag, message)
                "logW" -> Log.w(tag, message)
                "logE" -> Log.e(tag, message)
            }
            result.success("success");
        })
    }

}
```
**2.I use the Cache Engine in my app**

```
class App:Application() {
     lateinit var flutterEngine : FlutterEngine

    /**
     * 这里有坑，如果dark中有调用native的代码，此处应该会去初始化dark代码，从而报错
     * 即你在原生注册的方法MethodChannel不会生效，
     */
    override fun onCreate() {
        super.onCreate()
        flutterEngine = FlutterEngine(this)
        flutterEngine.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint.createDefault()
        )
        FlutterEngineCache
            .getInstance()
            .put("my_engine_id", flutterEngine)
    }
}
```
**3.And then I start flutter somewhere in my app**
```
  var intent = Intent(context, MyFlutterActivity::class.java)
        intent.putExtra("cached_engine_id", "my_engine_id")
        intent.putExtra("background_mode", FlutterActivityLaunchConfigs.BackgroundMode.transparent.name)
        startActivity(intent)
```
**4.The code in the dark is normal, I won't list it**

**5.Then I get the following error**
```
E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: MissingPluginException(No implementation found for method logD on channel native_log)
    #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:157:7)
    <asynchronous suspension>
    #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:332:12)
    #2      LogUtils.d (package:fluttermodule/util/LogUtils.dart:11:13)
    #3      MinePageState.initState (package:fluttermodule/module/home/MinePage.dart:43:14)
    #4      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4649:58)
    #5      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4485:5)
    #6      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3455:14)
    #7      Element.updateChild (package:flutter/src/widgets/framework.dart:3223:18)
    #8      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4536:16)
    #9      Element.rebuild (package:flutter/src/widgets/framework.dart:4227:5)
    #10     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4490:5)
```

**6.When I initialize the MethodChannel in my App, this problem doesn't arise**

```
class App:Application() {
     lateinit var flutterEngine : FlutterEngine
    private val CHANNEL_NETWORK = "native_network"
    private val CHANNEL_LOG = "native_log"
    /**
     * 这里有坑，如果用缓存engine,则应在此处初始化MethodChannel,
     * 而不是FlutterActivity中的configureFlutterEngine方法
     */
    override fun onCreate() {
        super.onCreate()
        flutterEngine = FlutterEngine(this)
        flutterEngine.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint.createDefault()
        )
        FlutterEngineCache
            .getInstance()
            .put("my_engine_id", flutterEngine)

        initMethodChannel()
    }

    private fun initMethodChannel() {
        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            CHANNEL_NETWORK
        ).setMethodCallHandler {
                call, result ->
            if (call.method == "test") {
                val batteryLevel: String = "hello kotlin"
                if (batteryLevel != null) {
                    result.success(batteryLevel) // 相当于是调用方法的return
                } else {
                    result.error("UNAVAILABLE", "Battery level not available.", null) // 告诉调用者失败了
                }
            } else {
                result.notImplemented() // 告诉调用者没有此方法，避免一直等到阻塞在这
            }
        }

        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            CHANNEL_LOG
        ).setMethodCallHandler(MethodChannel.MethodCallHandler { call, result ->
            var tag: String = call.argument("tag") ?: "flutter"
            var message: String = call.argument("msg") ?: ""
            when (call.method) {
                "logV" -> Log.v(tag, message)
                "logD" -> Log.d(tag, message)
                "logI" -> Log.i(tag, message)
                "logW" -> Log.w(tag, message)
                "logE" -> Log.e(tag, message)
            }
            result.success("success");
        })
    }
}
```