Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support adding a Flutter add-to-app module to a Flutter-created app #64542

Open
chandarlee opened this issue Aug 25, 2020 · 27 comments
Open

Support adding a Flutter add-to-app module to a Flutter-created app #64542

chandarlee opened this issue Aug 25, 2020 · 27 comments
Labels
c: new feature Nothing broken; request for a new capability c: proposal A detailed proposal for a change to Flutter engine flutter/engine repository. See also e: labels. P3 Issues that are less important to the Flutter project team-engine Owned by Engine team tool Affects the "flutter" command-line tool. See also t: labels. triaged-engine Triaged by Engine team

Comments

@chandarlee
Copy link

Use case

I have been working on a SDK project which is written with flutter. We implement our common business logic with flutter, and then provide Android/iOS native interfaces which just forward API request from native to flutter to work.
Then we build and assemble all the things together(generated libapp.so, flutter asserts and else), deliver as SDK in the form of AAR and Framework to developers.

To integrate the SDK to the projects which do not use flutter , it is simple and just works great.

But for those project which also use flutter, it does not work. As our SDK already has its own libapp.so, and the target project will also generates its own libapp.so too, but flutter engine does not support load two or more aot library simultaneously.

For now or future, our sdk just cannot be open source, so we cannot provide a flutter package to use. So we have to figure out another way to work out this situation.

Proposal

  • Flutter engine support load two or more aot library simultaneously
  • Flutter build tools support merge aot library to a whole one. Or support merge some kind of intermediate artifacts of aot library.
  • Any other better ways?
@pedromassangocode pedromassangocode added engine flutter/engine repository. See also e: labels. c: proposal A detailed proposal for a change to Flutter c: new feature Nothing broken; request for a new capability tool Affects the "flutter" command-line tool. See also t: labels. labels Aug 25, 2020
@jmagman
Copy link
Member

jmagman commented Sep 1, 2020

Similar to #39707, though it's not quite the same. This proposal is to add a Flutter module to a Flutter app, and that one is to add multiple modules to a native app.

@jmagman jmagman added this to Awaiting triage in Mobile - Add-to-app review via automation Sep 1, 2020
@jmagman jmagman changed the title Support build and run an Android/iOS native application which itself uses the flutter and also has an external flutter written sdk dependency at the same time Support adding a Flutter add-to-app module to a Flutter-created app Sep 1, 2020
@jmagman jmagman moved this from Awaiting triage to Engineer reviewed in Mobile - Add-to-app review Sep 1, 2020
@jmagman jmagman added this to Awaiting triage in Tools - existing app integration review via automation Sep 1, 2020
@jmagman jmagman moved this from Awaiting triage to Engineer reviewed in Tools - existing app integration review Sep 1, 2020
@wanjm
Copy link

wanjm commented Sep 7, 2020

I also need this. any updates?

@chandarlee
Copy link
Author

chandarlee commented Sep 7, 2020

Similar to #39707, though it's not quite the same. This proposal is to add a Flutter module to a Flutter app, and that one is to add multiple modules to a native app.

It is quite different from adding a Flutter module to a Flutter app which is integrated in source code level,that means Flutter app can touch the flutter module‘s dart codes。In my situation,the target flutter app can only integrate my SDK,the SDK contains no dart source code,but the libapp.so that is the AOT shared library。

@jmagman
Copy link
Member

jmagman commented Sep 10, 2020

@chandarlee it's similar in that it needs to handle the problem Flutter engine support load two or more aot library simultaneously as you pointed out. But it's not the same, so this wasn't closed as a duplicate.

@wanjm
Copy link

wanjm commented Oct 28, 2020

we lost one custom for their app is written in flutter and our SDK was written in flutter and android;

@jmagman
Copy link
Member

jmagman commented Oct 28, 2020

@wanjm Can you ship your functionality as a Flutter plugin instead? Flutter plugins contain dart and native code, and are made to be consumed by other Flutter apps.

@wanjm
Copy link

wanjm commented Oct 29, 2020

out sdk can't be open sourced;

@jmagman
Copy link
Member

jmagman commented Oct 29, 2020

You can distribute the native code or FFI as a binary but you're right, I don't think you can with the dart code.

@wanjm

This comment has been minimized.

@chandarlee
Copy link
Author

I hope flutter team put high priority on this one;

For now, I have found a workaround for Android which need to load the flutter engine a second time , and lead to much more memory consume. Beyond that , everything just work fine. For iOS to work, I am still trying.

@wanjm
Copy link

wanjm commented Nov 3, 2020

@chandarlee could you please share me the steps and how to build it;

@daigocy

This comment has been minimized.

@zanderso
Copy link
Member

zanderso commented Dec 3, 2020

/cc @gaaclarke @cbracken

@zanderso zanderso added the P3 Issues that are less important to the Flutter project label Dec 3, 2020
@bottee
Copy link

bottee commented Feb 25, 2021

I'm also very much interested in this feature.

@chandarlee, could you share the basic idea of the android workaround you found? This would be very much appreciated! And did you meanwhile find a solution for iOS?

@jmagman
Copy link
Member

jmagman commented Feb 25, 2021

Given #72009 and https://github.com/flutter/samples/tree/master/add_to_app/multiple_flutters this may just be a request for additional documentation to special-case Flutter modules when the host app is also using Flutter.

