NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) •...

18
NATIVE APPS HYBRID APPS

Transcript of NATIVE APPS HYBRID APPS - uniroma1.itberaldi/MACC/10_HYBRID_OK_19.pdf · Example (java side) •...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Recap

JS

Android

C/CPP