Infrastructure Middleware (Part 3): Android Runtime Core ...

44
Infrastructure Middleware (Part 3): Android Runtime Core & Native Libraries Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Transcript of Infrastructure Middleware (Part 3): Android Runtime Core ...

Page 1: Infrastructure Middleware (Part 3): Android Runtime Core ...

Infrastructure Middleware (Part 3):Android Runtime Core & Native Libraries

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Institute for Software Integrated Systems

Vanderbilt University Nashville, Tennessee, USA

Page 2: Infrastructure Middleware (Part 3): Android Runtime Core ...

2

Learning Objectives in this Part of the Lesson• Recognize key core Java

libraries that are part of the Android platform

Page 3: Infrastructure Middleware (Part 3): Android Runtime Core ...

3

Learning Objectives in this Part of the Lesson• Recognize key core Java

libraries that are part of the Android platform

• Recognize key core Android libraries that are part of theAndroid platform

Page 4: Infrastructure Middleware (Part 3): Android Runtime Core ...

4

Learning Objectives in this Part of the Lesson• Recognize key core Java

libraries that are part of the Android platform

• Recognize key core Android libraries that are part of theAndroid platform

• Recognize key native libraries that are part of the Androidplatform

C++

/C

Page 5: Infrastructure Middleware (Part 3): Android Runtime Core ...

5

Learning Objectives in this Part of the Lesson• Recognize key core Java

libraries that are part of the Android platform

• Recognize key core Android libraries that are part of theAndroid platform

• Recognize key native libraries that are part of the Androidplatform

Apps use core Java/Android libraries extensively; native libraries not as much

ART|Dalvik Virtual Machine

Page 6: Infrastructure Middleware (Part 3): Android Runtime Core ...

6

Overview of the Android Runtime:Core Java Libraries

Page 7: Infrastructure Middleware (Part 3): Android Runtime Core ...

7

C/Ja

va/

JNI

Android Runtime: Core Java Libraries• Android contains many (but not all) core Java

libraries in the java.* & javax.* packages

See en.wikipedia.org/wiki/Comparison_of_Java_and_Android_API

ART|Dalvik Virtual Machine

java

IntegerStringThread…

langArrayListHashMapVector…

utilFileFileInputStreamFileOutputStream…

io …

Page 8: Infrastructure Middleware (Part 3): Android Runtime Core ...

8

Android Runtime: Core Java Libraries• Android contains many (but not all) core Java

libraries in the java.* & javax.* packages, e.g.• Java Thread

See developer.android.com/reference/java/lang/Thread.html

C/Ja

va/

JNI

ART|Dalvik Virtual Machine

Page 9: Infrastructure Middleware (Part 3): Android Runtime Core ...

9See developer.android.com/reference/java/lang/Thread.html

Process A Process B Process C

Android Runtime: Core Java Libraries• Android contains many (but not all) core Java

libraries in the java.* & javax.* packages, e.g.• Java Thread

A Java thread is a unit of computation that runs in the context of a process

C/Ja

va/

JNI

ART|Dalvik Virtual Machine

Page 10: Infrastructure Middleware (Part 3): Android Runtime Core ...

10

Java threads running in a process can communicate with each other via shared objects or message passing

Android Runtime: Core Java Libraries• Android contains many (but not all) core Java

libraries in the java.* & javax.* packages, e.g.• Java Thread

Process A Process B Process C

C/Ja

va/

JNI

ART|Dalvik Virtual Machine

See en.wikipedia.org/wiki/Thread_(computing)

Page 11: Infrastructure Middleware (Part 3): Android Runtime Core ...

11

Android Runtime: Core Java Libraries

Process A Process B Process C

Each Java thread leverages unique “state” from the underlying Linux

kernel thread, e.g., a stack, a program counter, & other registers

C/Ja

va/

JNI

ART|Dalvik Virtual Machine

• Android contains many (but not all) core Java libraries in the java.* & javax.* packages, e.g.• Java Thread

See en.wikipedia.org/wiki/Thread_(computing)#Processes.2C_kernel_threads.2C_user_threads.2C_and_fibers

Page 12: Infrastructure Middleware (Part 3): Android Runtime Core ...

12

Android Runtime: Core Java Libraries• Android contains many (but not all) core Java