@gaaclarke does that sound right, or would there be additional work needed?

@gaaclarke
Copy link
Member

gaaclarke commented Feb 25, 2021

@jmagman No, this sounds like it would need some tooling support and documentation. For example, a flutter module is going to try to incorporate the flutter dynamic library and the flutter app is going to try the same thing.

Multiple-flutters just helps you launch multiple instances of the same flutter module (with different entry-points) in a low memory / time cost way. It doesn't do any tooling work to support multiple flutter projects on disk.

@bottee
Copy link

bottee commented Feb 27, 2021

Great to see activity on this issue!

@gaaclarke could you please shed some light about the effort and time horizon it needs to solve the issue? As this issue is very important for us, I could also work on that issue if you could point me towards the places where the work is to do.

@chandarlee
Copy link
Author

@wanjm @bottee I am sorry to be late for response. My workaround on Android is a little bit hack and memory consumed. The main point is a custom ClassLoader. Follows are the key steps:

Rename the generated libapp.so to libapp-new.so and libflutter.so to libflutter-new.so, this is to avoid file conflict with host application which also built with flutter. It is easy to do this either by gradle or manually.
Define a class extends FlutterLoader, eg named CustomFlutterLoader. In this class, override the Initialization logic to load library libflutter-new.so, and set the aot-share-library to our libapp-new.so with --aot-shared-library-name shell args options. FlutterLoader has just hard code that , we need to fix that.
Define a custom DexClassLoader to wrap the flutter_embedding jar. You need transform flutter_embedding jar to dex, and load the dex into DexClassLoader at runtime.
User the custom DexClassLoader to initialize the flutter engine. Use Reflection to create your CustomClassLoader instance to start initialize, and create a flutte engine instance. After this, the flutter engine is bind to your libflutter-new.so and execute the aot dart code in libapp-new.so.
In this way , we have create a AndroidShellHolder instance in native, which bind to our flutter engine and dart code. And be isolated from the main application.
There still are some implementation details need to be pay attention to , You can just run it up and fix them.

For iOS platform , I am still working on that.

I think if flutter engine support binding to one or more aot-shared-library or kind of tool to merge them when building apk/ipa, it is easy to fix the problem.

@caio-lc-coelho
Copy link

Any news on this issue? Really need it.

@gaaclarke
Copy link
Member

gaaclarke commented Apr 12, 2021

There isn't anyone working on this as far as I know. I'd interpret that as people deemed this lower priority than the other issues, not that they don't think it's a legitimate issue.

May I propose another avenue to get what you want? You have Flutter code you want to provide to customers, but you don't want to be open source, correct? If you were a Unity developer making a plugin, you'd have 2 options: 1) Ship the source code with your plugin 2) Ship a .dll file with your plugin. The dll is a compiled artifact, that will get compiled again, this time for AOT, when the final app is built. The Dart equivalent would be a Kernel, which I think the Dart team has explicitly stated isn't stable. You might want to see if you can get them to support plugins that have Kernels tied to a specific version of Dart. That way you could distribute your compiled code as a plugin, but you'd have to say something like it only works for Flutter 2.0.2 (you could supply multiple version to download).

edit: FWIW I posed the question about a stable Kernel to the Dart team and standardizing an interface for the VM isn't something they're working on. The language is still changing a lot under the hood. That means allowing a kernel plugin tied to a specific version would be the best bet.

@wanjm
Copy link

wanjm commented Aug 12, 2021

is there any way to minify the dart code?

@zhgqthomas
Copy link

Hello @xster , I saw your github profile that you're one of the flutter contributor. Do you by any chance have some information on this issue?

@xster
Copy link
Member

xster commented Sep 22, 2022

I don't have more insights into this issue. @gaaclarke's comment is the right one imo. We either figure out a way to add a dependency restriction to the module itself and require a specific version of the engine and ditch the flutter.so from the module and have the app's flutter.so somehow load 2 libapp.so (difficult), or ship pre-built dart code instead of shipping pre-built modules (also difficult). @mossmana or @leighajarett are likely the right people to evaluate the investment there.

@mossmana
Copy link
Contributor

mossmana commented May 4, 2023

FYI @reidbaker

@soham-sharpsell

This comment was marked as off-topic.

@amelmusic
Copy link

any news on this front? I believe since flutter is used more and more, this is quite important. We must have a way to develop module in flutter, pack it as AAR and 3rd party could just use our AAR and integrate. Unfortunately, there are lot's of situations where developers don't want to share source code in plain text.

At least is there some minifier / obfuscator which could change our dart code so that it's hard to read?

@yeyuchen198
Copy link

同求,要是flutter能像luajava那样,在lua脚本中直接调用jar和so就好了,但是现在好像只有platform channel?很麻烦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: new feature Nothing broken; request for a new capability c: proposal A detailed proposal for a change to Flutter engine flutter/engine repository. See also e: labels. P3 Issues that are less important to the Flutter project team-engine Owned by Engine team tool Affects the "flutter" command-line tool. See also t: labels. triaged-engine Triaged by Engine team
Projects
Mobile - Add-to-app review
  
Engineer reviewed
Development

No branches or pull requests