<<

Android & iOS – on the edge of Qt and Java/Objective-

Maciej Węglarczyk Android & iOS – on the edge of Qt and Java/Objective-C

About me

● Cracow, Poland

● Graduated from AGH-UST with MSc in CS

● 6+ Qt years

● Game industry

● Ganymede Ltd. (3 years+)

● Android & iOS lead Android & iOS – on the edge of Qt and Java/Objective-C

The purpose of this talk

● Explain if / why one needs to write code beyond Qt

● Show what cannot be done with only Qt

● Give a taste of the way to communicate with native environments

● Give a brief overview how Qt is integrated with Android & iOS

Android & iOS – on the edge of Qt and Java/Objective-C

Agenda

● Reasons of extending Qt app with native code

● Needed tools

● Mixing Qt with Java/Objective-C

● Adding 3rd party libraries

● Modifying application entry point

Android & iOS – on the edge of Qt and Java/Objective-C

Beyond Qt

“It's a dangerous business, Frodo, going out of your door”

Bilbo Baggins Android & iOS – on the edge of Qt and Java/Objective-C

EXTENDING? WHY?

Android & iOS – on the edge of Qt and Java/Objective-C

Importance of rich features

● Over 1,000,000 apps available in Google Play and Apple AppStore each

● Very demanding users

● Different ways of monetization

● Chances of being featured

● Increasing user engagement

● Virality

Android & iOS – on the edge of Qt and Java/Objective-C

Native integration - it's actually nothing new

● Qt Windows Extras, Qt Mac Extras, Qt X11 Extras

● Mobile OSes are evolving faster

● The problem is similar

Android & iOS – on the edge of Qt and Java/Objective-C

Where to start?

● First of all – know what you want and why

● Check if Qt supports it

● If not, check if it hasn't been done already by someone else...

● ...or if it couldn't be done by someone else in near future. :)

Android & iOS – on the edge of Qt and Java/Objective-C

What's not possible with Qt (at least for now)

● Notifications (push, rich) ● Alarms

● SMS API ● Communication with other apps

● Lock screen widgets ● Many useful 3rd party integrations

● Home screen widgets ● Facebook Connect

● Google / Apple API ● Google Analytics

● Maps ● Monetization libs (i.e. Chartboost)

● Game Services / Center ● ...

● Calendar

● ... Android & iOS – on the edge of Qt and Java/Objective-C

Required tools

Android iOS

● QtCreator ● QtCreator ● Some IDE for Java, i.e. Eclipse ●

● Android SDK & NDK ● Apple Developer Program Account

● Ant ● Xcode Command Line Tools

● Some devices ● Simulator is OK ( is still rather slow and sometimes unreliable) (devices are better, though)

Android & iOS – on the edge of Qt and Java/Objective-C

Useful tips

● Compile Output panel is much much more informative than Issues panel

● Android: QtCreator's Build command builds only Qt code, not Java yet

● Android: turn on the androiddeployqt's verbose option

Android & iOS – on the edge of Qt and Java/Objective-C

Basic Android / iOS projects

● Just take any of Qt's examples for given platform

● Compile and run it from QtCreator

● That's it

Android & iOS – on the edge of Qt and Java/Objective-C

ADDING NATIVE CODE

Android & iOS – on the edge of Qt and Java/Objective-C

Adding native code – Android

● JNI – Java Native Interface

● Encapsulated into Qt's androidextras module

● Containing a couple of useful classes and a QtAndroid

QT += androidextras

QAndroidActivityResultReceiver QAndroidJniEnvironment QAndroidJniObject

Android & iOS – on the edge of Qt and Java/Objective-C

Adding native code – Android

● Both directions possible:

● C++ -> Java

● Java -> C++

https://github.com/FenixVoltres/QtAndroidCpp2Java https://github.com/FenixVoltres/QtAndroidJava2Cpp

Android & iOS – on the edge of Qt and Java/Objective-C

Adding native code – iOS

It's very simple:

● Create some C++ class with normal header

● Use Objective-C in bodies of declared methods.

● That's all! Android & iOS – on the edge of Qt and Java/Objective-C

Adding native code – iOS

● Following example taken from Richard Moe Gustavsen's talk on Qt for iOS from last year Qt DD 13

https://github.com/richardmg/qtdd13_qmlapp

Android & iOS – on the edge of Qt and Java/Objective-C

How about Swift?

● C++ code can’t be executed from Swift directly

● Objective-C wrapper needed

Swift -> Objective-C++ -> Qt Android & iOS – on the edge of Qt and Java/Objective-C

ADDING NATIVE LIBRARIES

Android & iOS – on the edge of Qt and Java/Objective-C

Adding native libraries – Android

● Make sure you have defined ANDROID_PACKAGE_SOURCE_DIR variable in .pro file

● Add libs folder inside and put there all jars you want

● $PROJECT

● android-sources

● libs

https://github.com/FenixVoltres/QtAndroidNativeLib

Android & iOS – on the edge of Qt and Java/Objective-C

Adding native libraries – iOS

Even simpler:

LIBS += -F/path/to/frameworks/folder LIBS += -framework StoreKit Android & iOS – on the edge of Qt and Java/Objective-C

MODYFYING ENTRY POINT

Android & iOS – on the edge of Qt and Java/Objective-C

Why one would modify an entry point?

● Not whole application must be written in Qt

● To override some Qt's behavior

● To make some actions before Qt is loaded

Android & iOS – on the edge of Qt and Java/Objective-C

Deployment process – Android

● A bit complicated

● Two overrided classes – QtActivity.java and QtApplication.java are copied to android build folder

● Located in $QT/android_xxx/src/android/java/src/org/qtproject/qt5

● Used in AndroidManifest.xml

Android & iOS – on the edge of Qt and Java/Objective-C

Adding .java source files

● Define ANDROID_PACKAGE_SOURCE_DIR variable in .pro file, so it points to folder with Android internal folders structure (src, res, assets, libs) or let QtCreator does it for you

● Put all .java files in proper folder structure connected with their packages in src folder

● Put all other resources in proper folders – all of them will be copied into the final .apk file

Android & iOS – on the edge of Qt and Java/Objective-C

Modifying app’s entry point – Android

● AndroidManifest.xml can be changed – any different Activity can be an entry point.

● QtActivity and QtApplication can be overridden

https://github.com/FenixVoltres/QtAndroidSplash

Android & iOS – on the edge of Qt and Java/Objective-C

Deployment process – iOS

● Again, very simple

● Building a project creates Xcode project file in build folder

● It can be open an freely modified after QtCreator build.

● After any change in QtCreator the process must be repeated!

Android & iOS – on the edge of Qt and Java/Objective-C

Modifying app’s entry point – iOS

● Open Xcode project generated by QtCreator

● Modify what you want.

Android & iOS – on the edge of Qt and Java/Objective-C

WRAP UP

Android & iOS – on the edge of Qt and Java/Objective-C

Mixing Qt with native code

● Much easier on iOS than on Android

● Almost everything is possible

Good luck!

Android & iOS – on the edge of Qt and Java/Objective-C

THANK YOU!

Android & iOS – on the edge of Qt and Java/Objective-C

Qt& Am Questions & Answers maybe

Maciej Węglarczyk [email protected]