libraries in the java.* & javax.* packages, e.g.• Java Thread C/

Java

/JN

I

ART|Dalvik Virtual Machine

See en.wikipedia.org/wiki/Thread_(computing)#Processes.2C_kernel_threads.2C_user_threads.2C_and_fibers

Process A Process B Process C

Java dynamic & static objects can be shared across Java threads (i.e., this “state” is common)

Page 13: Infrastructure Middleware (Part 3): Android Runtime Core ...

13

Android Runtime: Core Java Libraries

See developer.android.com/reference/java/util/concurrent/package-summary.html

C/Ja

va/

JNI• Android contains many (but not all) core Java

libraries in the java.* & javax.* packages, e.g.• Java Thread• Java synchronizers

ART|Dalvik Virtual Machine

Process A Process B Process C

e.g., reentrant locks, stamped locks, semaphores, condition

objects, phasers, etc.

Page 14: Infrastructure Middleware (Part 3): Android Runtime Core ...

14

Android Runtime: Core Java Libraries

See en.wikipedia.org/wiki/Race_condition

C/Ja

va/

JNI• Android contains many (but not all) core Java

libraries in the java.* & javax.* packages, e.g.• Java Thread• Java synchronizers

ART|Dalvik Virtual Machine

Process A Process B Process C

Java synchronizers are used to prevent race conditions

Page 15: Infrastructure Middleware (Part 3): Android Runtime Core ...

15

Android Runtime: Core Java Libraries• Android contains many (but not all) core Java

libraries in the java.* & javax.* packages, e.g.• Java Thread• Java synchronizers• Java networking

See developer.android.com/reference/java/net/HttpURLConnection.html

Network

Java network programming mechanisms can exchange

data between Android devices & remote servers

C/Ja

va/

JNI

ART|Dalvik Virtual Machine

Page 16: Infrastructure Middleware (Part 3): Android Runtime Core ...

16

Android Runtime: Core Java Libraries• Android contains many (but not all) core Java

libraries in the java.* & javax.* packages, e.g.• Java Thread• Java synchronizers• Java networking• Java I/O & files

See docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html

Java file mechanisms can store data persistently on

Android devices

C/Ja

va/

JNI

ART|Dalvik Virtual Machine

Page 17: Infrastructure Middleware (Part 3): Android Runtime Core ...

17

Overview of the Android Runtime:

Core Android Libraries

Page 18: Infrastructure Middleware (Part 3): Android Runtime Core ...

18

Android Runtime: Core Android Libraries• Android contains thousands of classes in the

android.* packages

See www.makelinux.net/android/classes

C/Ja

va/

JNI

ART|Dalvik Virtual Machine

Page 19: Infrastructure Middleware (Part 3): Android Runtime Core ...

19

Android Runtime: Core Android Libraries• Android contains thousands of classes in the

android.* packages, e.g.• Concurrency

See www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

C/Ja

va/

JNI

ART|Dalvik Virtual Machine

Page 20: Infrastructure Middleware (Part 3): Android Runtime Core ...

20

Android Runtime: Core Android Libraries• Android contains thousands of classes in the

android.* packages, e.g.• Concurrency

• Handlers, Messages, & Runnables (HaMeR)

C/Ja

va/

JNI

Loop

er

Message

Message

Message

Message

Message

Message Queue

Runnable

Message

Background Thread A

Handler

Handler

Background Thread BUI Thread

(main thread)Message

Operations run in one or more threads & publish their

results to the UI thread

See developer.android.com/training/multiple-threads/communicate-ui.html

ART|Dalvik Virtual Machine

Page 21: Infrastructure Middleware (Part 3): Android Runtime Core ...

21

Android Runtime: Core Android Libraries• Android contains thousands of classes in the

android.* packages, e.g.• Concurrency

• Handlers, Messages, & Runnables (HaMeR)

• AsyncTask

C/Ja

va/

JNI

Loop

er

Message

Message

Message

Message

Message

Message Queue

Message

AsyncTask

Similar to HaMeRframework, but

without using threads, handlers, messages, or runnables directly

See developer.android.com/reference/android/os/AsyncTask.html

UI Thread(main thread)

ART|Dalvik Virtual Machine

Page 22: Infrastructure Middleware (Part 3): Android Runtime Core ...

22

