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

Post on 04-Jun-2020

2 views 0 download

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

NATIVE APPS HYBRID APPS

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

Example (java side)

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

Example (native side)

Pointer to the ‘JNI Environment’

Makes the function name visibleto the linker

JNI function (provided by the JNI environment)

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

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>

NativeàJava

JNI

xxxxx

xxxx

JNI->findClass(“ccc”)callMethod(“m1”)

Java side

Native sideExploits java reflection

yyyxxxxx

xx

class, methodname

JVM

ccc

m1

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

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

Xmarine (basic idea)

Example

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

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

Android calls JS• Write a js string interpreted by the JS Engine at run-time

Hybrid apps: Cordova

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

Recap

JS

Android

C/CPP