text
stringlengths
0
1.99k
--[ 1 - A Bit of History
> And you may find yourself inside an Android Runtime.
> And you may ask yourself - Well ... How did I get here?
It's 2008. The HTC Dream has just been released with a whopping 528 MHz
Processor and 192Mb of RAM. Java was chosen to be the language used to
program Android apps. People know Java. People already programmed apps for
mobile devices in Java ME, so it was brought to Android. Instead of using
the stack-based Java Virtual Machine (JVM), the register-based Dalvik
Virtual Machine (DVM) found its way into Android. The dalvikvm executable
aimed at having a smaller memory footprint than the jvm, and Dalvik
bytecode is easy to transpile from Java bytecode. It differs in some
specifications that surely will not come back to bite us `:)`.
For app developers this meant they could write Java code using the Android
Software Development Kit (SDK) that would take the Java source code,
compile it with a Java compiler like javac to Java bytecode, run Java-based
plugins for optimizations and obfuscations (such as ProGuard), and then
invoke the Dalvik compiler dx to create Dalvik Executable (.dex) files. The
popular plugin ProGuard renamed method and class identifiers, creating a
mapping.txt file that the developer can use to make sense of error
information, such as stack traces. The .dex files were bundled in an
Android Package (APK) zip, which in turn was distributed to devices, where
the dalvikvm interprets the apps' code. Android provided a
re-implementation of standard Java libraries, which also kept some lawyers
busy.
```txt
+-------------+ +-------------+
| source.java | | library.jar |
++------------+ ++------------+
| |
+v--------------+ | +-------------+
| tools to +--+----> mapping.txt |
| obfuscate and | | +-------------+
| optimize | |
++--------------+ |
| |
+v--------------+ |
| javac | |
++--------------+ |
| |
+v-----------------v-+
| dx |
+---------+----------+
|
| above is accessible to developers
----------+----------------------------
| below is running on devices
|
+---------+--------------------------+
| APK | |
| v |
| classes.dex |
| | |
+---------+--------------------------+
|
+---------+--------------------------+
| | |
| +----v-----+ |
| | dalvikvm | |
| +----+-----+ |
+------------------------------------+
```
It's 2010. The Nexus One puts a 1Ghz processor, 512MB of RAM, and 512MB
Storage into people's pockets. Apps need to go fast, and the dalvikvm
implemented Just-In-Time (JIT) compilation of Dalvik to machine code.
Another executable, dexopt, prepares an app's DEX code for this by saving
it as an ODEX file, containing optimized DEX code.
For some code, this is not fast enough. The Native Development Kit (NDK)
allows developers to include libraries written in C and C++ in their apps.
The Java Native Interface (JNI) allows this code to be called, allowing
bundling of optimized libraries.
```txt
+-------------+ +-------------+
| source.java | | library.jar |
++------------+ ++------------+
| |
+v--------------+ | +-------------+
| tools to +--+----> mapping.txt |
| obfuscate and | | +-------------+
| optimize | |
++--------------+ |
| |
+v--------------+ | +------------+
| javac | | | source.cpp |
++--------------+ | ++-----------+
| | |
+v-----------------v-+ +v-----------+
| dx | | NDK |
+---------+----------+ +--+---------+
| |
| | above is accessible to developers
----------+----------------+-----------
| | below is running on devices
| |
+---------+----------------+---------+
| APK | | |