Android Runtime: Core Android Libraries• Android contains thousands of classes in the

android.* packages, e.g.• Concurrency• App components

C/Ja

va/

JNI

ART|Dalvik Virtual Machine

Page 23: Infrastructure Middleware (Part 3): Android Runtime Core ...

23See developer.android.com/guide/components/fundamentals.html#Components

Android Runtime: Core Android Libraries• Android contains thousands of classes in the

android.* packages, e.g.• Concurrency• App components

• Building blocks of mobile apps that provide hooks that Android uses to control an app’s lifecycle

C/Ja

va/

JNI

Content Provider

ServiceActivity

Broadcast Receiver

ART|Dalvik Virtual Machine

Page 24: Infrastructure Middleware (Part 3): Android Runtime Core ...

24

Android Runtime: Core Android Libraries• Android contains thousands of classes in the

android.* packages, e.g.• Concurrency• App components• Binder IPC framework

C/Ja

va/

JNI

ART|Dalvik Virtual Machine

Page 25: Infrastructure Middleware (Part 3): Android Runtime Core ...

25

Android Runtime: Core Android Libraries• Android contains thousands of classes in the

android.* packages, e.g.• Concurrency• App components• Binder IPC framework

• Enables sync & asynccommunication between components on a device

C/Ja

va/

JNI

See elinux.org/Android_Binder

ART|Dalvik Virtual Machine

Page 26: Infrastructure Middleware (Part 3): Android Runtime Core ...

26

Android Runtime: Core Android Libraries• The source code for all the core Java & Android libraries is available online

See source.android.com

Page 27: Infrastructure Middleware (Part 3): Android Runtime Core ...

27

Overview of Android Native C/C++ Libraries

Page 28: Infrastructure Middleware (Part 3): Android Runtime Core ...

28

Android Native C/C++ Libraries

• Although Android apps are written using Java APIs, implementations of these APIs are often written in C/C++

C++

/C

Goal is to enhance system performance w/out sacrificing developer productivity

Performance Productivity

Page 29: Infrastructure Middleware (Part 3): Android Runtime Core ...

29

Android Native C/C++ Libraries

• Although Android apps are written using Java APIs, implementations of these APIs are often written in C/C++• Java & C/C++ are combined via the

Java Native Interface (JNI)

C++

/C

See developer.android.com/training/articles/perf-jni.html

Page 30: Infrastructure Middleware (Part 3): Android Runtime Core ...

30

Android Native C/C++ Libraries

• Although Android apps are written using Java APIs, implementations of these APIs are often written in C/C++• Java & C/C++ are combined via the

Java Native Interface (JNI)• JNI defines a standard way for

managed Java code to interact with native code written in C/C++

C++

/C

Page 31: Infrastructure Middleware (Part 3): Android Runtime Core ...

31

Android Native C/C++ Libraries

See developer.android.com/ndk

C++

/C• Android’s Native Development Kit (NDK) allows

the implementation of apps & services using native C/C++ code

Page 32: Infrastructure Middleware (Part 3): Android Runtime Core ...

32

Android Native C/C++ Libraries

• Android’s Native Development Kit (NDK) allows the implementation of apps & services using native C/C++ code• Using the NDK on portions of code can help

enhance performance by minimizing latency, maximizing throughput, & conserving key system resources

C++

/C

Resist the urge to develop all of your apps using the NDK!

Page 33: Infrastructure Middleware (Part 3): Android Runtime Core ...

33

Android Native C/C++ Libraries

• Android’s Native Development Kit (NDK) allows the implementation of apps & services using native C/C++ code• Using the NDK on portions of code can help

enhance performance by minimizing latency, maximizing throughput, & conserving key system resources

• It can also be used to integrate existing C/C++ libraries into Android apps

C++

/C

Page 34: Infrastructure Middleware (Part 3): Android Runtime Core ...

34

Android Native C/C++ Libraries

• System C library• bionic libc

• Surface Manager• display management

• Media Framework• audio/video streaming

• FreeType• library for rendering fonts

• Webkit• web browser engine

• OpenGL ES, SGL• graphics engines

• SQLite• relational database engine

• SSL• secure sockets layer

Android native libraries are open source & often have Java wrapper facades

C++

/C

Page 35: Infrastructure Middleware (Part 3): Android Runtime Core ...

