native apps hybrid apps - uniroma1.itberaldi/macc/10_hybrid_ok_19.pdf · example (java side) •...
TRANSCRIPT
![Page 1: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/1.jpg)
NATIVE APPS HYBRID APPS
![Page 2: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/2.jpg)
Why to use “Native code”?
• Call C/C++ libraries from Java may be required for• Implementing critical code (performance reason)
• Using existing projects (frequentily in C)
• OpenCV (Computer Vision)
• TensorflowLight
• ..
• Recall: indeed some java libraries are wrappers of C libraries
![Page 3: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/3.jpg)
Example (java side)
![Page 4: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/4.jpg)
Example (java side)• System.loadLibrary
• The compiled .cpp produces a shared library (a library that can be loaded at run-time)
• The shared library contains the actual executable code• The code uses the shared library through an address that is
relocated by the dynamic linker• call xxx à call yyy
• Native• This keywords allows to mark a method as implemented in C/C++• A Java call to a native method triggers the execution of the native
code
![Page 5: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/5.jpg)
Example (native side)
Pointer to the ‘JNI Environment’
Makes the function name visibleto the linker
JNI function (provided by the JNI environment)
![Page 6: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/6.jpg)
Example (native side)• extern "C" JNIEXPORT type JNICALL fname
• extern= fname have 'C' linkage• JNIEXPORT rtype JNICALL fname = function fname appears in the
dynamic table of the shared libray; return rtype
• JNIEenv pointers to the JNI ‘Enviroment’• Lots of functions available to the native side related to java• Notably, Reflection java capabilities, data representation
conversion
![Page 7: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/7.jpg)
JavaàNative
xxxxx
yyyxx
Dynamic Table
xxxxx
Java side
yyy
JNI
use JNI run-time(ex: newString…)
Native side
Note: source file contains<jni.h>
![Page 8: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/8.jpg)
NativeàJava
JNI
xxxxx
xxxx
JNI->findClass(“ccc”)callMethod(“m1”)
Java side
Native sideExploits java reflection
yyyxxxxx
xx
class, methodname
JVM
ccc
m1
![Page 9: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/9.jpg)
Asynchronous interaction
Java Native Java Native Java Java Native
1. Blocking call2. Native calls Java asynch3. Blocking java thread (avoids native to find and call a java method)
1 23
Recommended
![Page 10: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/10.jpg)
Xmarine (basic idea)Mono = Common Language Runtime (similar to jvm)MCW = Mono Callable WrapperACW = Android Callable Wrapper
Note: Native code (not only the app logic)
Native code = CLR + application logic
![Page 11: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/11.jpg)
Xmarine (basic idea)
![Page 12: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/12.jpg)
Example
![Page 13: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/13.jpg)
Binding JavaScript code to Android code• It is possible to create interfaces between JavaScript code
and Android code.
• In other words, JavaScript code can call a method implemented in ‘android’ (java/kotlin)
• And android can call JS
![Page 14: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/14.jpg)
JS calls Android code
• Public methods are seen as JS function
• Only simple data can be passed
• JSON can be useful in this respect
Android Code
HTML + JS..
Android.test()
Add Interface (C,“Android”)
Android.test();
Class Ctest
public method
![Page 15: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/15.jpg)
Android calls JS• Write a js string interpreted by the JS Engine at run-time
![Page 16: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/16.jpg)
Hybrid apps: Cordova
![Page 17: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/17.jpg)
Recap• JavaàNative
• JNI, native methods translated into executable code, linked and available as a library
• NativeàJava• No executable code produced at compile time.• Exploits Java Reflection (any method can me called)
• JSàAndroid• Android code is made available to the JS interpreter (an android
method is visible in the JS namespace)
• AndroidàJS• Evaluate JS code at run-time (any string can be executed)• Similar to reflection
![Page 18: NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) • System.loadLibrary • The compiled .cpp produces a shared library (a library that](https://reader034.vdocument.in/reader034/viewer/2022042314/5f0220357e708231d402b2e4/html5/thumbnails/18.jpg)
Recap
JS
Android
C/CPP