Loading

安卓原生开发问题.md

原生安卓开发问题

RecylerView在ScrollView内部,显示不全

  1. 更改ScrollViewandroidx.core.widget.NestedScrollView
  2. RecylerView添加属性android:nestedScrollingEnabled="false"

Realme手机 “安装包异常”

gradle.properties中增加android.injected.testOnly=false即可

Android Studio无法调试

报错如下:

Cannot debug application from module cbuni-mobile-android.app on device realme-*. This application does not have the debuggable attribute enabled in its manifest. If you have manually set it in the manifest, then remove it and let the IDE automatically assign it. If you are using Gradle, make sure that your current variant is debuggable.

解决:

Build Variants(IDE左下角)中,release改为debug

安卓和Flutter通信

原生安卓启动flutter,之间的通信可以使用:

  1. route:安卓启动flutter时需要传递route,可以使用它来传递数据
  2. EventChannel: EventChannel仅支持数据单向传递,无返回值。
  3. MethodChannel:MethodChannel支持数据双向传递,有返回值。
  4. BasicMessageChannel: BasicMessageChannel支持数据双向传递,有返回值。

MethodChannel

安卓:

flutterEngine = new FlutterEngine(this);
flutterEngine.getNavigationChannel().setInitialRoute(initPath);
flutterEngine.getDartExecutor().executeDartEntrypoint(
    DartExecutor.DartEntrypoint.createDefault()
);
// 设置flutter回调函数
// 管道名
MethodChannel methodChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "Channel");
// 设置方法回调函数
methodChannel.setMethodCallHandler((call, result) -> {
    //判断方法名是否支持
    if (call.method.equals("getUserName")) {
        // 返回用户名
        String userName = Constant.USER_NAME;
        result.success(username);

    } else {
        //方法名暂不支持
        result.notImplemented();
    }
});

flutter:

// 初始化管道
MethodChannel platform = MethodChannel("Channel");
// 调用安卓方法
handleButtonClick() async {
    String s;
    try {
        s = await platform.invokeMethod("getUserName");
    } catch (e) {
        s = e.toString();
    }
    debugPrint("UserName is " + s);
    return s;
}

Flutter引擎预加载导致提前调用安卓方法

flutter预加载时,就会开始调用flutter的build

flutterEngine = new FlutterEngine(this);
flutterEngine.getNavigationChannel().setInitialRoute(initPath);
flutterEngine.getDartExecutor().executeDartEntrypoint(
    DartExecutor.DartEntrypoint.createDefault()
);
FlutterEngineCache.getInstance().put(engineId, flutterEngine); // 此时就会加载flutter代码,如果flutter中有methodchannel通信,此时就已经开始进行通信调用了
posted @ 2022-06-23 16:35  nsfoxer  阅读(84)  评论(0编辑  收藏  举报