35

Android Native C/C++ Libraries

• System C library• bionic libc

• Surface Manager• display management

• Media Framework• audio/video streaming

• FreeType• library for rendering fonts

• Webkit• web browser engine

• OpenGL ES, SGL• graphics engines

• SQLite• relational database engine

• SSL• secure sockets layer

Enables developers to write native system services for Android

C++

/C

Page 36: Infrastructure Middleware (Part 3): Android Runtime Core ...

36

Android Native C/C++ Libraries

• System C library• bionic libc

• Surface Manager• display management

• Media Framework• audio/video streaming

• FreeType• library for rendering fonts

• Webkit• web browser engine

• OpenGL ES, SGL• graphics engines

• SQLite• relational database engine

• SSL• secure sockets layer

Composites 2D & 3D graphic layers from multiple apps

C++

/C

Page 37: Infrastructure Middleware (Part 3): Android Runtime Core ...

37

C++

/C

Android Native C/C++ Libraries

• System C library• bionic libc

• Surface Manager• display management

• Media Framework• audio/video streaming

• FreeType• library for rendering fonts

• Webkit• web browser engine

• OpenGL ES, SGL• graphics engines

• SQLite• relational database engine

• SSL• secure sockets layer

Supports audio-video streaming in the background

Page 38: Infrastructure Middleware (Part 3): Android Runtime Core ...

38

C++

/C

Android Native C/C++ Libraries

• System C library• bionic libc

• Surface Manager• display management

• Media Framework• audio/video streaming

• FreeType• library for rendering fonts

• Webkit• web browser engine

• OpenGL ES, SGL• graphics engines

• SQLite• relational database engine

• SSL• secure sockets layer

Renders bitmap & vector fonts

Page 39: Infrastructure Middleware (Part 3): Android Runtime Core ...

39

Android Native C/C++ Libraries

• System C library• bionic libc

• Surface Manager• display management

• Media Framework• audio/video streaming

• FreeType• library for rendering fonts

• Webkit• web browser engine

• OpenGL ES, SGL• graphics engines

• SQLite• relational database engine

• SSL• secure sockets layer

Framework used on mobile & non-mobile platforms for web browsing

C++

/C

Page 40: Infrastructure Middleware (Part 3): Android Runtime Core ...

40

Android Native C/C++ Libraries

Supports 2D & 3D vector graphics, e.g., often used for gaming apps

C++

/C• System C library

• bionic libc• Surface Manager

• display management• Media Framework

• audio/video streaming• FreeType

• library for rendering fonts

• Webkit• web browser engine

• OpenGL ES, SGL• graphics engines

• SQLite• relational database engine

• SSL• secure sockets layer

Page 41: Infrastructure Middleware (Part 3): Android Runtime Core ...

41

Android Native C/C++ Libraries

Relational database engine that performs CRUD operations on persistent data

C++

/C• System C library

• bionic libc• Surface Manager

• display management• Media Framework

• audio/video streaming• FreeType

• library for rendering fonts

• Webkit• web browser engine

• OpenGL ES, SGL• graphics engines

• SQLite• relational database engine

• SSL• secure sockets layer

Page 42: Infrastructure Middleware (Part 3): Android Runtime Core ...

42

Android Native C/C++ Libraries

Ensures confidentiality & integrity for web interactions (e.g., e-commerce)

C++

/C• System C library

• bionic libc• Surface Manager

• display management• Media Framework

• audio/video streaming• FreeType

• library for rendering fonts

• Webkit• web browser engine

• OpenGL ES, SGL• graphics engines

• SQLite• relational database engine

• SSL• secure sockets layer

Page 43: Infrastructure Middleware (Part 3): Android Runtime Core ...

43

Android Native C/C++ Libraries

Native C/C++ libraries use non-Java concurrency libraries, e.g., POSIX pthreads

C++

/C• System C library

• bionic libc• Surface Manager

• display management• Media Framework

• audio/video streaming• FreeType

• library for rendering fonts

• Webkit• web browser engine

• OpenGL ES, SGL• graphics engines

• SQLite• relational database engine

• SSL• secure sockets layer

Page 44: Infrastructure Middleware (Part 3): Android Runtime Core ...

44

End of Infrastructure Middleware (Part 3): the Android Runtime

Core & Native Libraries