I haven’t done Android development for a while. In order to keep my understanding of Android, I have reviewed some new knowledge of Android. I found that flutter is becoming more and more popular now. I have heard of flutter before but never tried it. Recently, I spent some time learning flutter and DART. After several days of study and understanding, I also had some superficial understanding of Flutter. I think that if I use Flutter in Android development in the future, I may not use DART code to solve all the front-end interface and background network requests. It is more likely that the front-end interface uses Flutter and the background network requests and other operations will still use the original Android Native system. After all, many companies have developed mature and stable solutions for Android over the years. Therefore, I have studied the communication method and mechanism between Flutter and Android Native in detail. Generally speaking, the communication between Flutter and Android Native is relatively simple. However, if all the background operations rely on Android Native code, you will find that there is a lot of redundant code after writing more code for Flutter and Android Native communication processing. It’s annoying to write too much. Therefore, I came up with the idea of using annotation and APT mechanism to generate the communication setting codes of Flutter and Android Native before compilation, and only write business codes during code development.
General communication process between Flutter and Android Native:
Called on the DART side
/ / create the channel
static const channel = const MethodChannel("xxxxxxxx/channelName");
void _doLogin() async{......final result = await channel
.invokeMethod("doLogin", {"userName":_username, "password":_pwd}); ......}Copy the code
Java server-side processing
The above Java side code deals with the communication between flutter and Android Native. The business side logic is a piece of each case. In addition, the implementation of case also considers the resolution of the parameters of flutter, the creation of asynchronous call processing and AsyncTask, which can also be automatically generated in APT.
Scenarios using annotations:
MethodChannelHandler
AsyncMethodCall
AsyncTask
doInBackgroup
onPostExecute
FlutterChannelFactory.register(this);
- There is no need to write template code for a Flutter Channel.
- Easy native Junit testing without relying on the Flutter API and Android API.
- Avoid too many switches when handling too many methods… case… Code block, easier to read.
Code reference: Flutter_channel_annotation
conclusion
This is a simple practice to simplify Flutter communication with Android Native using annotations and APT. This is also my reflection on flutter learning in a few days. I may not have mature ideas or there may be a better way. I also need to learn more about flutter in depth. In addition, the front-end flutter+channel handler+ back-end Android Native is directly the implementation of MPV mode. The structure of a flutter as a View, a channel handler as a Presenter, and other native codes as models is clear.