Questions about web language compatibility in a cross-plate project

I plan to create an application on iOS, Android and on a site with AngularJS.

But in order not to rewrite the business code for each application, I would like to use as much code as possible.

To be able to run the project core on any platform, I have to use a web language.

In different articles, I plan a common architecture for separating the business logic of the project - the core - with a user interface that will be reimplemented for each system (UIKit for iOS, AngularJS and Polymer for webapp, etc.)

The purpose of this architecture is to respect important principles of software development, such as hiding information by expanding requirements in modules , DRY and SOLID

  • Each function will be expanded in the module.
  • Core : the business logic code - reusable on each platform - will be presented as a library.
  • View . A view class will be developed on every other platform to use the various user interface elements offered on each platform. For example: a subclass of ViewController in Objective-C / Swift for iOS or a simple class for managing HTML for a web application. There is no logic in this class. Responsibility bears only:
    • Handle user interactions with business logic.
    • Display content from business logic
  • IView : an interface that abstracts a class that manipulates the view.
  • Presenter : The relationship between Interactor and View to control the user interface.
  • Interactor : module logic, such as algorithms.
  • Data warehouse . Manage persistence and data retrieval by exchanging data with a database or API or web service.
  • Model : data presented in structures.

Here for iOS (almost Android):

enter image description here

The "core" code will be executed through a virtual machine, as this article shows: http://www.skyscanner.net/blogs/developing-mobile-cross-platform-library-part-3-javascript

Here for AngularJS:

https://docs.google.com/drawings/d/1NtdDfsr1yiuGOm_lfOY6Qab_kHb4I5b4sx7zeTAqwrE/pub?w=915&h=377


Now that you know everything about architecture, here are my questions.

I do not have enough experience and reviews in web languages ​​to be able to make a reasonable choice. After several studies, I found that there are various options:

  • Dart

    • Question 1: Are there mechanisms in place to enable interaction between Objective-C / Swift and Java through the VM? I know that both platforms have a VM for executing Javascript code, and Google provides dart2js for compiling Dart code in Javascript. But this is not simple Javascript: see an example here . Therefore, I do not know if there is proper interoperability.
  • Javascript ES6: An event, if it is not yet fully implemented in browsers, it is possible to start using ES6 with Traceur .

    • Question 2: Is there Javascript compatibility compiled by Traceur and VM on iOS / Android?
    • Question 3: Is it safe to use ES6 through Traceur to develop a large-scale project and have production code?

Thanks for reading.

+5
source share
3 answers

I know that this was not one of the options that you specified, but do not automatically exclude C ++. This is what Dropbox uses, for example, they even open their tools for this purpose:

C ++ for Java / Objective-C Generator API:

https://github.com/dropbox/djinni

Example native application for Android / iOS:

https://github.com/libmx3/mx3

An interesting article on this subject with additional links:

http://oleb.net/blog/2014/05/how-dropbox-uses-cplusplus-cross-platform-development/

Updated answer:

If you really don't want to use C ++, and it's okay with the bloat that you get from the non-native transition, you can try the following:

https://github.com/MobileChromeApps/mobile-chrome-apps

This project is a Google fork from Cordoba and adds new features and benefits.

There is a Dart worm in the Chrome API:

https://github.com/dart-gde/chrome.dart

Basically, you should write your application in Dart using simple HTML5 technologies, and then for certain things that you would use the Chrome API (device status, etc.). Then you can expand:

  • Web: Compile JavaScript without Chrome API functions.
  • Chrome OS: compile JavaScript with Chrome API features.
  • Android: compile JavaScript and then use MobileChromeApps to create an Android app.
  • iOS: Compile JavaScript and then use MobileChromeApps to create an iOS application.
+2
source

This is an interesting topic. Here is what I learned from the GWT.Create conference, the guys from Google showed how they made the cross-platform project:

First, part of the DataStore and Model data in your image must be made on an external server, so it has already crossed platforms.

The user interface output should be done individually, which is the best solution.

They implemented common logic (complex calculation, encryption, etc.) with Java, it works non-standard for Android, for the Internet they use GWT to translate Java into Javascript, and for iOS they use the new Google product J2ObjC. You can find it here:

https://github.com/google/j2objc

They also mentioned the C / Cpp solution, this is not a bad idea at all, Java is just a high-level language and easy to use in most cases.

+1
source

If you want to create a cross-platform application (iOS / Android / Web), it is best to use as much code as possible between these platforms. You could use something like PhoneGap / Cordova , but this is not always very popular. Event: the best PhoneGap app seems not native because it does not use its own user interface. Instead, the browser is embedded in its own user interface container.

I recommend using the project structure as follows:

  • Myapp kernel
  • MYAPP-ISN
  • Myapp android
  • Myapp web

The main project is shared between ios, android and the web project. You can write a main project in Java and use GWT to translate this code into JavaScript for a web project. There is nothing to do for an Android project, because Android uses Java. For your ios project, you can use J2Objc to pass the main Java code to Objective-C.

What is included in the main project?

Use interfaces and abstract base classes as well as factories as much as possible.

Conversations, reminders and contacts. We are also talking about the complex task of network management and offline synchronization, when the application is used offline, reminders are created and emails are sent. This is an application to save all this and launch it on the Internet when the time comes. A source

Of course, there are a number of elements included in the Inbox folder on three platforms: code for managing network communications, caching of objects, local persistent storage, managing user changes both locally and remotely, and supporting it while offline. This logic must be faithfully and correctly implemented by all three clients. Rewriting it three times into three different languages ​​will absorb significant engineering resources and slow down how quickly we make improvements for Inbox.

For iOS, we developed the open source cross-compiler J2ObjC to translate our Java data model to Objective-C, and again we get a natural API on which you can create your own Inbox Inbox application (complete with - [Reminder snooze]), Insightful the reader may wonder how we deal with impedance mismatch when translating from a garbage collected language (Java) to a counted link (Objective-C). As a rule, J2ObjC relies on autorelease Objective-C pools, so objects that are usually garbage collected are freed when the pool merges. One of the problems with this approach is the reference cycles; Where loops exist in our Java data model, we use the Java annotation to identify @WeakReference. When forwarding, the corresponding property in Objective-C will have the __weak modifier, thereby breaking the save cycle. In practice, we have found that this is a relatively small problem, and we have automation tests that indicate rare cases of new cycles crawling around the object model. A source

Communication between your main modules can be done using Dagger2 , which runs on Android / iOS / GWT. There is also a cross-platform JSON library called realtime-json . HTTP transport can be implemented in the kernel, at least for Android and iOS. Form GWT / JavaScript, you must do this on the client side.

Here are some sample applications to help you:

+1
source

Source: https://habr.com/ru/post/1212223/


All Articles