Flutter - Import from existing android project

后端 未结 5 1674
[愿得一人]
[愿得一人] 2020-12-29 22:25

I am currently working on an android project which is ~75% complete. I need a similar application for iOS.

Can I import this project to flutter? How?

Also, w

5条回答
  •  一生所求
    2020-12-29 23:15

    Flutter applications work completely different, i.e. not like Android nor like iOS. Flutter has its own architecture and therefore own plugins etc.

    Summary

    No, you cannot convert your existing Android code into Dart code for Flutter, which is necessary to enable cross-platform behavior because all code used by the Flutter SDK has to be written in Dart.

    However, you could make use of your Android code by just including it in the Android folder and trying to integrate it with the Flutter SDK, which will probably be all lot more work than just rewriting it entirely in Dart. This is also not what you generally want to do because native Android and iOS code make most sense to be used to enable features that you cannot achieve with Flutter, in the form of plugins.

    There is also a third approach, which is that you keep your Android code and only add features with Flutter. This is harder to implement because you will have to create an interface to "switch" to Flutter from Android code. Using this you would also need to rewrite your remaining parts for iOS in either Flutter or native iOS, which does not help you at all.

    Elaboration

    There is a pretty straight forward answer to this.

    Flutter is a layer on top of the native SDK's. This means that you could keep your code because, if you ever took a look into a Flutter project, you would see that in it there are, at the moment, essentially three structures you can work with:

    • lib folder for Dart code + files Flutter needs to e.g. include plugins, short: Dart code for Flutter

    • iOS folder for either Objective-C or Swift code, which also includes the usual iOS accessories

    • android folder for either Java or Kotlin (or C++) code and all other gradle files etc.

    This means that you can use native code if you want to, but you do not need to do it.

    You can completely ignore the android and ios folders during development if you want to`.

    Most likely you will just be modifying aspects like the app icon, name, version name, license keys etc. in your native code.

    Flutter's goal is to write all code in Dart and only in the Flutter SDK, which means that your existing Android code could be included in the project, but it will not have any use for iOS because all cross-platform work is done in Dart making use of the Flutter SDK.

    Conclusion

    If you really want to switch to the Flutter SDK, your best choice is entirely switching to Flutter, i.e. rewriting all features in Dart.

    There is no "import" possibility to "convert" Android code to Flutter code. This would not even work if they were written in the same language because the Flutter architecture is completely different and the way you build apps is not remotely related to Android applications.

    If you think that learning Dart and rewriting your application for cross-platform is not worth it but you still want iOS support, I would suggest you to write in iOS native, but my general suggestion for the problem is rewriting the whole application in Dart.

提交回复
热议问题