flutter调用Android原生logcat打印日志
Posted yongfengnice
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flutter调用Android原生logcat打印日志相关的知识,希望对你有一定的参考价值。
//2.本地kotlin代码
class MainActivity : FlutterActivity() {
companion object { const val FLUTTER_android_LOG_CHANNEL = "flutter_android_log" } override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) GeneratedPluginRegistrant.registerWith(flutterEngine) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, FLUTTER_ANDROID_LOG_CHANNEL) .setMethodCallHandler { call, result -> var tag: String = call.argument("tag") ?: "LogUtils" var message: String = call.argument("msg") ?: "unknown log message" when (call.method) { "logD" -> Log.d(tag, message) "logE" -> Log.e(tag, message) } result.success(null) } } }
//2.flutter的dart代码 下划线_表示私有变量
import \'package:flutter/services.dart\';
class LogUtils {
static const String _tag = "LogUtils";
static const _perform = const MethodChannel("flutter_android_log");
static void d(String message) {
_perform.invokeMethod("logD", {\'tag\': _tag, \'msg\': message});
}
static void e(String message) {
_perform.invokeMethod("logE", {\'tag\': _tag, \'msg\': message});
}
}
//3.调用
void _onPush() {
LogUtils.e("_onPush press");
}
注意坑:与native方法有交互时,最好先Stop掉,再Start。热启动很多时候无效,坑死了。。。
以上是关于flutter调用Android原生logcat打印日志的主要内容,如果未能解决你的问题,请参考以下文章
从原生 Android 主屏幕小部件调用 Flutter (Dart) 代码
Flutter学习日记之Flutter与原生Android的互相调用