The official guide on how to Add a Flutter screen to an Android app explains how to add a simple page but what about a screen which gets data from native side through flutter channel.
Issue with native calls inside flutter aar : –
After converting module into flutter aar , native code is not included hence native call wont work.
How to add native code / flutter channel code in flutter aar ?
2
Answers
You may missing some steps.
Follow the instruction:
add-the-flutter-module-as-a-dependency
After running
flutter build aar
you need to do more config as the console hints.Flutter will make a local repository for you, and you need to copy it’s path add to your build file.
And then add the dependency to your host app.
Hints would be like:
So your issue is that you try to create aar via the flutter module aar creation means, and you are correct, but only in cases when the flutter part is a standalone module not connected to your app in any way – including the platform channels. There is no way to overcome it currently – at least, I didn’t find any.
You can achieve what you want, though. What you should do is create a wrapper Android native module that imports the flutter module and communicates with it via channels. In this native wrapper module, the flutter module should be injected via source code rather than by aar, as you understand. After that, create an Android(java, kotlin) interface that commutes your main app messages to/from the wrapper module in a needed way and which is exposed in a needed way from the wrapper module.
After that, you just create an aar from the Android wrapper module and import it as an aar library into your main project using the exposed interface as the medium of communication.
I’ve done it all, and it works – the only drawback is debugging and developing both flutter and native code simultaneously is tricky – as you understand, debugging flutter code from AAR is not possible, and flutter has to be built every time. You can overcome that issue also by having two different importing strategies for debug and release – so for release, you import aar, but for debug, you import the android wrapper module with the flutter module inside as a pure code – it would require also importing of the flutter itself to your main project, but I figure it is an acceptable bargain for such flexibility – moreover, you could delete it in some gradle script down the release building lane.
Also to build that Android wrapper module with the flutter inside into aar you would need to adjust the build scripts and flutter tools like this:
And inside this
flutter_module_library.gradle
, which you would have to store in you repo you would have to copy and paste theflutter_tools/gradle/flutter.gradle
but with places where the error will pop during build fixed – basically the flutter tools build script doesn’t know how to inject dependencies into the library projects – only into the app projects. I don’t remember exact places now – but they will pop up during the first build.The ios module builds without any major dancing around(as far as I remember)
As you can see, it is not a trivial task and, I would say, hardly justified – but it gives you relative power and flexibility. And experience))