Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
The Android RuntimeEnvironmentA quick, guided tour through the VM and the Core Libraries
Jörg Pleumann
Noser Engineering AG, Winterthur
8801
The Android Runtime Environment 2
OVERVIEW
VIRTUAL MACHINE
CORE LIBRARIES
SUMMARY
AGENDA
The Android Runtime Environment 3
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 4
Overview (II)
Technical cornerstones
Linux- Kernel
Java programming language
Virtual machine (that is not a JVM)
Application framework
How does thatall f it together?
The Android Runtime Environment 5
The Android Runtime Environment 6
Dalvik VM (I)
Bytecode interpreter for mobile systems
Slow CPU (250- 500 MHz)
Litt le RAM (≥ 64 MB)
No swap space
Battery- powered
Should support mult iple instances
Efficiency is crit ical
CPU / battery
MemoryLet's party likeit 's 1999... ;- )
The Android Runtime Environment 7
Dalvik VM (II)
Tradit ional JVM: Modified stack machine
Operands and results on a stack
Addit ional local variables
Interpreter (highly simplif ied)
while (true) { char c = fetchAndDecode(); switch (c) { case '#': doPush(getNumber()); break; case '+': doAdd(); break; case '-': doSub(); break; ... }}
Dispatch means overhead, so avoid it
The Android Runtime Environment 8
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 eff icient interpreter Can you givean example?
The Android Runtime Environment 9
Instruction set (I)
getstatic java.lang.System.out Ljava/io/PrintStream;ldc "Hallo Welt!"invokevirtual java/io/PrintStream/println(L...;)Vreturn
sget-object v0, java.lang.System.out Ljava/io/PrintStream;const-string v1, "Hallo Welt!"invoke-virtual {v0, v1}, java/io/PrintStream/println(L...;)Vreturn-void
System.out.println("Hallo Welt!");
JVM byte code
DVM byte code
Example 1: Hello, world!
The Android Runtime Environment 10
Instruction set (II)
public static long sumArray(int[] arr) { long sum = 0;
for (int i : arr) { sum += i; }
return sum;}
Example 2: A simple loop over an array
The Android Runtime Environment 11
Instruction set (III)
000b: iload 05000d: iload 04000f: if_icmpge 00240012: aload_30013: iload 050015: iaload0016: istore 060018: lload_10019: iload 06001b: i2l001c: ladd001d: lstore_1001e: iinc 05, #+010021: goto 000b
0007: if-ge v0, v2, 00100009: aget v1, v8, v0000b: int-to-long v5, v1000c: add-long/2addr v3, v5000d: add-int/lit8 v0, v0, #int 1000f: goto 0007
JVM byte code
DVM byte code
30% less instructionsin average case
The Android Runtime Environment 12
Instruction set (IV)
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' };}
Example 3: Init ializing an array
The Android Runtime Environment 13
Instruction set (V)
0000: bipush 170002: newarray char0004: dup0005: iconst_00006: bipush 780008: castore …005e: dup005f: bipush 160061: bipush 1000063: castore0064: putstatic DATA0067: return
0000: const/16 v0, #int 170002: new-array v0, v0, [C0004: fill-array-data v0, 0a0007: sput-object v0, DATA:[C0009: return-void000a: array-data (21 units) 'N', 'o', 's', 'e', 'r' ...
JVM byte code
DVM byte code
Init ializing the arrayfrom a table in memory
(new instruction)
The Android Runtime Environment 14
Binary (I)
Class files don't work (obviously)
Dalvik Executable Format (DEX)
Size reduction
Less instructions (as shown)
Mult iple 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
EclipseJava
CompilerJARTool
DxConverter
DalvikVM
HelloWorld.jar HelloWorld.jar
Hello.classclasses.dex
World.class
strings.txt
image.png
strings.txt
image.png
The Android Runtime Environment 16
Random other features (I)
Byte code verif ication
Yes, equivalent to what a JVM does
Just- in- t ime compilat ion
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- specif ic libraries
System info, debugging, ...
Java compatibility libraries
Base and utility classes
Third- party ut ility libraries
Apache HttpClient 4.0
java.* javax.*
org.apache.http.*
dalvik.*
The Android Runtime Environment 20
Part ly supported
Older version
Fully supported
Fully supported
Fully supported
java.textjava.ut iljava.ut il.concurrentjava.ut il.concurrent.atomicjava.ut il.concurrent.locksjava.ut il.jarjava.ut il.loggingjava.ut il.prefsjava.ut il.regexjava.ut il.zip
javax.cryptojavax.crypto.interfacesjavax.crypto.specjavax.netjavax.net.ssljavax.security.certjavax.sql
java.iojava.langjava.lang.annotat ionjava.lang.refjava.lang.ref lectjava.mathjava.netjava.niojava.nio.channelsjava.nio.channels.spijava.nio.charsetjava.nio.charset.spijava.securityjava.security.acljava.security.certjava.security.interfacesjava.security.specjava.sql
javax.security.authjavax.security.auth.callbckjavax.security.auth.loginjavax.security.auth.x500
javax.xmljavax.xml.parsers
org.w3c.dom
org.xml.saxorg.xml.sax .ex torg.xml.sax .helpers
Fully supported
Fully supported
Fully supported
Older version
Part ly supported
The Android Runtime Environment 21
Core Libraries (III)
Implementation (contributed largely by Noser)
1900 API classes, 3200 classes total (excl. Http)
Part ly taken from Apache Harmony
Part ly writ ten from scratch
Optimization
java.util.regex
java.text
java.security
java.math
ICU
OpenSSL
JNIJNI
The Android Runtime Environment 22
Core Libraries (IV)
What do we have here?
Not a Micro Edit ion
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 subsetof 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 areno- ops (unfortunately)!
No SecurityManager in use
Separate processes
Each application gets a user Linux ID
Permissions in AndroidManifest.xml
java.lang
java.net
java.io
The Android Runtime Environment 24
Internationalization
Based on ICU 3.8.1
Data differs slightly from JDK
Expect 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 specif ic 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 f lags 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 t ime- crit ical 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- specif ic 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 throwUnsupportedOperationException
Other drivers via SPI
Alternative
Classes in android.database.sqlite
Better integration with Activit ies anduser 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 mobileapplications (so we thought)
SAX Version 2
SAXParser and DocumentBuilderare non- validating
Based on KXML2 (moving to Expat)
Other implementations via SPI
javax.xml.*
org.xml.sax.*
org.w3c.dom.*
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 script ing 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 t iny differences
Reuse of your knowledge and toolsThank you foryour attention!