24
Developing in C++ for Mobile Igor Kantor CTO @

Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Embed Size (px)

Citation preview

Page 1: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Developing in C++ for MobileIgor Kantor

CTO @

Page 2: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

С++ in mobile

Page 3: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Our case

• Product: iOS, Android Apps; Backend; Desktop coming

• Communication protocol; API; security; client-server

• A lot of non-trivial code to be duplicated

Android

iOS

Symbian

The whole solution brakes down, because somethings takes too much time to implement on Android

Page 4: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Why should we re-write the same code and fix the same bugs 2-3 times?

Page 5: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Goals

1. Less developers, optimize efforts, cost saving

2. Streamline and speed up the development and bugfixing

3. Increase stability and quality of the product

Page 6: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Option 1: cross-platform tools• PhoneGap • RhoMobile • Sencha Touch • appMobi • Telerik • Adobe AIR • Xamarin • Appcelerator Titanium • Corona • JavaScript

Page 7: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Option2: C++

OK, it’s clear with games. But what about business apps?

Page 8: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

DecisionIn 2013 we decided to re-design the product architecture and write the core in C++

OS abstraction layer

Protocol Media

High-level SDK: business login, etc.

iOS Android DesktopJNI

Symbian

C++

More to come

Page 9: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"
Page 10: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Our results

• Learning curve

• Are we happy? Definitely yes: 70% of code is shared in C++ and support is much easier

• Streamlined development: we get all the new features simultaneously

Page 11: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Lesson Learned• Choose solid and convenient OS abstraction library

• We had to re-write timers routine 3 times!

• Encapsulation and polymorphism

• Understand what should remain native

• Unless 100% sure keep the networking part native

• Get a tool for interface between native language and C++

Page 12: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Recommendations

Page 13: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

When to use

Use Don’t use

Long running project/product Short projects, outsourcing

Cost and effort optimization is a key Projects are not so complicated

Familiar and ready for C++ and cross-platform C++ libraries Don’t have enough C++ expertise

Page 14: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Embrace C++11/14

C++11/14 makes replacing ObjC/Java feasible:

• shared_ptr, unique_ptr • new containers • atomics and threading

Page 15: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Define where to put the line

• UI – not a good candidate for C++

• Business logic, Data, Model – perfect candidates

• Define what should remain native: e.g. networking

Page 16: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Choose solid OS abstraction library

• Choose it carefully

• Choose mature only solutions

• It will become your framework to provide you with primitives like Thread, Timer, File access, etc. etc.

• Options: Qt, Boost, Poco, self-made

Page 17: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Interface

• Define how your Apps will speak to C++ part

• Think this through at the very beginning

• Easy on iOS; relatively easy on Windows (directly or PInvoke)

• JNI – default NDK option for Android. Try to avoid it.

Page 18: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Interface: optionsPossible ways to build interfaces:

1. Traditional: ObjC ↔ C++ Java ↔ JNI ↔ C++

2. Protocol Buffers + ZeroMQ

3. Djinni

4. http://google.github.io/flatbuffers/

Page 19: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Interface: Protocol Buffers + ZeroMQBenefits:

1. Your C++ part will act as “local server”

2. Thread safe

3. Language independent interface: don’t have to define bindings and types for each platform

Page 20: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Setup build/run process

• Be ready to test your code on Android/iOS which implies in having both Desktop and Mac

Page 21: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Data layer

• A good candidate for cross-platform C++ implementation

• Many great libraries exists: SQLite, SQLCipher, Realm.io / Poco

• For simple structures use plain SQLite C implementation

Page 22: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

Where to starthttps://github.com/libmx3/mx3

Start with simple prototypes just to get the feeling on timers, threads, data access, network, etc.

Page 23: Lviv MD Day 2015 Ігор Кантор "Розробка додатків зі спільним C++ кодом для iOS i Android"

We’re in good company