SlideShare a Scribd company logo
1 of 45
Download to read offline
Android and C++ 
Joan Puig Sanz
About me 
@joanpuigsanz 
2
What we will see? 
1. Apps for all the platforms 
2. C++ 11 
3. Libraries 
4. Java, C++ and JNI 
5. Final thoughts 
3
Text 
The same app running in all the platforms 
4
5
Most common solutions? 
PhoneGap Adobe Air 
Xamarin 
Titanium 
6
7
✦ Not native UI 
✦ Slow performance 
✦ Custom components. 
✦ Depend on a company 
✦ Poor user experience 
8
Good user experience 
Smooth 
UI 
UX 
9
Text 
An other old solution… 
C++ 10
Why C++? 
Cross platform 
language 
Better performance 
Mature language 
Lots of libraries 
11
Combining C++ with Android 
Native UI and UX 
JNI 
C++ core 
12
Text 
C++ 11 features 
13
C++11 Features :: Auto 
The compiler deducing the type 
auto i = 42; // i is an int 
auto l = 42LL; // l is an long long 
auto p = new Foo(); // p is a foo* 
std::map<std::string, std::vector<int>> map; 
for(auto it = begin(map); it != end(map); ++it) 
{} 
14
C++11 Features :: 
Range-based for loops 
Iterate collections with foreach 
std::vector<int> v; 
v.push_back(1); 
v.push_back(2); 
v.push_back(3); 
for (auto value : v) { 
std::cout << value << std::endl; 
} 
15
C++11 Features :: 
Smart pointers 
unique_ptr: Ownership of a memory resource it is 
not shared, but it can be transferred to another 
unique_ptr 
shared_ptr: Ownership of a memory resource 
should be shared 
weak_ptr: Holds a reference to an object managed 
by a shared_ptr, but does not contribute to the 
reference count; it is used to break dependency cycles. 
16
C++11 Features :: Lambdas 
Powerful feature borrowed from functional 
programming. 
You can use lambdas wherever a function object or a 
functor or a std::function is expected 
std::function<int(int)> lfib = [&lfib](int n) { 
return n < 2 ? 1 : lfib(n-1) + lfib(n-2); 
}; 
17
C++11 Features :: More 
Final and override 
non-member begin() and end() 
Initializer lists 
Object construction improvement 
Unrestricted unions 
User-defined literals 
static_assert 
Strongly-typed enums 
… 
18
Text 
Complementing C++ 
Libraries 
19
Complementing C++ 
C++11 standard library is 
missing utilities: 
HTTP Requests 
XML and JSON 
Big numbers 
Security 
Math 
20
C++ libs :: Boost 
Supports: 
Licence: 
AS IS 
Big community 
Lots of modules 
21
C++ libs :: Juce 
Supports: 
No WP (for now) 
Licence: 
Core: AS IS 
Other modules: GPL and commercial licence 
Very well written code 
Easy to use 
22
C++ libs :: Qt 
Supports: 
Licence: 
GPL and commercial licence 
Big community 
Lots of modules 
23
Text 
Putting stuff together 
JNI 
24
Calling JNI from Java 
Declare native methods 
private native void doSomethingNative (String str); 
Implement C method 
JNIEXPORT void Java_com_example_MyClass_doSomethingNative (JNIEnv 
*env, jobject obj, jstring s) 
{ 
const char* const utf8 = env->GetStringUTFChars (s, nullptr); 
CharPointer_UTF8 utf8CP (utf8); 
String cppString (utf8CP); 
env->ReleaseStringUTFChars (s, utf8); 
// Do Something 
} 
25
Calling JNI from Java 
JNIEXPORT void Java_com_example_MyClass_doSomethingNative 
(JNIEnv *env, 
jobject obj, 
jstring s) 
Required macro to support Windows 
Java + package_name + 
className + methodName 
26
Calling C from Java 
JNIEXPORT void Java_com_example_MyClass_doSomethingNative 
(JNIEnv *env, 
jobject obj, 
jstring s) 
Structure to access all the JNI functions 
Java object that calls the native function 
Java method arguments 
27
Local and global references 
Local reference will be managed by java 
A global reference is managed by the 
developer 
Max number 
of java 
references 
= 512 
28
Calling Java from C 
public String myMethod (int[] array, boolean enabled) 
29
Calling Java from C 
public String myMethod (int[] array, boolean enabled) 
JNIEnv* env = getEnv(); 
jclass myJavaClass = env->GetObjectClass (myJavaObject); 
jmethodID myJavaMethod = env->GetMethodID(myJavaClass , 
”methodName", "([I;Z)Ljava/lang/String"); 
jstring jresult = (jstring) env->CallObjectMethod 
(myJavaObject, myJavaMethod , myIntArray, myBoolean); 
// Do something 
env->DeleteLocalRef (jresult); 
env->DeleteLocalRef (myJavaClass); 
30
Calling Java from C 
public String myMethod (int[] array, boolean enabled) 
JNIEnv* env = getEnv(); 
jclass myJavaClass = env->GetObjectClass(myJavaObject); 
jmethodID myJavaMethod = env->GetMethodID(myJavaClass , 
”methodName", "([I;Z)Ljava/lang/String"); 
jstring jresult = (jstring) env->CallObjectMethod 
(myJavaObject, myJavaMethod , myIntArray, myBoolean); 
// Do something 
env->DeleteLocalRef (jresult); 
env->DeleteLocalRef (myJavaClass); 
Must not be NULL 
This is the java object that has the method 
31
Calling Java from C 
public String myMethod (int[] array, boolean enabled) 
JNIEnv* env = getEnv(); 
jclass myJavaClass = env->GetObjectClass(myJavaObject); 
jmethodID myJavaMethod = env->GetMethodID(myJavaClass, 
”myMethod", "([I;Z)Ljava/lang/String"); 
jstring jresult = (jstring) env->CallObjectMethod 
(myJavaObject, myJavaMethod , myIntArray, myBoolean); 
// Do something 
env->DeleteLocalRef Method signature 
(jresult); 
env->DeleteLocalRef (myJavaClass); 
32
Calling Java from C 
public String myMethod (int[] array, boolean enabled) 
JNIEnv* env = getEnv(); 
jclass myJavaClass = env->GetObjectClass(myJavaObject); 
jmethodID myJavaMethod = env->GetMethodID(myJavaClass, 
”myMethod", "([I;Z)Ljava/lang/String"); 
jstring jresult = (jstring) env->CallObjectMethod 
(myJavaObject, myJavaMethod , myIntArray, myBoolean); 
// Do something 
env->DeleteLocalRef Method signature 
(jresult); 
env->DeleteLocalRef (myJavaClass); 
Type Signature Java Type 
Z Boolean 
B byte 
C char 
S short 
I int 
J long 
F float 
D double 
L class ; full qualified 
[type; tcylpases[] 
33
Calling Java from C 
public String myMethod (int[] array, boolean enabled) 
JNIEnv* env = getEnv(); 
jclass myJavaClass = env->GetObjectClass(myJavaObject); 
jmethodID myJavaMethod = env->GetMethodID(myJavaClass, 
”myMethod", "([I;Z)Ljava/lang/String"); 
jstring jresult = (jstring) env->CallObjectMethod 
(myJavaObject, myJavaMethod , myIntArray, myBoolean); 
// Do something 
env->DeleteLocalRef Method signature 
(jresult); 
env->DeleteLocalRef (myJavaClass); 
34
Calling Java from C 
public String myMethod (int[] array, boolean enabled) 
JNIEnv* env = getEnv(); 
jclass myJavaClass = env->GetObjectClass(myJavaObject); 
jmethodID myJavaMethod = env->GetMethodID(myJavaClass , 
”myMethod", "([I;Z)Ljava/lang/String"); 
jstring jresult = (jstring) env->CallObjectMethod 
(myJavaObject, myJavaMethod , myIntArray, myBoolean); 
// Do something 
env->DeleteLocalRef (jresult); 
env->DeleteLocalRef (myJavaClass); 
35
Calling Java from C 
public String myMethod (int[] array, boolean enabled) 
JNIEnv* env = getEnv(); 
jclass myJavaClass = env->GetObjectClass(myJavaObject); 
jmethodID myJavaMethod = env->GetMethodID(myJavaClass , 
”myMethod", "([I;Z)Ljava/lang/String"); 
jstring jresult = (jstring) env->CallObjectMethod 
(myJavaObject, myJavaMethod , myIntArray, myBoolean); 
// Do something 
env->DeleteLocalRef (jresult); 
env->DeleteLocalRef (myJavaClass); 
36
Java and C++ object binding 
37
Java and C++ object binding 
Java (OO) 
C 
C++ (OO) 
38
Java and C++ object binding 
Java (OO) 
C 
C++ (OO) 
39
Foo Java implementation 
public class Foo { 
native void destroyCppInstanceNative(double ref); 
native long newCppInstanceNative(); 
native String getStringNative(double ref); 
private double _ref = 0; 
public Foo() { 
_ref = newCppInstanceNative(); 
} 
public String getString() { 
return getStringNative(_ref); 
} 
public void destroy() { 
destroyCppInstanceNative(_ref); 
} 
} 40
Foo Java implementation 
JNIEXPORT long Java_com_example_Foo_newCppInstanceNative(JNIEnv 
*env, jobject obj) { 
Foo* newFoo = new Foo(); 
return (double)(newFoo); 
} 
JNIEXPORT jstring Java_com_example_Foo_getStringNative(JNIEnv 
*env, jobject obj, double ref) { 
Foo* foo = (Foo*)(ref); 
jstring jStringResult = env->NewStringUTF (foo->getString()); 
return jStringResult; 
} 
JNIEXPORT void 
Java_com_example_Foo_destroyCppInstanceNative(JNIEnv *env, jobject 
obj, double ref) { 
Foo* foo = (Foo*)(ref); 
delete foo; 
} 
41
Text 
Some advice to live happier with C++ 
42
Some advice 
Code quality! (Code conventions, code reviews, 
etc.) 
C++ is not a read only code, don’t put the blame on 
it 
Remember to initialise all the values in the 
constructor 
Use unity builds to speed up compilation time 
JNI could be painful. Check out djinni to generate 
cross-language type declarations and interface 
bindings. 
dropbox/djinni 
43
Final thoughts 
Pros 
Same core and with native UI/UX per platform 
Better performance 
Faster cross platform development 
Easier maintenance 
Cons 
Need to learn a new language 
Android apps will be bigger (code compiled 
for different architectures) 
Can be fixed distributing specific apk per 
each architecture 44
?@joanpuigsanz 
45

More Related Content

What's hot

Introduction to OpenCV (with Java)
Introduction to OpenCV (with Java)Introduction to OpenCV (with Java)
Introduction to OpenCV (with Java)Luigi De Russis
 
The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++Alexander Granin
 
Introduction to OpenCV 3.x (with Java)
Introduction to OpenCV 3.x (with Java)Introduction to OpenCV 3.x (with Java)
Introduction to OpenCV 3.x (with Java)Luigi De Russis
 
3150 Chapter 2 Part 1
3150 Chapter 2 Part 13150 Chapter 2 Part 1
3150 Chapter 2 Part 1Mole Wong
 
Async await in C++
Async await in C++Async await in C++
Async await in C++cppfrug
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and deletePlatonov Sergey
 
10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about goDvir Volk
 
Introduction to D programming language at Weka.IO
Introduction to D programming language at Weka.IOIntroduction to D programming language at Weka.IO
Introduction to D programming language at Weka.IOLiran Zvibel
 
AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8Phil Eaton
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationGlobalLogic Ukraine
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)Ishin Vin
 
Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...
Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...
Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...Yandex
 
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17LogeekNightUkraine
 
Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016maiktoepfer
 
Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersJen Yee Hong
 
Python on a chip
Python on a chipPython on a chip
Python on a chipstoggi
 

What's hot (20)

Easy native wrappers with SWIG
Easy native wrappers with SWIGEasy native wrappers with SWIG
Easy native wrappers with SWIG
 
Introduction to OpenCV (with Java)
Introduction to OpenCV (with Java)Introduction to OpenCV (with Java)
Introduction to OpenCV (with Java)
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++
 
Introduction to OpenCV 3.x (with Java)
Introduction to OpenCV 3.x (with Java)Introduction to OpenCV 3.x (with Java)
Introduction to OpenCV 3.x (with Java)
 
3150 Chapter 2 Part 1
3150 Chapter 2 Part 13150 Chapter 2 Part 1
3150 Chapter 2 Part 1
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
 
Async await in C++
Async await in C++Async await in C++
Async await in C++
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and delete
 
10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about go
 
Introduction to D programming language at Weka.IO
Introduction to D programming language at Weka.IOIntroduction to D programming language at Weka.IO
Introduction to D programming language at Weka.IO
 
AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8
 
Golang
GolangGolang
Golang
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...
Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...
Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...
 
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
 
Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016
 
Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmers
 
Python on a chip
Python on a chipPython on a chip
Python on a chip
 

Similar to Android and cpp

JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)DroidConTLV
 
Improving Java performance at JBCNConf 2015
Improving Java performance at JBCNConf 2015Improving Java performance at JBCNConf 2015
Improving Java performance at JBCNConf 2015Raimon Ràfols
 
Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014Raimon Ràfols
 
Eric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemEric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemGuardSquare
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsAzul Systems, Inc.
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13Chris Ohk
 
Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoinknight1128
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...Doug Jones
 
Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleThierry Wasylczenko
 
Catch a spider monkey
Catch a spider monkeyCatch a spider monkey
Catch a spider monkeyChengHui Weng
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerGarth Gilmour
 
Shiksharth com java_topics
Shiksharth com java_topicsShiksharth com java_topics
Shiksharth com java_topicsRajesh Verma
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahNick Plante
 
iOS for Android Developers (with Swift)
iOS for Android Developers (with Swift)iOS for Android Developers (with Swift)
iOS for Android Developers (with Swift)David Truxall
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
Jug trojmiasto 2014.04.24  tricky stuff in java grammar and javacJug trojmiasto 2014.04.24  tricky stuff in java grammar and javac
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javacAnna Brzezińska
 

Similar to Android and cpp (20)

Android ndk
Android ndkAndroid ndk
Android ndk
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)
 
Improving Java performance at JBCNConf 2015
Improving Java performance at JBCNConf 2015Improving Java performance at JBCNConf 2015
Improving Java performance at JBCNConf 2015
 
Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014
 
Eric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemEric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build system
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM Mechanics
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
 
Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoin
 
Json generation
Json generationJson generation
Json generation
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
 
Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradle
 
Catch a spider monkey
Catch a spider monkeyCatch a spider monkey
Catch a spider monkey
 
Invoke Dynamic
Invoke DynamicInvoke Dynamic
Invoke Dynamic
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
Shiksharth com java_topics
Shiksharth com java_topicsShiksharth com java_topics
Shiksharth com java_topics
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and Pindah
 
iOS for Android Developers (with Swift)
iOS for Android Developers (with Swift)iOS for Android Developers (with Swift)
iOS for Android Developers (with Swift)
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
Jug trojmiasto 2014.04.24  tricky stuff in java grammar and javacJug trojmiasto 2014.04.24  tricky stuff in java grammar and javac
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
 

Recently uploaded

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 

Recently uploaded (20)

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 

Android and cpp

  • 1. Android and C++ Joan Puig Sanz
  • 3. What we will see? 1. Apps for all the platforms 2. C++ 11 3. Libraries 4. Java, C++ and JNI 5. Final thoughts 3
  • 4. Text The same app running in all the platforms 4
  • 5. 5
  • 6. Most common solutions? PhoneGap Adobe Air Xamarin Titanium 6
  • 7. 7
  • 8. ✦ Not native UI ✦ Slow performance ✦ Custom components. ✦ Depend on a company ✦ Poor user experience 8
  • 9. Good user experience Smooth UI UX 9
  • 10. Text An other old solution… C++ 10
  • 11. Why C++? Cross platform language Better performance Mature language Lots of libraries 11
  • 12. Combining C++ with Android Native UI and UX JNI C++ core 12
  • 13. Text C++ 11 features 13
  • 14. C++11 Features :: Auto The compiler deducing the type auto i = 42; // i is an int auto l = 42LL; // l is an long long auto p = new Foo(); // p is a foo* std::map<std::string, std::vector<int>> map; for(auto it = begin(map); it != end(map); ++it) {} 14
  • 15. C++11 Features :: Range-based for loops Iterate collections with foreach std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); for (auto value : v) { std::cout << value << std::endl; } 15
  • 16. C++11 Features :: Smart pointers unique_ptr: Ownership of a memory resource it is not shared, but it can be transferred to another unique_ptr shared_ptr: Ownership of a memory resource should be shared weak_ptr: Holds a reference to an object managed by a shared_ptr, but does not contribute to the reference count; it is used to break dependency cycles. 16
  • 17. C++11 Features :: Lambdas Powerful feature borrowed from functional programming. You can use lambdas wherever a function object or a functor or a std::function is expected std::function<int(int)> lfib = [&lfib](int n) { return n < 2 ? 1 : lfib(n-1) + lfib(n-2); }; 17
  • 18. C++11 Features :: More Final and override non-member begin() and end() Initializer lists Object construction improvement Unrestricted unions User-defined literals static_assert Strongly-typed enums … 18
  • 19. Text Complementing C++ Libraries 19
  • 20. Complementing C++ C++11 standard library is missing utilities: HTTP Requests XML and JSON Big numbers Security Math 20
  • 21. C++ libs :: Boost Supports: Licence: AS IS Big community Lots of modules 21
  • 22. C++ libs :: Juce Supports: No WP (for now) Licence: Core: AS IS Other modules: GPL and commercial licence Very well written code Easy to use 22
  • 23. C++ libs :: Qt Supports: Licence: GPL and commercial licence Big community Lots of modules 23
  • 24. Text Putting stuff together JNI 24
  • 25. Calling JNI from Java Declare native methods private native void doSomethingNative (String str); Implement C method JNIEXPORT void Java_com_example_MyClass_doSomethingNative (JNIEnv *env, jobject obj, jstring s) { const char* const utf8 = env->GetStringUTFChars (s, nullptr); CharPointer_UTF8 utf8CP (utf8); String cppString (utf8CP); env->ReleaseStringUTFChars (s, utf8); // Do Something } 25
  • 26. Calling JNI from Java JNIEXPORT void Java_com_example_MyClass_doSomethingNative (JNIEnv *env, jobject obj, jstring s) Required macro to support Windows Java + package_name + className + methodName 26
  • 27. Calling C from Java JNIEXPORT void Java_com_example_MyClass_doSomethingNative (JNIEnv *env, jobject obj, jstring s) Structure to access all the JNI functions Java object that calls the native function Java method arguments 27
  • 28. Local and global references Local reference will be managed by java A global reference is managed by the developer Max number of java references = 512 28
  • 29. Calling Java from C public String myMethod (int[] array, boolean enabled) 29
  • 30. Calling Java from C public String myMethod (int[] array, boolean enabled) JNIEnv* env = getEnv(); jclass myJavaClass = env->GetObjectClass (myJavaObject); jmethodID myJavaMethod = env->GetMethodID(myJavaClass , ”methodName", "([I;Z)Ljava/lang/String"); jstring jresult = (jstring) env->CallObjectMethod (myJavaObject, myJavaMethod , myIntArray, myBoolean); // Do something env->DeleteLocalRef (jresult); env->DeleteLocalRef (myJavaClass); 30
  • 31. Calling Java from C public String myMethod (int[] array, boolean enabled) JNIEnv* env = getEnv(); jclass myJavaClass = env->GetObjectClass(myJavaObject); jmethodID myJavaMethod = env->GetMethodID(myJavaClass , ”methodName", "([I;Z)Ljava/lang/String"); jstring jresult = (jstring) env->CallObjectMethod (myJavaObject, myJavaMethod , myIntArray, myBoolean); // Do something env->DeleteLocalRef (jresult); env->DeleteLocalRef (myJavaClass); Must not be NULL This is the java object that has the method 31
  • 32. Calling Java from C public String myMethod (int[] array, boolean enabled) JNIEnv* env = getEnv(); jclass myJavaClass = env->GetObjectClass(myJavaObject); jmethodID myJavaMethod = env->GetMethodID(myJavaClass, ”myMethod", "([I;Z)Ljava/lang/String"); jstring jresult = (jstring) env->CallObjectMethod (myJavaObject, myJavaMethod , myIntArray, myBoolean); // Do something env->DeleteLocalRef Method signature (jresult); env->DeleteLocalRef (myJavaClass); 32
  • 33. Calling Java from C public String myMethod (int[] array, boolean enabled) JNIEnv* env = getEnv(); jclass myJavaClass = env->GetObjectClass(myJavaObject); jmethodID myJavaMethod = env->GetMethodID(myJavaClass, ”myMethod", "([I;Z)Ljava/lang/String"); jstring jresult = (jstring) env->CallObjectMethod (myJavaObject, myJavaMethod , myIntArray, myBoolean); // Do something env->DeleteLocalRef Method signature (jresult); env->DeleteLocalRef (myJavaClass); Type Signature Java Type Z Boolean B byte C char S short I int J long F float D double L class ; full qualified [type; tcylpases[] 33
  • 34. Calling Java from C public String myMethod (int[] array, boolean enabled) JNIEnv* env = getEnv(); jclass myJavaClass = env->GetObjectClass(myJavaObject); jmethodID myJavaMethod = env->GetMethodID(myJavaClass, ”myMethod", "([I;Z)Ljava/lang/String"); jstring jresult = (jstring) env->CallObjectMethod (myJavaObject, myJavaMethod , myIntArray, myBoolean); // Do something env->DeleteLocalRef Method signature (jresult); env->DeleteLocalRef (myJavaClass); 34
  • 35. Calling Java from C public String myMethod (int[] array, boolean enabled) JNIEnv* env = getEnv(); jclass myJavaClass = env->GetObjectClass(myJavaObject); jmethodID myJavaMethod = env->GetMethodID(myJavaClass , ”myMethod", "([I;Z)Ljava/lang/String"); jstring jresult = (jstring) env->CallObjectMethod (myJavaObject, myJavaMethod , myIntArray, myBoolean); // Do something env->DeleteLocalRef (jresult); env->DeleteLocalRef (myJavaClass); 35
  • 36. Calling Java from C public String myMethod (int[] array, boolean enabled) JNIEnv* env = getEnv(); jclass myJavaClass = env->GetObjectClass(myJavaObject); jmethodID myJavaMethod = env->GetMethodID(myJavaClass , ”myMethod", "([I;Z)Ljava/lang/String"); jstring jresult = (jstring) env->CallObjectMethod (myJavaObject, myJavaMethod , myIntArray, myBoolean); // Do something env->DeleteLocalRef (jresult); env->DeleteLocalRef (myJavaClass); 36
  • 37. Java and C++ object binding 37
  • 38. Java and C++ object binding Java (OO) C C++ (OO) 38
  • 39. Java and C++ object binding Java (OO) C C++ (OO) 39
  • 40. Foo Java implementation public class Foo { native void destroyCppInstanceNative(double ref); native long newCppInstanceNative(); native String getStringNative(double ref); private double _ref = 0; public Foo() { _ref = newCppInstanceNative(); } public String getString() { return getStringNative(_ref); } public void destroy() { destroyCppInstanceNative(_ref); } } 40
  • 41. Foo Java implementation JNIEXPORT long Java_com_example_Foo_newCppInstanceNative(JNIEnv *env, jobject obj) { Foo* newFoo = new Foo(); return (double)(newFoo); } JNIEXPORT jstring Java_com_example_Foo_getStringNative(JNIEnv *env, jobject obj, double ref) { Foo* foo = (Foo*)(ref); jstring jStringResult = env->NewStringUTF (foo->getString()); return jStringResult; } JNIEXPORT void Java_com_example_Foo_destroyCppInstanceNative(JNIEnv *env, jobject obj, double ref) { Foo* foo = (Foo*)(ref); delete foo; } 41
  • 42. Text Some advice to live happier with C++ 42
  • 43. Some advice Code quality! (Code conventions, code reviews, etc.) C++ is not a read only code, don’t put the blame on it Remember to initialise all the values in the constructor Use unity builds to speed up compilation time JNI could be painful. Check out djinni to generate cross-language type declarations and interface bindings. dropbox/djinni 43
  • 44. Final thoughts Pros Same core and with native UI/UX per platform Better performance Faster cross platform development Easier maintenance Cons Need to learn a new language Android apps will be bigger (code compiled for different architectures) Can be fixed distributing specific apk per each architecture 44