The Android Runtime Environment
A quick, guided tour through the VM and the Core Libraries
Jrg Pleumann Noser Engineering AG, Winterthur 8801
The Android Runtime Environment
AGENDA
OVERVIEW VIRTUAL MACHINE CORE LIBRARIES SUMMARY
The Android Runtime Environment
Overview (I)
What is Android? Not (just) a single mobile phone A complete software stack for mobile devices Kernel, middleware, and basic applications Competes with Windows Mobile, Symbian etc. Android is open- source Free beer no license fees Free speech no strings attached Apache 2 license where possible Open Handset Alliance (OHA)
The Android Runtime Environment
Overview (II)
Technical cornerstones Linux- Kernel Java programming language Virtual machine (that is not a JVM) Application framework
How does that all fit together?
The Android Runtime Environment
The Android Runtime Environment
Dalvik VM (I)
Bytecode interpreter for mobile systems Slow CPU (250- 500 MHz) Little RAM ( 64 MB) No swap space Battery- powered Should support multiple instances Efficiency is critical CPU / battery Memory
Let's party like it's 1999... ;- )
The Android Runtime Environment
Dalvik VM (II)
Traditional JVM: Modified stack machine Operands and results on a stack Additional local variables Interpreter (highly simplified)
while (true) { char c = fetchAndDecode(); switch (c) { case '#': doPush(getNumber()); case '+': doAdd(); case '-': doSub(); ... } }
break; break; break;
Dispatch means overhead, so avoid it
The Android Runtime Environment
Dalvik VM (III)
Dalvik VM: Register machine All temporary values in registers Implicit invocation stack Custom instruction set Higher semantic density of code Less instructions do the same job New instructions for problem cases Very efficient interpreter
Can you give an example?
The Android Runtime Environment
Instruction set (I)
Ex ample 1: Hello, world!
System.out.println("Hallo Welt!");
JVM byte code
getstatic java.lang.System.out Ljava/io/PrintStream; ldc "Hallo Welt!" invokevirtual java/io/PrintStream/println(L...;)V return
DVM byte code
sget-object v0, java.lang.System.out Ljava/io/PrintStream; const-string v1, "Hallo Welt!" invoke-virtual {v0, v1}, java/io/PrintStream/println(L...;)V return-void
The Android Runtime Environment
10
Instruction set (II)
Ex ample 2: A simple loop over an array
public static long sumArray(int[] arr) { long sum = 0; for (int i : arr) { sum += i; } return sum; }
The Android Runtime Environment
11
Instruction set (III)
JVM byte code
000b: 000d: 000f: 0012: 0013: 0015: 0016: 0018: 0019: 001b: 001c: 001d: 001e: 0021: iload 05 iload 04 if_icmpge 0024 aload_3 iload 05 iaload istore 06 lload_1 iload 06 i2l ladd lstore_1 iinc 05, #+01 goto 000b
30% less instructions in average case
DVM byte code
0007: 0009: 000b: 000c: 000d: 000f: if-ge v0, v2, 0010 aget v1, v8, v0 int-to-long v5, v1 add-long/2addr v3, v5 add-int/lit8 v0, v0, #int 1 goto 0007
The Android Runtime Environment
12
Instruction set (IV)
Ex ample 3: Initializing an array
public class Demo { private static final char[] DATA = { 'N','o','s','e','r', 'k','n','o','w','s', 'A','n','d','r','o','i','d' }; }
The Android Runtime Environment
13
Instruction set (V)
JVM byte code
0000: 0002: 0004: 0005: 0006: 0008: 005e: 005f: 0061: 0063: 0064: 0067: bipush 17 newarray char dup iconst_0 bipush 78 castore dup bipush 16 bipush 100 castore putstatic DATA return
Initializing the array from a table in memory (new instruction) DVM byte code
0000: 0002: 0004: 0007: 0009: 000a: const/16 v0, #int 17 new-array v0, v0, [C fill-array-data v0, 0a sput-object v0, DATA:[C return-void array-data (21 units) 'N', 'o', 's', 'e', 'r' ...
The Android Runtime Environment
14
Binary (I)
Class files don't work (obviously) Dalvik Executable Format (DEX) Size reduction Less instructions (as shown) Multiple classes in one DEX file Shared constant pools No compression Still smaller than JAR in average case Allows to mmap() the DEX file
The Android Runtime Environment
15
Binary (II)
The big picture Eclipse Java Compiler JAR Tool Dx Converter Dalvik VM
HelloWorld.jar
Hello.class
HelloWorld.jar
classes.dex
World.class strings.txt image.png strings.txt image.png
The Android Runtime Environment
16
Random other features (I)
Byte code verification Yes, equivalent to what a JVM does Just- in- time compilation Not yet, but work in progress Compacting garbage collection No, processes usually short- lived Java Native Interface (JNI) Yes, native SDK to be announced
The Android Runtime Environment
17
Random other features (II)
Command line interface Yes, via the Android Debug Bridge (ADB) Supports usual parameters
The Android Runtime Environment
18
The Android Runtime Environment
19
Core libraries (I)
Set of libraries close to the VM Each device is supposed to provide them Android framework builds upon them Three (public) parts Dalvik VM- specific libraries System info, debugging, ... Java compatibility libraries Base and utility classes Third- party utility libraries Apache HttpClient 4.0
dalvik.*
java.* javax.*
org.apache.http.*
The Android Runtime Environment
20
java.io java.lang java.lang.annotation java.lang.ref java.lang.reflect java.m ath java.net java.nio java.nio.channels java.nio.channels.spi java.nio.charset java.nio.charset.spi java.security java.security.acl java.security.cert java.security.interfaces java.security.spec java.sql
ll Fu Fu y ll y su su pp pp or or d ed tt e
java.tex t java.util java.util.concurrent java.util.concurrent.atom ic java.util.concurrent.locks java.util.jar java.util.logging java.util.prefs java.util.regex java.util.zip javax .crypto javax .crypto.interfaces javax .crypto.spec javax .net javax .net.ssl javax .security.cert javax .sql
ll Fu Fu ll y p p su su p po r r tt ed ed
org.x m l.sax org.x m l.sax .ex t org.x m l.sax .helpers
javax .x m l javax .x m l.parsers org.w3c.dom
javax .security.auth javax .security.auth.callbck javax .security.auth.login javax .security.auth.x 500
ll Fu Fu
y ll y
su su
pp pp or or d ed tt e ii o n r r tt ed ed po po
ll d O O
r er s rs er ve p p su su
r r tt Pa Pa y ll y
The Android Runtime Environment
21
Core Libraries (III)
Implementation (contributed largely by Noser) 1900 API classes, 3200 classes total (ex cl. Http) Partly taken from Apache Harmony Partly written from scratch Optimization java.util.regex java.text JNI JNI java.security java.math
ICU
OpenSSL
The Android Runtime Environment
22
Core Libraries (IV)
What do we have here? Not a Micro Edition Looks more like a desktop JRE GUI not a concern (no AWT / Swing) Can we be more precise? No, Android does not follow a JSR Mostly compatible to a subset of J2SE 1.5 / 5.0
Mostly? Where are the differences?
The Android Runtime Environment
23
Core packages
ClassLoader based on DEX No defineClass(byte[]) No instrumentation, byte code weaving Thread lacks deprecated methods No suspend(), resume(), or stop() No exceptions, these methods are no- ops (unfortunately)! No SecurityManager in use Separate processes Each application gets a user Linux ID Permissions in AndroidManifest.x ml java.lang
java.net
java.io
The Android Runtime Environment
24
Internationalization
Based on ICU 3.8.1 Data differs slightly from JDK Ex pect small differences in java.lang.Character java.util.Locale java.util.Formatter Limited set of locales Depends on what OEM / carrier selects Don't rely on a specific locale! java.lang
java.util
java.text
The Android Runtime Environment
25
Regular expressions
Based on ICU 3.8.1 Efficient native implementation Syntax / semantics Differs minimally from JDK Not all flags supported Collisions unlikely Caution with highly complex cases! java.util.regex
The Android Runtime Environment
26
Security
Hybrid implementation Bouncy Castle as JCE Provider OpenSSL for time- critical stuff Supported algorithms Set differs slighly from JDK All relevant ones are there Others via SPI Keystore is special Bouncy Castle (.bks) format No Android- specific keytool java.crypto
javax.crypto
javax.security
javax.net.ssl
The Android Runtime Environment
27
Database
JDBC 2.0 SQLite driver is limited Not all data types supported Some ResultSet methods throw UnsupportedOperationEx ception Other drivers via SPI Alternative Classes in android.database.sqlite Better integration with Activities and user interface java.sql
javax.sql
The Android Runtime Environment
28
XML
DOM Level 2 Core No XPath, XSL, ... Roughly the state of 2001 Should be sufficient for mobile applications (so we thought) SAX Version 2 SAXParser and DocumentBuilder are non- validating Based on KXML2 (moving to Ex pat) Other implementations via SPI javax.xml.*
org.w3c.dom.*
org.xml.sax.*
The Android Runtime Environment
29
Core Libraries (V)
Portability between Android & JRE Not a real problem Need to be aware of the few pitfalls Some examples for ported projects Jetty web server BeanShell scripting engine Coming: integration with Harmony Improved compatibility with JRE Both projects will benefit
The Android Runtime Environment
30
Summary
Dalvik VM Very efficient byte code interpreter Register machine Executes transformed Java byte code Core Libraries Feature- rich system library Subset of desktop JRE 1.5 Some tiny differences Reuse of your knowledge and tools
Thank you for your attention!
Jrg Pleumann Noser Engineering AG