2. About ...
β Author of:
β Introduced Linux Trace Toolkit in 1999
β Originated Adeos and relayfs (kernel/relay.c)
3. 1. Motivation
2. Stack Comparison
3. Android-Specific Concepts
4. Useful Embedded Linux Tricks
5. Pros and Cons
4. 1. Motivation
β Embedded Linux developers:
β Developing Android apps
β Porting apps
β Creating custom Android device
β Android developers:
β Leverage Linux heritage
β Learn embedded Linux tricks
β Understand relation to embedded Linux
5. 2. Stack Comparison
β Starting point
β Architecture
β Requirements
β Development approach
β Development tools
β Development environment
β Bootloader
β Kernel
β Root filesystem
β Startup
β Default applications
β Application development
6. 2.1. Starting point - EL
β Usually:
β Bare/custom hardware
β Create/building custom stack components
β Lots of low-level work ahead (drivers, etc.)
β No tools to start with
7. 2.1. Starting point - Android
β Usually β App dev:
β Functional consumer-ready device
β Fully-functional development environment
β Distribution system for your app: Market
β Exceptionally β Device dev:
β Bare/custom hardware
β Create/building custom stack components
β Lots of low-level work ahead (drivers, etc.)
β Fewer tools to start with
10. 2.3. Requirements - EL
β Minimal:
β Linux workstation
β GNU cross-development toolchain
β Internet connection β download packages
β Extras:
β Distribution (free or $)
β JTAG debugger
11. 2.3. Requirements - Android
β Minimal:
β Windows / Mac / Linux workstation
β JDK
β Android SDK
β Eclipse
β Extras:
β Android NDK
β Android Open Source Project
12. 2.4. Development approach - EL
1.Get a functional toolchain
2.Generate and βinstallβ bootloader
3.Generate and βinstallβ kernel
4.Create a basic functional root filesystem
5.Configure target to boot over NFS
6. ... Develop ...
7.Write root filesystem to flash
8.Ship
13. 2.4. Development approach - Android
1.Create new project in Eclipse
2.Write application
3.Use emulator to debug
4.Test application on real device(S)
5.Ship
14. 2.5. Development tools - EL
β GNU cross-development toolchain:
β gcc - compiler
β as - assembler
β ld - linker
β gdb/gdbserver - debugger
β etc.
β C library: uClibc, eglibc or glibc
β JTAG debugger
15. 2.5. Development tools - Android
β SDK:
β android β manage AVDs and SDK components
β apkbuilder β creating .apk packages
β dx β converting .jar to .dex
β adb β debug bridge
β emulator β QEMU-based ARM emulator
β ...
β Eclipse w/ ADT plugin
β NDK: GNU toolchain for native binaries
16. 2.6. Development environment - EL
β Editor/IDE:
β vi
β emacs
β Eclipse
β K Develop
β CLI
β Serial port emulator: minicom, C-Kermit, ...
18. 2.7. Bootloader - EL
β U-Boot: PPC, ARM, x86, MIPS, SuperH, ...
β CLI interface, serial-based
β Environment variables, including built-in scripts
β Evolved from forks and merges (8xxrom / PPCBoot / ARMBoot)
β Very versatile and flexible bootloader
β Actively maintained
β Very mature
β Supports a wide range of boot methods: TFTP, DHCP, IDE, SCSI,
DOC, JFFS2, ...
β Fairly well documented
β Distributed under the terms of the GPL
19. 2.7. Bootloader - Android
β aosp/bootable/bootloader
β Custom bootloader for Android
β USB-based
β Implements the βfastbootβ protocol
β Controlled via βfastbootβ cli tool on host
β aosp/bootable/recovery
β UI-based recovery boot program
β Accessed through magic key sequence at boot
β Usually manufacturer specific variant
20. 2.8. Kernel β EL
β Stock kernel from kernel.org
β May require porting to custom hardware
22. 2.9. Root filesystem - EL
β /bin => Essential user binaries
β /boot => Bootloader and kernel images
β /dev => Device files
β /etc => System configuration
β /home => User home directories
β /lib => Essential shared libs and kernel modules
β /mnt => Temporary mount point
β /opt => Add-on software packages
β /proc => Virtual filesystem for kernel information
β /sbin => Essential system binaries
β /sys => Virtual filesystem for device access and information
β /tmp => Temporary files
β /usr => Secondary hierarchy (mostly user apps)
β /var => Variable data generated by daemons
23. 2.9. Root filesystem - Android
β Mainly
β /data => User data
β /system => System components
β Also found:
β /cache
β /mnt
β /sbin
β Etc.
24. 2.10. Startup - EL
β Software taking part in the system's startup:
β Bootloader
β Kernel
β Init process
β Bootloader:
β First to run
β Initializes hardware to a known state
β Places kernel parameters for the kernel to find
β Loads kernel and jumps to it
25. β Kernel:
β Early startup code is very hardware dependent
β Initializes environment for the running of C code
β Jumps to the architecture-independent start_kernel()
function.
β Initializes high-level kernel subsystems
β Mounts root filesystem
β Starts the init process
β The init process takes care of loading all the user
applications and starting the various daemons.
37. 3.1.1. Components
β 1 App = N Components
β Apps can use components of other applications
β App processes are automagically started whenever any part
is needed
β Ergo: N entry points, !1, and !main()
β Components:
β Activities
β Services
β Broadcast Receivers
β Content Providers
38. 3.1.2. Intents
β Intent = asynchronous message w/ or w/o
designated target
β Like a polymorphic Unix signal, but w/o
required target
β Intents βpayloadβ held in Intent Object
β Intent Filters specified in Manifest file
39. 3.1.3. Component lifecycle
β System automagically starts/stops/kills
processes:
β Entire system behaviour predicated on low memory
β System triggers Lifecycle callbacks when
relevant
β Ergo: Must manage Component Lifecycle
β Some Components are more complex to
manage than others
40. 3.1.4. Manifest file
β Informs system about appβs components
β XML format
β Always called AndroidManifest.xml
β Activity = <activity> ... static
β Service = <service> ... static
β Broadcast Receiver:
β Static = <receiver>
β Dynamic = Context.registerReceiver()
β Content Provider = <provider> ... static
41. 3.1.5. Processes and threads
β Processes
β Default: all callbacks to any app Component are issued to the main process thread
β <activity>β<service>β<recipient>β<provider> have process attribute to override
default
β Do NOT perform blocking/long operations in main process thread:
β Spawn threads instead
β Process termination/restart is at systemβs discretion
β Therefore:
β Must manage Component Lifecycle
β Threads:
β Create using the regular Java Thread Object
β Android API provides thread helper classes:
β Looper: for running a message loop with a thread
β Handler: for processing messages
β HandlerThread: for setting up a thread with a message loop
42. 3.1.6. Remote procedure calls
β Apparently System V IPC is evil ...
β Android RPCs = Binder mechanism
β Binder is a low-level functionality, not used as-is
β Instead: must define interface using Interface
Definition Language (IDL)
β IDL fed to aidl Tool to generate Java interface
definitions
43. 3.2. Framework components
β UI
β Data storage
β Security/Permissions
β ... and much more ... :
β Graphics
β Audio and Video
β Location and Maps
β Bluetooth
β NFC
44. 3.2.1. UI
β Everything based on hierarchy of Views and
ViewGroups (layouts)
β Declared in XML or dynamically through Java
β UI components:
β Widgets
β Event handlers
β Menus
β Dialogs
β Notifications
β ...
45. 3.2.2. Data storage
β Shared preferences
β Private primitive key-pair values
β Internal storage
β Private data on device memory
β External storage
β Public data on shared external device (SD)
β SQLite DB
β Private DB
β Network connection
β Web-based storage (REST)
46. 3.2.3. Security/Permissions
β Most security enforced at process level: UID, GID
β Permissions enforce restrictions on:
β Per-process operations
β Per-URI access
β Applications are sandboxed
β Specific permissions required to βexitβ sandbox
β Decision to grant access based on:
β Certificates
β User prompts
β All permissions must be declared statically
47. 3.3. Native development
β What it can and cannot do
β Getting and installing the NDK
β Using the NDK
β Implementing fully native apps
48. 3.3.1. What it can and cannot do
β Useful for:
β Porting existing body of code to Android
β Developing optimized native apps, especially for gaming
β Provides:
β Tools and build files to generate native code libraries from C/C++
β Way to embed native libs into .apk
β Set of stable (forward-compatible) native libs
β Documentation, samples and tutorials
β Enables:
β Calling native code from Java using JNI
β Implementing fully native apps (since 2.3)
β Doesn't allow you to:
β Compile traditional Linux/Unix apps as-is
49. 3.3.2. Getting and installing the NDK
β Whatβs in the NDK?
β Development tools
β Stable native APIs system headers
β Documentation - IMPORTANT
β Samples
β Getting the NDK
β http://developer.android.com/sdk/ndk/index.html
β Prerequisites
β Windows, Mac or Linux
β Complete SDK
β make (GNUβs) and awk
β For Windows, Cygwin 1.7 or higher
β NDK set up:
β Make sure prerequisites are installed
β Download and install NDK
50. 3.3.3. Using the NDK
1.Place native code under <project>/jni/...
2.Create <project>/jni/Android.mk to describe native code
to NDK
3.Optional: create <project>/jni/Application.mk for
describing which natives sources are required by app
4.Build native code:
β’ cd <project>
β’ <ndk>/ndk-build
5.Compile app with SDK. Native code will be shared lib
in .apk file.
51. 3.3.4. Implementing fully native apps
β Android 2.3 and up
β Native lifecycle management
β Still runs within context of dedicated Dalvik VM
β Can use JNI to call on Java functions
β Limited API:
β Activity lifecycle management
β Input events and sensors
β Window management
β Direct access to assets
β Make sure your activity is called: βandroid.app.NativeActivityβ
55. β Some features of interest:
β color-coded file lists
β tab completion
β "home", "end"
β grep, sed, wc, more, less
β vi
β ifconfig
β httpd
β sendmail
β tftp
β top
β ...
56. β Download BusyBox (1.18.3)
β Move to the directory for the rest of the setup:
$ cd busybox-1.18.3
β Configuration of BusyBox's options:
$ make menuconfig
β Options that must be set:
β βBuild Optionsβ -> βDo you want to build BusyBox with a Cross Compiler?β
β Cross-compiler prefix: arm-unknown-linux-gnueabi-
β βInstallation Optionsβ -> βDon't use /usrβ
β Installation prefix: ${PRJROOT}/rootfs
β Build:
$ make
β Install:
$ make install
57. β Cheat sheet:
Commands to get the new Busybox onto the rootfs:
adb shell mount -o remount,rw rootfs /
adb shell mkdir /bin
adb push busybox /bin/
adb shell /bin/busybox --install /bin
adb shell
To do after going into the shell:
/bin/ash
export PATH=/bin:$PATH
58. 4.3. uClibc
β Originates from uClinux effort
β Support both CPUs that have and those that
lack an MMU and/or an FPU.
β Allows both static and dynamic linking
β Most applications that build with glibc will build
and work the same with uClibc.
β Available from: http://uclibc.org/
59. 5. Pros and Cons
β Target application type:
β Stock Android vs. Custom Android
β Development language
β Licensing
β Branding:
β Do you care about the CTS?