joshlf
joshlf

Reputation: 23547

Best Practices for Shared Android/iOS Development

I'm working on an app that will run on both Android and iOS, and our team has decided that we want to use a shared library to cut down on code and effort duplication.

Most of this is pretty straightforward: we write a library in some language that can go cross-platform (C++ with a native interface, C# with Mono, etc), and then provide platform-specific bindings. The problem, however, is that since our library has to implement complex application logic, it has to do a lot of interacting with the system itself (storing data in files and databases, querying APIs such as location, notification, etc). As best as we can tell, this requires another layer - sitting below our library and above the system - which is written in the platform-specific language, and provides bindings that our library can call to.

The problem with this is that the different platforms have fundamentally different design paradigms for performing different tasks. For example, storing app data involves touching fundamentally different system components (databases vs files, different partitions, or files with different filepaths) on the two platforms. We want this platform-specific logic to stay out of the library, but figuring out how to put it in the conversion layer is non-obvious. We can't simply provide API methods - what happens if one platform necessitates performing a given action at a different time, or in a different order? What about listening for system actions, which may, on certain platforms, require spawning different threads?

The question is: is there any wisdom about how to solve these problems cleanly? We know that it's been done before - Dropbox, for example, has a common library (libdropbox) written in C++ that backs their Android, iOS, and desktop clients.

EDIT: We know that single-codebase options like Xamarin are available, but we're curious to see if there are other options.

Upvotes: -1

Views: 725

Answers (2)

Chamila Chulatunga
Chamila Chulatunga

Reputation: 4914

So firstly, there's probably a few places where these kinds of issues have already been solved - for C#, for example, have a look at Mono/Xamarin Studio. But if you're more interested in a general approach (I'll use saving data as the use case for examples):

Look at it from the perspective of your shared library - what kind of function calls do you want to make from the shared library into the platform-specific code. This defines the interface from the shared library to the platform-specific code. E.g.

Save(data)

Once you've got that worked out, it's then a matter of implementing the platform-specific code to fulfil those functions. Since each platform has different mechanisms for any given task, you will probably need some kind of co-ordination layer that understands the sequence of actions needed to enact the desired outcomes on that specific platform. E.g

Platform A (check permissions + file)

int Save(byte[] data)
{
    var isAllowed = CheckPermissions();
    if(isAllowed)
    {
        var dataFile = GetDataFile();
        return SaveToFile(dataFile, data);
    }

    return -1;
}

Platform B (database)

int Save(byte[] data)
{
    var database = GetDatabase();
    var targetTable = GetTable(database, "temp");
    return AppendToTable(database, targetTable, data);
}

The co-ordination layer could in turn call lower-level libraries to enact the individual actions - this may or may not make sense depending on whether there are lower-level functions that would be shared across various functions at the co-ordination level. E.g. candidates for shared use might be (from above)

Platform A

CheckPermissions()
GetDataFile()
SaveToFile(File file, byte[] data)

Platform B

GetDataBase()
GetTable(Database database, string tableName)
AppendToTable(Database database, Table table, byte[] data)

Upvotes: 1

Ali Behzadian Nejad
Ali Behzadian Nejad

Reputation: 9044

The best approach is to use hybrid (html5/JS) platforms like Apache Cordova OR Xamarin: "With Xamarin, you write your apps entirely in C#, sharing the same code on iOS, Android, Windows and Mac."

Upvotes: 0

Related Questions