The document discusses using C++ for cross-platform mobile app development. It covers using C++11 features and libraries like Boost and Qt to build the core app logic. JNI is used to integrate C++ code with Android's Java framework, allowing native UI and performance while reusing business logic across platforms. While learning C++ is required, this approach can offer better performance, user experience and code maintenance than alternatives like PhoneGap that use non-native UI wrappers.
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
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
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
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
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