安卓原生开发问题.md
原生安卓开发问题
目录
RecylerView在ScrollView内部,显示不全
- 更改
ScrollView
为androidx.core.widget.NestedScrollView
- 在
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,之间的通信可以使用:
route
:安卓启动flutter时需要传递route
,可以使用它来传递数据EventChannel
:EventChannel
仅支持数据单向传递,无返回值。MethodChannel
:MethodChannel
支持数据双向传递,有返回值。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通信,此时就已经开始进行通信调用了