android & ios – on the edge of qt and java/objective-c€¦ · the purpose of this talk...

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

Upload: others

Post on 18-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

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

Maciej Węglarczyk

Page 2: 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

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

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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

Page 6: 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

EXTENDING? WHY?

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 7: 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

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

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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

● Notifications (push, rich)

● SMS API

● Lock screen widgets

● Home screen widgets

● Google / Apple API

● Maps

● Game Services / Center

● Calendar

● ...

● Alarms

● Communication with other apps

● Many useful 3rd party integrations

● Facebook Connect

● Google Analytics

● Monetization libs (i.e. Chartboost)

● ...

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 11: 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

Required tools

Android & iOS – on the edge of Qt

and Java/Objective-C

Android

● QtCreator

● Some IDE for Java, i.e. Eclipse

● Android SDK & NDK

● Ant

● Some devices (emulator is still rather slow and sometimes unreliable)

iOS

● QtCreator

● Xcode

● Apple Developer Program Account

● Xcode Command Line Tools

● Simulator is OK (devices are better, though)

Page 12: 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

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

Page 13: 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

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

Page 14: 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

ADDING NATIVE CODE

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 15: 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

Adding native code – Android

● JNI – Java Native Interface

● Encapsulated into Qt's androidextras module

● Containing a couple of useful classes and a namespace QtAndroid

QT += androidextras

QAndroidActivityResultReceiver

QAndroidJniEnvironment

QAndroidJniObject

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 16: 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

Adding native code – Android

● Both directions possible:

● C++ -> Java

● Java -> C++

<Example>

https://github.com/FenixVoltres/QtAndroidCpp2Java

https://github.com/FenixVoltres/QtAndroidJava2Cpp

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 17: 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

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

Page 18: 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

Adding native code – iOS

● Following example taken from Richard Moe Gustavsen's

talk on Qt for iOS from last year Qt DD 13

<Example>

https://github.com/richardmg/qtdd13_qmlapp

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 19: 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

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

Page 20: 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

ADDING NATIVE LIBRARIES

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 21: 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

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

● <put your jar here>

<Example>

https://github.com/FenixVoltres/QtAndroidNativeLib

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 22: 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

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

Page 23: 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

MODYFYING ENTRY POINT

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 24: 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

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

Page 25: 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

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

Page 26: 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

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

Page 27: 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

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

<Example>

https://github.com/FenixVoltres/QtAndroidSplash

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 28: 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

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

Page 29: 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

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

Page 30: 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

WRAP UP

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 31: 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

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

Page 32: 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

THANK YOU!

Android & iOS – on the edge of Qt

and Java/Objective-C

Page 33: 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

Qt& Am Questions & Answers maybe

Android & iOS – on the edge of Qt

and Java/Objective-C

Maciej Węglarczyk

[email protected]