Flutter - Import from existing android project

2019-03-15 14:17发布

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, will flutter supports the libraries I have used in this project?

5条回答
Fickle 薄情
2楼-- · 2019-03-15 14:46

Can I import this project to flutter? How?

As of I have researched so far, there is no import option available in flutter for projects from any other framework. Flutter architecture is completely different and new as compared to others. We can include our existing android code in our 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.

will flutter supports the libraries I have used in this project?

No, because flutter provides its own libraries written in Dart. Day by day they are adding new libraries and we need to use that.

So, we need to write the complete project in dart language and use flutter existing libraries and build a new project.

查看更多
3楼-- · 2019-03-15 14:57

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.

查看更多
再贱就再见
4楼-- · 2019-03-15 14:57

I would say no, there isn't. And the reason is well explained by @creativecreatorormaybenot. I would add that is is not just the language difference that makes it challenging but the whole android native views, view hierarchy is different from Flutter's view hierarchy. If you are familiar with Cocos framework, which has its own View hierarchy drawn, flutter is similar to that only better, rich and attested by Google(I guess, ;) ). The launch point is still regular android activity in flutter, but that's about it. Then the control flows to the flutter for measuring and drawing the UI Widgets. You might very well still need to implement some features that require system services, hardware access to be written in Native Java/Kotlin android style/ IOS native style.

Recommendation: At this point, I would not recommend you to use a hackish way to convert dart to java/kotlin and Android UI to Flutter UI even if you see something online that does it. The reason being, Flutter itself is an experiment by Google and is not yet mature as Android. You should expect some nuances to be changed added frequently. Since you are already 75 percent done, I would say go all native(Android and IOS) or all Flutter.

To precisely answer your question: Can I import this project to flutter? Not at the moment.

Also, will flutter supports the libraries I have used in this project? Kind of. Since you have not mentioned any of the libraries that you have used, I am going to take a few examples to make it clearer: - Image downloading library(glide, picasso) may be replaced by flutter's native Image widget - If you are using any core java libraries that you want to port to Flutter, you can follow the flutter's documentation about developing packages and plugins which essentially says to declare contract/apis in dart and imlementation java packages in 'android' folder. Contract remains same for IOS and android. - If you want to use system level components, like I mentioned earlier, packages and plugins are the way to go. Here is the list of first hand plugins developed by Dart team.

查看更多
5楼-- · 2019-03-15 15:02

From what I've seen, you're probably going to have to port it to Dart for the most part. However, since your 75% probably includes a lot more than coding (such as the design, the interaction, the flow), the easy part will be recoding that in Dart to finish.

查看更多
\"骚年 ilove
6楼-- · 2019-03-15 15:03

Flutter use a radically different rendering architecture. All the rendering is computed with Dart code, without ever using native elements.

So not only you can't use native code, you can't have something like a "Import native to flutter" either.

For libraries, it's a complicated topic. You can use them. But only inside what Flutter calls Platform channel. Which is an api to make system call written in code that run natively in the OS.

So libraries such as Firebase are fine. But components libraries or things like rxjava are out. You'll need a similar dart library.

查看更多
登录 后发表回答